﻿depose = {
	# Basic Setup
	skill = intrigue
	desc = "depose_desc_general"
	success_desc = "DEPOSE_SUCCESS_DESC"
	icon = icon_scheme_depose
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = political
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	cooldown = { years = 5 }

	# Parameters
	speed_per_skill_point = t2_spsp_owner_value
	speed_per_target_skill_point = t2_spsp_target_value
	base_progress_goal = t2_base_phase_length_value
	maximum_secrecy = 95
	base_maximum_success = t2_base_max_success_value
	phases_per_agent_charge = 1
	success_chance_growth_per_skill_point = t2_scgpsp_value

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		government_allows = administrative
	}

	valid = {
		scope:target = {
			government_allows = administrative
			is_alive = yes
			in_diplomatic_range = prev
			this != scope:owner
			primary_title.tier >= tier_empire
			top_liege.primary_title = prev.top_liege.primary_title # This hides the tooltip
		}
		
		# If liege has the tradition "Palace Politics" you cannot be unfit to rule
		trigger_if = {
			limit = {
				scope:target.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
			}
			valid_for_byz_succession_trigger = yes
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		ai_agent_join_chance_hostile_general_suite_modifier = yes
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { courtiers peer_vassals vassals councillors }
	agent_groups_target_character_perspective = { courtiers vassals }

	odds_prediction = {
		add = hostile_scheme_base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_intrigue_value
		add = agent_groups_owner_perspective_value
		add = agent_groups_target_character_perspective_value
		add = odds_depose_scheme_misc_value 
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 0

		scheme_type_skill_success_chance_modifier = { SKILL = INTRIGUE }

		# Countermeasures
		apply_calculated_scheme_success_chance_adjustments_modifier = yes

		# Spiritual Head of Faith
		modifier = {
			add = -30
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:target = {
				faith = scope:owner.faith
				faith = {
					has_doctrine = doctrine_spiritual_head
				}
				faith = {
					religious_head ?= {
			 			this = scope:target
			 		}
				}
			}
		}

		# Sponsored Inspiration
		modifier = {
			add = -15
			desc = target_has_sponsored_inspiration
			scope:target = {
				inspiration ?= { exists = inspiration_sponsor }
			}
		}

		# Family Feud
		house_feud_hostile_scheme_success_modifier = yes

		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_within_realm_bonus_modifier = yes

		# Modifiers
		modifier = {
			scope:owner = {
				has_character_modifier = united_political_agents_modifier
				is_vassal_of = scope:target
			}
			desc = united_political_agents_modifier
			add = 15
		}

		modifier = {
			scope:owner.house = { is_dominant_family = yes }
			add = {
				value = 25
				desc = is_dominant_family_desc
			}
		}

		# Distance Penalty
		ttd_medium_distance_success_chance_modifier = yes
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:calculating
		}
		add_scheme_starting_opportunities_intrigue_effect = yes
		# 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_political_socialite
			add_agent_slot = agent_bureaucrat
			add_agent_slot = agent_courtesan
		}
	}
	on_hud_click  = { depose_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			depose_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}
	## Invalidation
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = depose_invalidated_title
				left_icon = scope:target
				if = { # Invalidation due to death
					limit = {
						scope:target = { is_alive = no }
						NOT = { block_death_event_trigger = { DEAD = scope:target } }
					}
					custom_tooltip = scheme_target_died
				}
				else_if = { # Diplo range: is it relevant here?
					limit = {
						scope:target = { 
							NOT = { in_diplomatic_range = scope:owner } 
						}
					}
					custom_tooltip = scheme_target_not_in_diplomatic_range
				}
				else_if = { # No more admin
					limit = {
						scope:target = {
							NOT = { government_allows = administrative }
						}
					}
					custom_tooltip = scheme_target_not_admin
				}
				else_if = { # No more admin
					limit = {
						scope:owner = {
							NOT = { government_allows = administrative }
						}
					}
					custom_tooltip = scheme_owner_not_admin
				}
			}
		}
	}

	## Ongoing
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}
			fire_generic_contract_scheme_on_going_events_effect = yes
		}
	}
}
