############################################
# Lord Ruler Event Chain
# Tracks the Lord Ruler's attention on powerful nobles
# and the consequences of drawing too much notice
############################################

namespace = mistborn_lord_ruler

# ============================================================
# TIER 1 - OBLIGATOR WARNING
# An Obligator arrives to investigate your growing influence
# ============================================================
mistborn_lord_ruler.0001 = {
	type = character_event
	title = mistborn_lord_ruler.0001.t
	desc = mistborn_lord_ruler.0001.desc
	theme = intrigue

	left_portrait = {
		character = root
		animation = worry
	}

	trigger = {
		is_ruler = yes
		has_variable = lord_ruler_attention
		var:lord_ruler_attention >= 25
		var:lord_ruler_attention < 60
		NOT = { has_character_flag = recent_obligator_warning }
	}

	immediate = {
		add_character_flag = {
			flag = recent_obligator_warning
			years = 3
		}
	}

	# Option A: Submit - Pay tribute and grovel
	option = {
		name = mistborn_lord_ruler.0001.a
		add_gold = -200
		add_prestige = -100
		change_variable = {
			name = lord_ruler_attention
			add = -15
		}
		stress_impact = {
			ambitious = 15
			arrogant = 20
		}
		ai_chance = {
			base = 60
			modifier = {
				add = 20
				has_trait = craven
			}
			modifier = {
				add = -20
				has_trait = ambitious
			}
		}
	}

	# Option B: Diplomatically deflect
	option = {
		name = mistborn_lord_ruler.0001.b
		trigger = {
			diplomacy >= 12
		}
		change_variable = {
			name = lord_ruler_attention
			add = -8
		}
		add_prestige = -25
		ai_chance = {
			base = 30
			modifier = {
				add = 20
				has_trait = deceitful
			}
		}
	}

	# Option C: Refuse - Defy the Obligator
	option = {
		name = mistborn_lord_ruler.0001.c
		add_prestige = 50
		change_variable = {
			name = lord_ruler_attention
			add = 20
		}
		add_stress = 30
		stress_impact = {
			brave = -15
			craven = 30
		}
		ai_chance = {
			base = 10
			modifier = {
				add = 30
				has_trait = ambitious
			}
			modifier = {
				add = 20
				has_trait = brave
			}
		}
	}
}

# ============================================================
# TIER 2 - INQUISITION VISIT
# Steel Inquisitors come to your keep
# ============================================================
mistborn_lord_ruler.0010 = {
	type = character_event
	title = mistborn_lord_ruler.0010.t
	desc = mistborn_lord_ruler.0010.desc
	theme = skulduggery

	left_portrait = {
		character = root
		animation = fear
	}

	trigger = {
		is_ruler = yes
		has_variable = lord_ruler_attention
		var:lord_ruler_attention >= 60
		var:lord_ruler_attention < 90
		NOT = { has_character_flag = recent_inquisition_visit }
	}

	immediate = {
		add_character_flag = {
			flag = recent_inquisition_visit
			years = 5
		}
		# Select a random courtier or family member to be executed
		random_courtier = {
			limit = {
				is_alive = yes
				NOT = { this = root }
			}
			weight = {
				base = 1
				modifier = {
					add = 5
					has_trait = trait_mistborn
				}
				modifier = {
					add = 3
					has_trait = trait_allomantic_potential
				}
				modifier = {
					add = 2
					is_close_family_of = root
				}
			}
			save_scope_as = inquisition_victim
		}
	}

	# Option A: Full submission - Sacrifice everything to survive
	option = {
		name = mistborn_lord_ruler.0010.a
		if = {
			limit = {
				exists = scope:inquisition_victim
			}
			scope:inquisition_victim = {
				death = {
					death_reason = death_execution
				}
			}
		}
		add_gold = -500
		add_prestige = -300
		# Lose a title if holding multiple
		random_held_title = {
			limit = {
				tier = tier_county
				NOT = { this = root.capital_county }
			}
			save_scope_as = lost_title
		}
		if = {
			limit = {
				exists = scope:lost_title
			}
			scope:lost_title = {
				set_de_jure_liege_title = root.capital_county.de_jure_liege
			}
		}
		set_variable = {
			name = lord_ruler_attention
			value = 20
		}
		stress_impact = {
			ambitious = 30
			brave = 25
		}
		ai_chance = {
			base = 50
			modifier = {
				add = 30
				has_trait = craven
			}
		}
	}

	# Option B: Plead innocence
	option = {
		name = mistborn_lord_ruler.0010.b
		trigger = {
			diplomacy >= 16
		}
		# Diplomacy check
		duel = {
			skill = diplomacy
			value = 16
			target = root
			50 = {
				desc = mistborn_lord_ruler.0010.b.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 2.0
				}
				send_interface_toast = {
					title = mistborn_lord_ruler.0010.b.success
				}
				change_variable = {
					name = lord_ruler_attention
					add = -20
				}
				add_prestige = -50
			}
			50 = {
				desc = mistborn_lord_ruler.0010.b.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -2.0
				}
				send_interface_toast = {
					title = mistborn_lord_ruler.0010.b.failure
				}
				if = {
					limit = {
						exists = scope:inquisition_victim
					}
					scope:inquisition_victim = {
						death = {
							death_reason = death_execution
						}
					}
				}
				add_gold = -300
				add_prestige = -200
				change_variable = {
					name = lord_ruler_attention
					add = 10
				}
			}
		}
		ai_chance = {
			base = 30
			modifier = {
				add = 20
				has_trait = deceitful
			}
		}
	}

	# Option C: Resist - near-suicidal
	option = {
		name = mistborn_lord_ruler.0010.c
		add_prestige = 200
		change_variable = {
			name = lord_ruler_attention
			add = 30
		}
		# Combat against Steel Inquisitors - devastating
		random_list = {
			20 = {
				# Miraculous survival
				add_trait = wounded_3
				custom_tooltip = mistborn_lord_ruler.0010.c.survive
			}
			50 = {
				# Heavy casualties
				add_trait = wounded_3
				if = {
					limit = {
						exists = scope:inquisition_victim
					}
					scope:inquisition_victim = {
						death = {
							death_reason = death_murder
						}
					}
				}
				# Kill additional courtiers
				random_courtier = {
					limit = {
						is_alive = yes
						NOT = { this = root }
						NOT = { this = scope:inquisition_victim }
					}
					death = {
						death_reason = death_murder
					}
				}
			}
			30 = {
				# Death
				death = {
					death_reason = death_murder
				}
			}
		}
		ai_chance = {
			base = 5
			modifier = {
				add = 15
				has_trait = brave
			}
			modifier = {
				add = 10
				has_trait = ambitious
			}
		}
	}
}

# ============================================================
# TIER 3 - THE LORD RULER'S WRATH
# The Lord Ruler himself acts - total devastation
# ============================================================
mistborn_lord_ruler.0020 = {
	type = character_event
	title = mistborn_lord_ruler.0020.t
	desc = mistborn_lord_ruler.0020.desc
	theme = death

	left_portrait = {
		character = root
		animation = fear
	}

	trigger = {
		is_ruler = yes
		has_variable = lord_ruler_attention
		var:lord_ruler_attention >= 90
		NOT = { has_character_flag = lord_ruler_wrath_survived }
	}

	immediate = {
		add_character_flag = {
			flag = lord_ruler_wrath_survived
			years = 20
		}
	}

	# The only option: survive - barely
	option = {
		name = mistborn_lord_ruler.0020.a

		# Kill multiple family members
		every_close_family_member = {
			limit = {
				is_alive = yes
				NOT = { this = root }
			}
			random = {
				chance = 60
				death = {
					death_reason = death_murder
				}
			}
		}

		# Kill random courtiers
		every_courtier = {
			limit = {
				is_alive = yes
				NOT = { this = root }
				NOT = { is_close_family_of = root }
			}
			random = {
				chance = 40
				death = {
					death_reason = death_murder
				}
			}
		}

		# Lose all titles except capital
		every_held_title = {
			limit = {
				NOT = { this = root.capital_county }
				tier >= tier_county
			}
			root = {
				# Titles are stripped
			}
		}

		# Devastate holdings
		capital_county = {
			add_county_modifier = {
				modifier = koloss_rampage
				years = 10
			}
		}

		add_gold = -1000
		add_prestige = -500
		add_trait = wounded_3
		add_stress = 100

		# Reset attention - you are no longer a threat
		set_variable = {
			name = lord_ruler_attention
			value = 0
		}

		# Small chance of total dynasty death
		random = {
			chance = 15
			death = {
				death_reason = death_murder
			}
		}

		ai_chance = {
			base = 100
		}
	}
}

# ============================================================
# ATTENTION DECAY EVENT
# Lord Ruler attention naturally decreases over time
# ============================================================
mistborn_lord_ruler.0030 = {
	type = character_event
	title = mistborn_lord_ruler.0030.t
	desc = mistborn_lord_ruler.0030.desc
	theme = stewardship
	hidden = yes

	trigger = {
		is_ruler = yes
		has_variable = lord_ruler_attention
		var:lord_ruler_attention > 0
	}

	immediate = {
		# Base decay
		change_variable = {
			name = lord_ruler_attention
			add = -3
		}

		# Extra decay for loyal behavior
		if = {
			limit = {
				faith = faith:steel_ministry
				NOT = { has_trait = ambitious }
			}
			change_variable = {
				name = lord_ruler_attention
				add = -2
			}
		}

		# Ensure attention doesn't go below 0
		if = {
			limit = {
				var:lord_ruler_attention < 0
			}
			set_variable = {
				name = lord_ruler_attention
				value = 0
			}
		}
	}

	option = {
		name = mistborn_lord_ruler.0030.a
	}
}

# ============================================================
# ATTENTION GAIN EVENT
# Hidden event that tracks attention-increasing behaviors
# ============================================================
mistborn_lord_ruler.0040 = {
	type = character_event
	title = mistborn_lord_ruler.0040.t
	desc = mistborn_lord_ruler.0040.desc
	theme = intrigue
	hidden = yes

	trigger = {
		is_ruler = yes
	}

	immediate = {
		# Initialize variable if not set
		if = {
			limit = {
				NOT = { has_variable = lord_ruler_attention }
			}
			set_variable = {
				name = lord_ruler_attention
				value = 0
			}
		}

		# Holding 3+ kingdom titles
		if = {
			limit = {
				any_held_title = {
					count >= 3
					tier = tier_kingdom
				}
			}
			change_variable = {
				name = lord_ruler_attention
				add = 10
			}
		}

		# Having a Mistborn in court
		if = {
			limit = {
				any_courtier = {
					has_trait = trait_mistborn
				}
			}
			change_variable = {
				name = lord_ruler_attention
				add = 5
			}
		}

		# Practicing non-Steel Ministry religion openly
		if = {
			limit = {
				NOT = { faith = faith:steel_ministry }
			}
			change_variable = {
				name = lord_ruler_attention
				add = 20
			}
		}

		# Being a Compounder
		if = {
			limit = {
				has_character_modifier = compounder_modifier
			}
			change_variable = {
				name = lord_ruler_attention
				add = 30
			}
		}

		# Cap attention at 100
		if = {
			limit = {
				var:lord_ruler_attention > 100
			}
			set_variable = {
				name = lord_ruler_attention
				value = 100
			}
		}
	}

	option = {
		name = mistborn_lord_ruler.0040.a
	}
}
