﻿namespace = succession_crisis_diplomacy_event

succession_crisis_diplomacy_event.0001 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0001.t
	desc = succession_crisis_diplomacy_event.0001.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		if = {
			limit = { NOT = { exists = scope:current_ruler } }
			if = {
				limit = { exists = involved_activity }
				involved_activity = {
					activity_host = { save_scope_as = current_ruler }
				}
			}
		}

		if = {
			limit = { NOT = { exists = scope:crisis_destination } }
			root = {
				location = { save_scope_as = crisis_destination }
			}
		}

		if = {
			limit = { 
				NOT = { exists = scope:crisis_heir }
				root = { has_character_flag = crisis_heir_flag } 
			}
			root = { save_scope_as = crisis_heir }
		}
		else_if = {
			limit = { 
				NOT = { exists = scope:crisis_heir }
				exists = involved_activity 
			}
			involved_activity = {
				activity_host = { save_scope_as = crisis_heir }
			}
		}
		
		if = {
			limit = { 
				NOT = { root = { has_character_flag = crisis_heir_flag } }
				NOT = { exists = scope:claimant }
			}
			root = { save_scope_as = claimant }
		}

		if = {
			limit = { NOT = { exists = scope:warleader } }
			root = { save_scope_as = warleader }
		}

		if = {
			limit = { 
				exists = scope:current_ruler 
				NOT = { exists = scope:crisis_title }
			}
			scope:current_ruler = {
				primary_title = {
					save_scope_as = crisis_title
				}
			}
		}

		set_variable = {
			name = crisis_diplomacy_score
			value = 0
		}
		if = {
			limit = {
				is_adult = no
				has_active_diarchy = yes
			}
			diarch = {
				save_scope_as = left_portrait_char
			}
		}
		else = {
			root = {
				save_scope_as = left_portrait_char
			}
		}

		scope:current_ruler = {
			if = {
				limit = {
					is_adult = no
					has_active_diarchy = yes
				}
				diarch = {
					save_scope_as = right_portrait_char
				}
			}
			else = {
				save_scope_as = right_portrait_char
			}
		}

		scope:activity = {
			every_attending_character = {
				limit = {
					NOT = { this = scope:left_portrait_char }
					NOT = { this = scope:right_portrait_char }
				}
				add_to_list = potential_witnesses
			}
		}
		
		random_in_list = {
			list = potential_witnesses
			save_scope_as = witness_left
		}
		
		random_in_list = {
			list = potential_witnesses
			limit = { NOT = { this = scope:witness_left } }
			save_scope_as = witness_right
		}

		random_in_list = {
			list = potential_witnesses
			limit = { 
				NOT = { this = scope:witness_left }
				NOT = { this = scope:witness_right }
			}
			save_scope_as = witness_center
		}

		random_in_list = {
			list = potential_witnesses
			limit = { 
				NOT = { this = scope:witness_left }
				NOT = { this = scope:witness_right }
				NOT = { this = scope:witness_center }
			}
			save_scope_as = witness_lower_left
		}
	}

	left_portrait = {
		character = scope:witness_left
		camera = camera_event_left_pointing_back_fov20
		animation = frontend_center_idle
	}

	center_portrait = {
		character = scope:left_portrait_char
		animation = wedding_objection_start
		camera = camera_activity_horse
	}

	right_portrait = {
		character = scope:witness_right
		animation = toast_goblet
		camera = camera_event_center_away_pointing_left_fov25
	}

	lower_left_portrait = {
		character = scope:witness_lower_left
		animation = frontend_right_idle
	}

	lower_center_portrait = {
		character = scope:witness_center
		animation = eavesdrop
	}

	lower_right_portrait = {
		character = scope:right_portrait_char
		animation = throne_room_conversation_1
	}

	option = {
		name = succession_crisis_diplomacy_event.0001.a
		
		ai_chance = {
			base = 10
			modifier = { add = 20 has_trait = calm }
			modifier = { add = 20 has_trait = patient }
			modifier = { add = 20 has_trait = diligent }
			modifier = { add = 20 has_trait = honest }
			modifier = { add = 50 diplomacy >= 15 }
		}

		custom_tooltip = succession_crisis_diplomacy_event.0001.a.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						scope:left_portrait_char.diplomacy > scope:right_portrait_char.diplomacy
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0001.b
		
		ai_chance = {
			base = 10
			modifier = { add = 20 has_trait = arrogant }
			modifier = { add = 20 has_trait = ambitious }
			modifier = { add = 20 has_trait = brave }
			modifier = { add = 50 prestige_level >= 4 }
		}

		custom_tooltip = succession_crisis_diplomacy_event.0001.b.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						scope:left_portrait_char.prestige_level >= 3
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0001.c
		
		trigger = {
			short_term_gold >= 1000
		}

		ai_chance = {
			base = 5
			modifier = { add = 30 has_trait = greedy }
			modifier = { add = 20 has_trait = cynical }
			modifier = { add = 10 has_trait = callous }
			modifier = { add = 50 gold >= 2000 }
		}
		
		remove_short_term_gold = medium_gold_value
		
		custom_tooltip = succession_crisis_diplomacy_event.0001.c.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 40
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0001.d
		
		ai_chance = {
			base = 10
			modifier = { add = 20 has_trait = cynical }
			modifier = { add = 20 has_trait = stubborn }
			modifier = { add = 20 has_trait = diligent }
			modifier = { add = 20 has_trait = patient }
			modifier = { add = 50 learning >= 15 }
		}

		custom_tooltip = succession_crisis_diplomacy_event.0001.d.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						scope:left_portrait_char.learning > scope:right_portrait_char.learning
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}
}
# ============================================================
# EVENT 0002:  DIPLOMACY REJECTED
# ============================================================
succession_crisis_diplomacy_event.0002 = {
	type = character_event
	title = succession_crisis_diplomacy_event.0002.t
	desc = succession_crisis_diplomacy_event.0002.desc
	theme = diplomacy
	window = big_event_window
	override_background = council_chamber

	immediate = {
		root = { save_scope_as = warleader }
		
		if = {
			limit = { root = { has_character_flag = crisis_heir_flag } }
			root = { save_scope_as = crisis_heir }
		}
		else = {
			scope:current_ruler = { save_scope_as = crisis_heir }
		}

		if = {
			limit = { root = { has_variable = crisis_title } }
			root = {
				var:crisis_title = { save_scope_as = crisis_title }
			}
		}
	}

	left_portrait = {
		character = root
		animation = map_rage
		camera = camera_body_right
	}

	right_portrait = {
		character = scope:current_ruler
		animation = disapproval
		camera = camera_body
	}

	# OPTION A: TO ARMS!
	option = {
		name = succession_crisis_diplomacy_event.0002.a
		custom_tooltip = succession_crisis_diplomacy_event.0002.a.tt
		succession_crisis_process_vassal_loyalty_effect = yes
		
		ai_chance = {
			base = 60
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = 40
				has_trait = wrathful
			}
			modifier = {
				add = 20
				has_trait = brave
			}
			modifier = {
				add = 20
				has_trait = arrogant
			}
			modifier = {
				add = -50
				has_trait = craven
			}
			modifier = {
				add = -50
				has_trait = content
			}
			modifier = {
				add = -20
				has_trait = calm
			}
		}

		if = {
			limit = {
				root = { has_character_flag = crisis_heir_flag }
			}
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_claimant_event.0005
					days = { 14 30 }
				}
			}
		}
		else = {
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_heir_event.0006
					days = { 14 30 }
				}
			}
		}

		trigger_event = succession_crisis_travel.0002
	}

	# OPTION B: ACCEPT THE VERDICT
	option = {
		name = succession_crisis_diplomacy_event.0002.b
		custom_tooltip = succession_crisis_diplomacy_event.0002.b.tt
		
		ai_chance = {
			base = 40
			modifier = {
				add = 50
				has_trait = content
			}
			modifier = {
				add = 50
				has_trait = craven
			}
			modifier = {
				add = 30
				has_trait = calm
			}
			modifier = {
				add = 30
				has_trait = humble
			}
			modifier = {
				add = -40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				has_trait = wrathful
			}
		}

		add_prestige = -500
		add_stress = 40
		trigger_event = succession_crisis_travel.0002
	}
}

# ============================================================
# EVENT 0003:  DIPLOMACY ROUND 2 (CLAIMANT PERSPECTIVE)
# ============================================================
succession_crisis_diplomacy_event.0003 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0003.t
	desc = succession_crisis_diplomacy_event.0003.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		if = {
			limit = { NOT = { exists = scope:current_ruler } }
			if = {
				limit = { exists = involved_activity }
				involved_activity = {
					activity_host = { save_scope_as = current_ruler }
				}
			}
		}

		if = {
			limit = { NOT = { exists = scope:crisis_destination } }
			root = {
				location = { save_scope_as = crisis_destination }
			}
		}

		if = {
			limit = { 
				NOT = { exists = scope:crisis_heir }
				root = { has_character_flag = crisis_heir_flag } 
			}
			root = { save_scope_as = crisis_heir }
		}
		else_if = {
			limit = { 
				NOT = { exists = scope:crisis_heir }
				exists = involved_activity 
			}
			involved_activity = {
				activity_host = { save_scope_as = crisis_heir }
			}
		}

		if = {
			limit = { NOT = { exists = scope:claimant } }
			root = { save_scope_as = claimant }
		}

		if = {
			limit = { NOT = { exists = scope:warleader } }
			root = { save_scope_as = warleader }
		}

		if = {
			limit = { 
				exists = scope:current_ruler 
				NOT = { exists = scope:crisis_title }
			}
			scope:current_ruler = {
				primary_title = {
					save_scope_as = crisis_title
				}
			}
		}

		succession_crisis_ancestry_check = yes
		scope:current_ruler = {
			succession_crisis_ancestry_check = yes
		}
		if = {
			limit = {
				is_adult = no
				has_active_diarchy = yes
			}
			diarch = {
				save_scope_as = left_portrait_char
			}
		}
		else = {
			root = {
				save_scope_as = left_portrait_char
			}
		}

		# Save Ruler or their Diarch as right_portrait_char
		scope:current_ruler = {
			if = {
				limit = {
					is_adult = no
					has_active_diarchy = yes
				}
				diarch = {
					save_scope_as = right_portrait_char
				}
			}
			else = {
				save_scope_as = right_portrait_char
			}
		}

		scope:activity = {
			every_attending_character = {
				limit = {
					NOT = { this = scope:left_portrait_char }
					NOT = { this = scope:right_portrait_char }
				}
				add_to_list = potential_witnesses
			}
		}
		
		random_in_list = {
			list = potential_witnesses
			save_scope_as = witness_left
		}
		
		random_in_list = {
			list = potential_witnesses
			limit = { NOT = { this = scope:witness_left } }
			save_scope_as = witness_right
		}

		random_in_list = {
			list = potential_witnesses
			limit = { 
				NOT = { this = scope:witness_left }
				NOT = { this = scope:witness_right }
			}
			save_scope_as = witness_center
		}

		random_in_list = {
			list = potential_witnesses
			limit = { 
				NOT = { this = scope:witness_left }
				NOT = { this = scope:witness_right }
				NOT = { this = scope:witness_center }
			}
			save_scope_as = witness_lower_left
		}
	}

	left_portrait = {
		character = scope:witness_left
		camera = camera_event_left_pointing_back_fov20
		animation = chancellor

	}

	center_portrait = {
		character = scope:left_portrait_char
		animation = debating
		camera = camera_activity_horse
	}

	right_portrait = {
		character = scope:witness_right
		animation = writing
		camera = camera_event_center_away_pointing_left_fov25
	}

	lower_left_portrait = {
		character = scope:witness_lower_left
		animation = frontend_right_idle
	}

	lower_center_portrait = {
		character = scope:witness_center
		animation = eavesdrop
	}

	lower_right_portrait = {
		character = scope:right_portrait_char
		animation = throne_room_conversation_1
	}

	option = {
		name = succession_crisis_diplomacy_event.0003.a
		
		ai_chance = {
			base = 10
			modifier = {
				add = 90
				var:crisis_ancestry_score > scope:right_portrait_char.var:crisis_ancestry_score
			}
		}

		custom_tooltip = succession_crisis_diplomacy_event.0003.a.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						var:crisis_ancestry_score > scope:right_portrait_char.var:crisis_ancestry_score
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0003.b
		
		ai_chance = {
			base = 10
			modifier = { add = 20 has_trait = callous }
			modifier = { add = 20 has_trait = cynical }
			modifier = { add = 50 intrigue >= 15 }
		}

		custom_tooltip = succession_crisis_diplomacy_event.0003.b.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						scope:left_portrait_char.intrigue > scope:right_portrait_char.intrigue
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0003.c
		
		ai_chance = {
			base = 10
			modifier = { add = 20 has_trait = zealous }
			modifier = { add = 50 piety_level >= 3 }
		}

		custom_tooltip = succession_crisis_diplomacy_event.0003.c.tt
		hidden_effect = {
			random_list = {
				50 = {
					modifier = {
						add = 25
						scope:left_portrait_char.piety_level >= scope:right_portrait_char.piety_level
					}
					change_variable = {
						name = crisis_diplomacy_score
						add = 1
					}
					trigger_event = {
						id = succession_crisis_misc.0009
						days = { 10 30 }
					}
				}
				50 = {
					trigger_event = {
						id = succession_crisis_misc.0010
						days = { 10 30 }
					}
				}
			}
		}
	}
}
# ============================================================
# EVENT 0004:  DIPLOMACY WIN (CLAIMANT PERSPECTIVE)
# ============================================================
succession_crisis_diplomacy_event.0004 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0004.t
	desc = succession_crisis_diplomacy_event.0004.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia
	override_effect_2d = { reference = legend_glow }

	immediate = {
		root = { save_scope_as = winner }
		involved_activity = { activity_host = { save_scope_as = loser } }

		# Handle witness list selection for the triumph scene
		if = {
			limit = {
				scope:loser = {
					any_vassal = { has_character_flag = arrived_vassals }
				}
			}
			scope:loser = {
				random_vassal = {
					limit = { has_character_flag = arrived_vassals }
					save_scope_as = witness_left
				}
				random_vassal = {
					limit = { 
						has_character_flag = arrived_vassals 
						NOT = { this = scope:witness_left } 
					}
					save_scope_as = witness_right
				}
			}
		}
		else = {
			scope:loser = {
				random_courtier = {
					save_scope_as = witness_left
				}
				random_courtier = {
					limit = { NOT = { this = scope:witness_left } }
					save_scope_as = witness_right
				}
			}
		}

		root = {
			succession_crisis_diplomatic_transfer_effect = yes
		}
	}

	left_portrait = {
		character = scope:witness_left
		camera = camera_event_left_pointing_back_fov20
		animation = throne_room_applaud_1
	}

	center_portrait = {
		character = root
		animation = prayer
		camera = camera_activity_horse
	}

	right_portrait = {
		character = scope:witness_right
		camera = camera_event_center_away_pointing_left_fov25
		animation = throne_room_cheer_1
	}

	lower_right_portrait = {
		character = scope:loser
		animation = anger
	}

	option = {
		name = succession_crisis_diplomacy_event.0004.a
		
		if = {
			limit = {
				is_valid_for_legitimacy_change = yes
			}
			add_legitimacy = 100
			add_prestige = medium_prestige_value
		}
	}
}
# ============================================================
# EVENT 0005:  DIPLOMACY LOSE
# ============================================================
succession_crisis_diplomacy_event.0005 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0005.t
	desc = succession_crisis_diplomacy_event.0005.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		involved_activity = {
			activity_host = {
				save_scope_as = current_ruler
				if = {
					limit = { NOT = { exists = scope:crisis_title } }
					var:crisis_title = { save_scope_as = crisis_title }
				}
			}
		}

		root = {
			save_scope_as = warleader
		}

		# Handle witness selection using the arrived_vassals flag
		if = {
			limit = {
				scope:current_ruler = {
					any_vassal = { has_character_flag = arrived_vassals }
				}
			}
			scope:current_ruler = {
				random_vassal = {
					limit = { has_character_flag = arrived_vassals }
					save_scope_as = witness_left
				}
				random_vassal = {
					limit = { 
						has_character_flag = arrived_vassals 
						NOT = { this = scope:witness_left } 
					}
					save_scope_as = witness_right
				}
			}
		}
		else = {
			scope:current_ruler = {
				random_courtier = {
					save_scope_as = witness_left
				}
				random_courtier = {
					limit = { NOT = { this = scope:witness_left } }
					save_scope_as = witness_right
				}
			}
		}
	}

	left_portrait = {
		character = scope:witness_left
		camera = camera_event_left_pointing_back_fov20
		animation = throne_room_applaud_1
	}

	center_portrait = {
		character = scope:current_ruler
		animation = prayer
	}

	right_portrait = {
		character = scope:witness_right
		camera = camera_event_center_away_pointing_left_fov25
		animation = throne_room_cheer_1
	}

	lower_right_portrait = {
		character = root
		animation = anger
	}

	option = {
		name = succession_crisis_diplomacy_event.0005.a
		root = {
			remove_claim = scope:crisis_title
		}
		
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
		}
		
		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0005.b
		
		root = {
			add_character_flag = imminent_war
		}

		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
		}
		
		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}
# ============================================================
# EVENT 0006:  DIPLOMATIC COMPROMISE
# ============================================================
succession_crisis_diplomacy_event.0006 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0006.t
	desc = succession_crisis_diplomacy_event.0006.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		save_scope_as = claimant_to_marry
		save_scope_as = warleader
		random_child = {
			limit = {
				sc_diplomatic_marriage_check = yes
			}
			save_scope_as = claimant_child_to_marry
		}
		scope:current_ruler = {
			save_scope_as = ruler_to_marry
			random_child = {
				limit = {
					sc_diplomatic_marriage_check = yes
				}
				save_scope_as = ruler_child_to_marry
			}
		}
	}

	left_portrait = {
		character = root
		camera = camera_event_right_pointing_left
		animation = thinking
	}

	right_portrait = {
		character = scope:current_ruler
		camera = camera_event_right
		animation = throne_room_conversation_3
	}

	lower_left_portrait = {
		character = scope:claimant_child_to_marry
		animation = prayer
	}

	lower_right_portrait = {
		character = scope:ruler_child_to_marry
		animation = anger
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.a
		trigger = {
			root = {
				is_child_of = scope:current_ruler
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.b
		trigger = {
			always = yes
		}
		succession_crisis_process_vassal_loyalty_effect = yes
		scope:current_ruler = {
			if = {
				limit = { this = scope:crisis_heir }
				trigger_event = { id = succession_crisis_heir_event.0006 days = { 3 10 } }
			}
			else = { trigger_event = { id = succession_crisis_claimant_event.0005 days = { 3 10 } } }
			remove_character_flag = debating
		}
		remove_character_flag = debating
		if = {
			limit = { has_active_diarchy = yes }
			diarch = { remove_character_flag = debating }
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.c
		trigger = {
			exists = scope:claimant_to_marry
			exists = scope:ruler_to_marry
			OR = {
				AND = {
					scope:claimant_to_marry = { is_female = yes }
					scope:ruler_to_marry = { is_female = no }
				}
				AND = {
					scope:claimant_to_marry = { is_female = no }
					scope:ruler_to_marry = { is_female = yes }
				}
			}
		}
		scope:ruler_to_marry = {
			trigger_event = { id = succession_crisis_diplomacy_ruler.0009 }
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.d
		trigger = {
			exists = scope:claimant_to_marry
			exists = scope:ruler_child_to_marry
			OR = {
				AND = {
					scope:claimant_to_marry = { is_female = yes }
					scope:ruler_child_to_marry = { is_female = no }
				}
				AND = {
					scope:claimant_to_marry = { is_female = no }
					scope:ruler_child_to_marry = { is_female = yes }
				}
			}
		}
		scope:ruler_to_marry = {
			trigger_event = { id = succession_crisis_diplomacy_ruler.0010 }
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.e
		trigger = {
			exists = scope:claimant_child_to_marry
			exists = scope:ruler_to_marry
			OR = {
				AND = {
					scope:claimant_child_to_marry = { is_female = yes }
					scope:ruler_to_marry = { is_female = no }
				}
				AND = {
					scope:claimant_child_to_marry = { is_female = no }
					scope:ruler_to_marry = { is_female = yes }
				}
			}
		}
		scope:ruler_to_marry = {
			trigger_event = { id = succession_crisis_diplomacy_ruler.0011 }
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0006.f
		trigger = {
			exists = scope:claimant_child_to_marry
			exists = scope:ruler_child_to_marry
			OR = {
				AND = {
					scope:claimant_child_to_marry = { is_female = yes }
					scope:ruler_child_to_marry = { is_female = no }
				}
				AND = {
					scope:claimant_child_to_marry = { is_female = no }
					scope:ruler_child_to_marry = { is_female = yes }
				}
			}
		}
		scope:ruler_to_marry = {
			trigger_event = { id = succession_crisis_diplomacy_ruler.0012 }
		}
	}
}

# ============================================================
# EVENT 0007: DIPLOMACY REJECTION
# ============================================================
succession_crisis_diplomacy_event.0007 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0007.t
	desc = succession_crisis_diplomacy_event.0007.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	left_portrait = {
		character = root
		animation = anger
	}

	right_portrait = {
		character = scope:current_ruler
		animation = dismissal
	}

	option = {
		name = succession_crisis_diplomacy_event.0007.a
		
		ai_chance = {
			base = 60
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = 40
				has_trait = wrathful
			}
			modifier = {
				add = 20
				has_trait = brave
			}
			modifier = {
				add = 20
				has_trait = arrogant
			}
			modifier = {
				add = -50
				has_trait = craven
			}
			modifier = {
				add = -50
				has_trait = content
			}
		}

		succession_crisis_process_vassal_loyalty_effect = yes
		add_character_flag = imminent_war
		
		scope:current_ruler = {
			remove_character_flag = debating
		}
		
		remove_character_flag = debating

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0007.b
		
		ai_chance = {
			base = 40
			modifier = {
				add = 50
				has_trait = content
			}
			modifier = {
				add = 50
				has_trait = craven
			}
			modifier = {
				add = 30
				has_trait = calm
			}
			modifier = {
				add = -40
				has_trait = ambitious
			}
		}

		add_prestige = -500

		scope:current_ruler = {
			remove_character_flag = debating
		}

		remove_character_flag = debating

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0008: DIPLOMACY NOTIFICATION (CHILD DESIGNATED)
# ============================================================
succession_crisis_diplomacy_event.0008 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0008.t
	desc = succession_crisis_diplomacy_event.0008.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}
	}

	left_portrait = {
		character = root
		animation = scepter
	}

	right_portrait = {
		character = scope:current_ruler
		animation = acknowledging
	}

	option = {
		name = succession_crisis_diplomacy_event.0008.a
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
		}

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0009: DIPLOMACY NOTIFICATION (CLAIMANT DESIGNATED)
# ============================================================
succession_crisis_diplomacy_event.0009 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0009.t
	desc = succession_crisis_diplomacy_event.0009.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}
	}

	left_portrait = {
		character = root
		animation = ecstasy
	}

	right_portrait = {
		character = scope:current_ruler
		animation = acknowledging
	}

	option = {
		name = succession_crisis_diplomacy_event.0009.a
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
		}

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0010: RULER MARRY CLAIMANT CHILD
# ============================================================
succession_crisis_diplomacy_event.0010 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0010.t
	desc = succession_crisis_diplomacy_event.0010.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}
	}

	left_portrait = {
		character = root
		animation = ecstasy
	}

	right_portrait = {
		character = scope:current_ruler
		animation = acknowledging
	}

	option = {
		name = succession_crisis_diplomacy_event.0010.a
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
			
		}

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0011: CLAIMANT MARRY RULER CHILD
# ============================================================
succession_crisis_diplomacy_event.0011 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0011.t
	desc = succession_crisis_diplomacy_event.0011.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}
	}

	left_portrait = {
		character = root
		animation = ecstasy
	}

	right_portrait = {
		character = scope:current_ruler
		animation = acknowledging
	}

	option = {
		name = succession_crisis_diplomacy_event.0011.a
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating

		}

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0012: CLAIMANT CHILD MARRY RULER CHILD
# ============================================================
succession_crisis_diplomacy_event.0012 = {
	type = activity_event
	title = succession_crisis_diplomacy_event.0012.t
	desc = succession_crisis_diplomacy_event.0012.desc
	theme = diplomacy
	window = big_event_window
	override_background = ep3_hagia_sophia

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}
	}

	left_portrait = {
		character = root
		animation = ecstasy
	}

	right_portrait = {
		character = scope:current_ruler
		animation = acknowledging
	}

	option = {
		name = succession_crisis_diplomacy_event.0012.a
		remove_character_flag = debating

		scope:current_ruler = {
			remove_character_flag = debating
		}

		if = {
			limit = {
				has_active_diarchy = yes
			}
			diarch = {
				remove_character_flag = debating
			}
		}
	}
}

# ============================================================
# EVENT 0013: CLAIMANT IS LATE (CLAIMANT PERSPECTIVE)
# ============================================================
succession_crisis_diplomacy_event.0013 = {
	type = character_event
	title = succession_crisis_diplomacy_event.0013.t
	desc = succession_crisis_diplomacy_event.0013.desc
	theme = realm

	trigger = {
		NOT = { has_character_flag = active_succession_crisis }
	}

	immediate = {
		# Capture the current ruler
		scope:host = {
			save_scope_as = current_ruler
		}
		
		root = { save_scope_as = warleader }

		if = {
			limit = { root = { has_character_flag = crisis_heir_flag } }
			root = { save_scope_as = crisis_heir }
		}
		else = {
			scope:current_ruler = { save_scope_as = crisis_heir }
		}
		
		# Ensure we have the crisis title in scope
		if = {
			limit = { NOT = { exists = scope:crisis_title } }
			scope:current_ruler = {
				primary_title = {
					save_scope_as = crisis_title
				}
			}
		}
	}

	left_portrait = {
		character = root
		animation = shame
	}

	right_portrait = {
		character = scope:current_ruler
		animation = disapproval
	}

	option = {
		name = succession_crisis_diplomacy_event.0013.a
		ai_chance = {
			base = 10
			modifier = { add = 40 has_trait = craven }
			modifier = { add = 30 has_trait = content }
			modifier = { add = 20 has_trait = lazy }
			modifier = { add = -20 has_trait = brave }
			modifier = { add = -20 has_trait = ambitious }
			modifier = { add = -20 has_trait = arrogant }
		}

		if = {
			limit = { has_character_flag = debate_target }
			remove_character_flag = debate_target
		}

		# Claimant loses prestige
		add_prestige = -150

		# If they have a pressed (strong) claim, it becomes an unpressed (weak) claim
		if = {
			limit = {
				has_claim_on = scope:crisis_title
			}
			remove_claim = scope:crisis_title
			add_unpressed_claim = scope:crisis_title
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0013.b
		ai_chance = {
			base = 40
			modifier = { add = 30 has_trait = brave }
			modifier = { add = 30 has_trait = ambitious }
			modifier = { add = 20 has_trait = wrathful }
			modifier = { add = 20 has_trait = impatient }
			modifier = { add = 20 has_trait = arrogant }
			modifier = { add = -30 has_trait = craven }
			modifier = { add = -30 has_trait = content }
			modifier = { add = -20 has_trait = patient }
		}

		if = {
			limit = { has_character_flag = debate_target }
			remove_character_flag = debate_target
		}

		succession_crisis_process_vassal_loyalty_effect = yes

		if = {
			limit = {
				scope:current_ruler = { has_character_flag = crisis_heir_flag }
			}
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_heir_event.0006
					days = { 14 30 }
				}
			}
		}
		else = {
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_claimant_event.0005
					days = { 14 30 }
				}
			}
		}
	}

	option = {
		name = succession_crisis_diplomacy_event.0013.c
		ai_chance = {
			base = 50
			modifier = { add = 30 has_trait = patient }
			modifier = { add = 20 has_trait = deceitful }
			modifier = { add = 20 has_trait = calm }
			modifier = { add = 10 has_trait = cynical }
			modifier = { add = -20 has_trait = impatient }
			modifier = { add = -20 has_trait = wrathful }
			modifier = { add = -10 has_trait = honest }
		}

		add_character_flag = {
			flag = crisis_war_enabled
			years = 1
		}

		succession_crisis_process_vassal_loyalty_effect = yes

		trigger_event = {
			id = succession_crisis_diplomacy_event.0014
			days = 180 
		}
	}
}
# ============================================================
# EVENT 0014: DIPLOMACY OPTION SELECTION
# ============================================================
succession_crisis_diplomacy_event.0014 = {
	type = character_event
	title = succession_crisis_diplomacy_event.0014.t
	desc = succession_crisis_diplomacy_event.0014.desc
	theme = diplomacy

	trigger = {
		NOT = { has_character_flag = active_succession_crisis }
	}

	immediate = {
		# Check if the invalidated host is a claimant to the crisis title
		if = {
			limit = {
				exists = root.var:invalidated_host
				exists = scope:crisis_title
				root.var:invalidated_host = {
					has_claim_on = scope:crisis_title
				}
			}
			root.var:invalidated_host = {
				save_scope_as = current_ruler
			}
		}

		# Fallback to current holder if invalidated host is not a claimant
		if = {
			limit = {
				NOT = { exists = scope:current_ruler }
				exists = scope:crisis_title.holder
			}
			scope:crisis_title.holder = {
				save_scope_as = current_ruler
			}
		}
		
		root = { 
			save_scope_as = warleader
			save_scope_as = claimant
		}

		if = {
			limit = { root = { has_character_flag = crisis_heir_flag } }
			root = { save_scope_as = crisis_heir }
		}
		else_if = {
			limit = { exists = scope:current_ruler }
			scope:current_ruler = { save_scope_as = crisis_heir }
		}
		
		# Ensure we have the crisis title in scope
		if = {
			limit = { 
				NOT = { exists = scope:crisis_title }
				exists = scope:current_ruler
			}
			scope:current_ruler = {
				primary_title = {
					save_scope_as = crisis_title
				}
			}
		}
	}

	# OPTION B
	option = {
		name = succession_crisis_diplomacy_event.0014.b
		ai_chance = {
			base = 50
			modifier = { add = 30 has_trait = brave }
			modifier = { add = 30 has_trait = ambitious }
			modifier = { add = 20 has_trait = wrathful }
			modifier = { add = 20 has_trait = impatient }
			modifier = { add = 20 has_trait = vengeful }
			modifier = { add = -30 has_trait = craven }
			modifier = { add = -30 has_trait = content }
			modifier = { add = -20 has_trait = patient }
		}

		if = {
			limit = { has_character_flag = debate_target }
			remove_character_flag = debate_target
		}

		succession_crisis_process_vassal_loyalty_effect = yes

		if = {
			limit = {
				exists = scope:current_ruler
				scope:current_ruler = { has_character_flag = crisis_heir_flag }
			}
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_heir_event.0006
					days = { 10 30 }
				}
			}
		}
		else_if = {
			limit = { exists = scope:current_ruler }
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_claimant_event.0005
					days = { 10 30 }
				}
			}
		}
	}

	# OPTION C
	option = {
		name = succession_crisis_diplomacy_event.0014.c
		ai_chance = {
			base = 10
			modifier = { add = 40 has_trait = craven }
			modifier = { add = 30 has_trait = content }
			modifier = { add = 20 has_trait = lazy }
			modifier = { add = -20 has_trait = brave }
			modifier = { add = -20 has_trait = ambitious }
			modifier = { add = -20 has_trait = arrogant }
		}

		if = {
			limit = { has_character_flag = debate_target }
			remove_character_flag = debate_target
		}

		if = {
			limit = {
				exists = scope:crisis_title
				has_claim_on = scope:crisis_title
			}
			remove_claim = scope:crisis_title
		}
		if = {
			limit = { exists = scope:current_ruler }
			scope:current_ruler = {
				trigger_event = {
					id = succession_crisis_misc.0005
					days = 7
				}
			}
		}
	}
}