### Grand Vizier Tasks ###
# Three tasks for grand_vizier_court_position
# Adapted for CK3 1.19 framework

# ============================================================
# TASK 1: Administer the Realm
# Vizier focuses on managing crownlands.
# ============================================================

grand_vizier_administer_realm = {
	court_position_types = { grand_vizier_court_position }

	# Monthly cost for the task
	cost = {
		round = no
		gold = {
			value = 0
			if = {
				limit = {
					scope:liege = {
						has_treasury = no
					}
				}
				add = {
					value = monthly_court_position_task_cost
					desc = COURT_POSITION_TASK_COST_BREAKDOWN_BASE
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
		}
		treasury = {
			value = 0
			if = {
				limit = {
					scope:liege = {
						has_treasury = yes
					}
				}
				add = {
					value = monthly_court_position_task_cost
					desc = COURT_POSITION_TASK_COST_BREAKDOWN_BASE
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
		}
	}

	# The Vizier benefits from the administrative routine
	employee_modifier = {
		monthly_stewardship_lifestyle_xp_gain_mult = 0.10
		monthly_prestige = 0.5
	}

	# Employer gains scale with Vizier aptitude
	scaling_employer_modifiers = {
		terrible = {
			domain_tax_mult = 0.01
			build_speed = -0.02
			holding_build_speed = -0.02
		}
		poor = {
			domain_tax_mult = 0.02
			build_speed = -0.04
			holding_build_speed = -0.04
		}
		average = {
			domain_tax_mult = 0.03
			build_speed = -0.06
			holding_build_speed = -0.06
			stress_gain_mult = -0.05
		}
		good = {
			domain_tax_mult = 0.04
			build_speed = -0.08
			holding_build_speed = -0.08
			stress_gain_mult = -0.10
		}
		excellent = {
			domain_tax_mult = 0.05
			build_speed = -0.10
			holding_build_speed = -0.10
			stress_gain_mult = -0.10
		}
	}

	is_shown = { }
	is_valid_showing_failures_only = { }

	on_start = { }
	on_end = { }

	ai_will_do = {
		value = 1
		# More likely if realm is large
		if = {
			limit = {
				vassal_count >= 20
			}
			add = 20
		}
		# AI falls back to this task if they can't afford the others
		if = {
			limit = {
				has_treasury = no
				monthly_character_income < monthly_court_position_task_cost_double
			}
			add = -1000
		}
		else_if = {
			limit = {
				has_treasury = yes
				monthly_character_treasury_income < monthly_court_position_task_cost_double
			}
			add = -1000
		}
	}
}

# ============================================================
# TASK 2: Collect Taxes
# The Vizier turns his focus to revenue extraction of vassals.
# ============================================================

grand_vizier_collect_taxes = {
	court_position_types = { grand_vizier_court_position }

	# Monthly cost for the task
	cost = {
		round = no
		gold = {
			value = 0
			if = {
				limit = {
					scope:liege = {
						has_treasury = no
					}
				}
				add = {
					value = monthly_court_position_task_cost
					desc = COURT_POSITION_TASK_COST_BREAKDOWN_BASE
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
		}
		treasury = {
			value = 0
			if = {
				limit = {
					scope:liege = {
						has_treasury = yes
					}
				}
				add = {
					value = monthly_court_position_task_cost
					desc = COURT_POSITION_TASK_COST_BREAKDOWN_BASE
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
		}
	}

	employee_modifier = {
		monthly_prestige = 0.5
		general_opinion = -5
		monthly_stewardship_lifestyle_xp_gain_mult = 0.05
	}

	# Tax bonuses scale with aptitude
	scaling_employer_modifiers = {
		terrible = {
			clan_government_tax_contribution_mult = 0.02
			vassal_opinion = -5
		}
		poor = {
			clan_government_tax_contribution_mult = 0.04
			vassal_opinion = -5
		}
		average = {
			clan_government_tax_contribution_mult = 0.08
			vassal_opinion = -5
		}
		good = {
			clan_government_tax_contribution_mult = 0.10
			vassal_opinion = -5
		}
		excellent = {
			clan_government_tax_contribution_mult = 0.15
			vassal_opinion = -5
		}
	}

	is_shown = { }
	is_valid_showing_failures_only = { }

	on_start = { }
	on_end = { }

	ai_will_do = {
		value = 1
		# AI prioritizes this when gold income is low
		if = {
			limit = {
				monthly_character_income < 5
			}
			add = 30
		}
		# AI avoids if vassals are already unhappy
		if = {
			limit = {
				average_vassal_opinion < -10
			}
			add = -40
		}
		if = {
			limit = {
				has_treasury = no
				monthly_character_income < monthly_court_position_task_cost_double
			}
			add = -1000
		}
		else_if = {
			limit = {
				has_treasury = yes
				monthly_character_treasury_income < monthly_court_position_task_cost_double
			}
			add = -1000
		}
	}
}

# ============================================================
# TASK 3: Manage Vassal Relations
# The Vizier acts as an intermediary between the ruler and
# their vassals, improving vassal opinion & contribution
# ============================================================

grand_vizier_manage_vassals = {
	court_position_types = { grand_vizier_court_position }

	# Monthly cost for the task — paid in prestige as political capital
	cost = {
		round = no
		prestige = {
			add = {
				value = monthly_court_position_task_cost
				desc = COURT_POSITION_TASK_COST_BREAKDOWN_BASE
				format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
			}
		}
	}

	# The Vizier becomes a more accomplished diplomat through this work
	employee_modifier = {
		monthly_diplomacy_lifestyle_xp_gain_mult = 0.10
		diplomacy = 1
	}

	# Vassal opinion and military contributions scale with aptitude
	scaling_employer_modifiers = {
		terrible = {
			vassal_opinion = 1
			vassal_levy_contribution_mult = 0.01
			legitimacy_gain_mult = 0.03
		}
		poor = {
			vassal_opinion = 3
			vassal_levy_contribution_mult = 0.02
			legitimacy_gain_mult = 0.03
		}
		average = {
			vassal_opinion = 4
			vassal_levy_contribution_mult = 0.03
			legitimacy_gain_mult = 0.05
		}
		good = {
			vassal_opinion = 6
			vassal_levy_contribution_mult = 0.05
			legitimacy_gain_mult = 0.08
		}
		excellent = {
			vassal_opinion = 8
			vassal_levy_contribution_mult = 0.08
			legitimacy_gain_mult = 0.1
		}
	}

	is_shown = { }

	# Only valid when the ruler actually has vassals to manage
	is_valid_showing_failures_only = {
		scope:liege = {
			custom_tooltip = {
				text = grand_vizier_manage_vassals_requires_vassals_tt
				vassal_count >= 1
			}
		}
	}

	on_start = { }
	on_end = { }

	ai_will_do = {
		value = 1
		# AI prefers this when vassals are restless
		if = {
			limit = {
				average_vassal_opinion < 0
			}
			add = 30
		}
		if = {
			limit = {
				average_vassal_opinion < -20
			}
			add = 30
		}
		# Not worth it with no vassals
		if = {
			limit = {
				vassal_count < 1
			}
			add = -1000
		}
		# AI avoids if prestige income is very low
		if = {
			limit = {
				monthly_character_income < monthly_court_position_task_cost_double
				monthly_prestige < monthly_court_position_task_cost_double
			}
			add = -500
		}
	}
}
