﻿found_despotate = {
	# Basic Setup
	skill = diplomacy
	desc = "found_despotate_desc_general"
	success_desc = "FOUND_DESPOTATE_SUCCESS_DESC"
	icon = icon_found_despotate
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = political
	target_type = title
	cooldown = { years = 10 }

	# Parameters
	speed_per_skill_point = t3_spsp_owner_value
	speed_per_target_skill_point = t3_spsp_target_value
	spymaster_speed_per_skill_point = t3_spsp_owner_spy_value
	target_spymaster_speed_per_skill_point = 0
	tier_speed = 1
	base_progress_goal = 365
	base_maximum_success = t2_base_max_success_value
	phases_per_agent_charge = 1
	success_chance_growth_per_skill_point = t3_scgpsp_value

	is_secret = no

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		is_governor = yes
		highest_held_title_tier < tier_kingdom
		top_liege.highest_held_title_tier >= tier_empire
	}

	valid = {
		scope:owner = { is_governor = yes }
		scope:target = {
			tier = tier_kingdom
			is_landless_type_title = no
			any_de_jure_county = {
				county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
			}
			NOT = { holder ?= scope:owner }
			is_title_created = no
		}
		trigger_if = {
			limit = {
				scope:owner ?= { has_active_diarchy = yes } #Unop: Replace scope:actor with scope:owner as actor does not seems to exists
			}
			NOT = {
				scope:owner.liege.diarch ?= { #Unop: Replace scope:actor with scope:owner as actor does not seems to exists
					any_held_title = {
						any_this_title_or_de_jure_above = { this = scope:target }
					}
				}
			}
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		#Vassals who would be vassalized you are NOT INTO THIS
		modifier = {
			add = -50
			is_landed = yes
			NOT = {
				is_vassal_of = scope:owner
			}
			any_held_title = {
				kingdom = scope:target
			}
			highest_held_title_tier < tier_empire
			desc = WILL_BE_VASSALIZED_REASON
		}
		modifier = {
			add = -25
			is_ruler = yes
			liege ?= scope:owner.liege
			desc = RIVAL_VASSAL_REASON
		}
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { peer_vassals vassals councillors courtiers scripted_relations }

	odds_prediction = {
		add = base_odds_prediction_target_is_title_value
		add = odds_skill_contribution_diplomacy_title_value
		add = agent_groups_owner_perspective_value
		add = odds_found_despotate_scheme_misc_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = -25

		# Skill
		compare_modifier = {
			desc = "SCHEME_MY_DIPLOMACY"
			value = scope:owner.diplomacy
			multiplier = 2
		}
		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_title_within_realm_bonus_modifier = yes #Unop: Use the correct modifier (was diarch_scheming_within_realm_bonus_modifier)
		# Land Context
		modifier = { # Owner's counties outside primary title
			scope:owner = {
				any_sub_realm_county = {
					NOT = { target_is_de_facto_liege_or_above = scope:owner.primary_title }
				}
			}
			desc = FOUND_DESPOTATE_HELD_PROVINCES_DESC
			add = {
				value = 0
				scope:owner = {
					every_sub_realm_county = {
						limit = {
							NOT = { target_is_de_facto_liege_or_above = scope:owner.primary_title }
						}
						add = -10
					}
				}
			}
		}
		modifier = { # Held counties in despotate
			desc = FOUND_DESPOTATE_HELD_COUNTIES_IN_TARGET_DESC
			add = {
				value = 0
				scope:target = {
					every_de_jure_county = {
						limit = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
						add = 0.5
					}
				}
			}
		}
		modifier = { # Controlled duchies in despotate
			desc = FOUND_DESPOTATE_HELD_DUCHIES_IN_TARGET_DESC
			add = {
				value = 0
				scope:target = {
					every_direct_de_jure_vassal_title = {
						limit = {
							save_temporary_scope_as = duchy_temp
							scope:owner = { completely_controls = scope:duchy_temp }
						}
						add = 5
					}
				}
			}
		}
		modifier = { # Controlled duchies in despotate
			scope:owner.top_liege.capital_county.kingdom ?= scope:target
			desc = FOUND_DESPOTATE_IMPERIAL_CAPITAL_DESC
			add = -15
		}
		# House
		modifier = {
			scope:owner.house = {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = OWNER_POWERFUL_FAMILY_DESC
			add = 10
		}
		modifier = {
			scope:owner.house = { is_dominant_family = yes }
			desc = OWNER_DOMINANT_FAMILY_DESC
			add = 20
		}
		# Influence
		modifier = {
			desc = OWNER_INFLUENCE_LEVEL_DESC
			add = {
				value = scope:owner.influence_level
				multiply = 5
			}
		}
		# Favour
		opinion_modifier = {
			who = scope:owner.top_liege
			opinion_target = scope:owner
			multiplier = 0.2
		}
		# house_head_request_interaction
		modifier = {
			add = political_scheme_variable_list_value
			scope:owner = {
				has_variable_list = supporting_political_schemes
			}
			desc = HOUSE_HEAD_SCHEME_SUPPORT_DESC
		}
		modifier = {
			add = -10
			scope:owner = { has_character_modifier = political_schemes_distracted_modifier }
			desc = political_schemes_distracted_modifier
		}
		#Modifiers
		modifier = {
			scope:owner = {
				has_character_modifier = united_political_agents_modifier
				is_vassal_of = scope:target.holder #Unop ck3-tiger
			}
			desc = united_political_agents_modifier
			add = 15
		}
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:indirect
		}
		# Fallback agents — a balanced config if you start the scheme from script.
		if = {
			limit = {
				scheme_owner = {
					NOT = { has_variable = agents_added }
				}
			}
			add_agent_slot = agent_intermediary
			add_agent_slot = agent_proponent
			add_agent_slot = agent_sycophant
			add_agent_slot = agent_political_socialite
			add_agent_slot = agent_bureaucrat
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		found_despotate_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { found_despotate_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			found_despotate_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}
	on_monthly = { fire_generic_contract_scheme_on_going_events_effect = yes }

	## Invalidation
	on_invalidated = {
		#scheme_target_character = { save_scope_as = target } #Unop ck3-tiger
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = found_despotate_invalidated_title
				left_icon = scope:target

				if = { # Invalidation due to loss of land
					limit = {
						exists = scope:target.holder
						scope:target.holder != scope:owner
					}
					custom_tooltip = scheme_target_another_holder
				}
				else_if = { # Invalidation due to death
					limit = {
						scope:target.holder = { #Unop ck3-tiger
							any_sub_realm_county = {
								count = 0
								county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
							}
						}
					}
					custom_tooltip = scheme_target_no_counties_within
				}
			}
		}
	}
}
