﻿##############
# Canonizing Saints - decisions
# pam_canonize_saint_decision
# pam_extract_relic_decision
##############
# pam_canonize_saint_decision (character picker) chains into pam_choose_consecration_site_decision (title picker), which commits the canonization on confirm.
##############

# Put a deceased dynast forward for sainthood; the Head of Faith, if any, approves it.
pam_canonize_saint_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_legitimacy.dds" # TODO_PAM_ALI placeholder art - TIT-74202
	}
	decision_group_type = religious

	sort_order = 105

	desc = pam_canonize_saint_decision_desc
	selection_tooltip = pam_canonize_saint_decision_tooltip
	confirm_text = pam_canonize_saint_decision_confirm

	widget = {
		gui = "decision_view_widget_select_character"
		controller = select_scope_object
		show_from_start = yes
		decision_to_second_step_button = "SELECT"

		# Candidates gated by pam_is_canonization_candidate_trigger; widget root = none, so the canonizer is scope:actor.
		setup_items = {
			scope:actor = {
				every_ancestor = {
					even_if_dead = yes
					limit = {
						pam_is_canonization_candidate_trigger = { CANONIZER = scope:actor }
					}
					add_to_list = item_list
				}
			}
		}

		# Pre-select the worthiest forebear deterministically (virtues, age, learning), so the pre-selection is always the best candidate rather than a weighted roll.
		default_item = {
			scope:actor = {
				ordered_ancestor = {
					even_if_dead = yes
					limit = {
						pam_is_canonization_candidate_trigger = { CANONIZER = scope:actor }
					}
					order_by = pam_saint_worthiness_value
					position = 0
					save_scope_as = selected_item
				}
			}
		}

		is_item_valid = {
			pam_is_canonization_candidate_trigger = { CANONIZER = scope:actor }
		}

		ai_select_item = {
			scope:actor = {
				random_ancestor = {
					even_if_dead = yes
					limit = {
						pam_is_canonization_candidate_trigger = { CANONIZER = scope:actor }
					}
					weight = {
						base = 1
						modifier = {
							add = pam_saint_worthiness_value
						}
					}
					save_scope_as = selected_item
				}
			}
		}
	}

	# AI commits via pam_choose_consecration_site_decision
	ai_check_interval = 0

	is_shown = {
		is_ai = no
		# Hide unless an eligible ancestor exists - the picker opens first and would be blank.
		any_ancestor = {
			pam_is_canonization_candidate_trigger = { CANONIZER = root }
			even_if_dead = yes
		}
		pam_can_canonize_saint_trigger = yes
		has_pam_dlc_trigger = yes
	}

	# Capability gate only; the goal-facing Requirements live in is_valid below.
	is_valid_showing_failures_only = {
		is_available_quick = {
			incapable = no
			alive = yes
			adult = yes
			imprisoned = no
		}
		# Step 1 carries no cooldown of its own (it lives on the committing step so backing out is free), so mirror that cooldown here - otherwise this entry, and its alert, would resurface during the post-canonization cooldown.
		NOT = { is_decision_on_cooldown = pam_choose_consecration_site_decision }
	}

	# Goal-facing Requirements, mirrored on pam_choose_consecration_site_decision - keep in sync.
	is_valid = {
		custom_description = {
			text = pam_canonize_saint_decision_req_candidate
			any_ancestor = {
				even_if_dead = yes
				pam_is_canonization_candidate_trigger = { CANONIZER = root }
			}
		}
		pam_hof_thinks_well_of_canonizer_trigger = yes
		# Cathedral faiths need a cathedral; others a holy site (same trigger, different wording).
		trigger_if = {
			limit = { pam_faith_uses_cathedrals_trigger = yes }
			custom_description = {
				text = pam_canonize_saint_decision_req_site
				pam_has_consecration_site_trigger = yes
			}
		}
		trigger_else = {
			custom_description = {
				text = pam_canonize_saint_decision_req_site_holy_site
				pam_has_consecration_site_trigger = yes
			}
		}
	}

	# No cost or cooldown here: both live on the committing decision (pam_choose_consecration_site_decision) so a player who backs out of the site step loses nothing. minimum_cost still blocks opening the picker when the eventual cost is unaffordable.
	minimum_cost = {
		piety = pam_canonize_saint_piety_cost
		treasury_or_gold = pam_canonize_saint_gold_cost
	}

	effect = {
		save_scope_as = canonizer

		scope:selected_item = { save_scope_as = saint_candidate }

		# We store the saint so they survive the jump to the site decision
		set_variable = {
			name = pam_pending_saint
			value = scope:saint_candidate
		}
		# Surface the HoF's estimated approval odds. The odds value is rooted at the saint, which can't see the HoF's cynicism, so we branch on it here and show the matching tooltip.
		if = {
			limit = { pam_needs_hof_approval_for_canonization_trigger = yes }
			# Cynical HoFs see a tempered-odds variant; the Saint/Venerated-Ancestor verb inside each key is swapped by the PamCanonizeVenerateVerb custom loc.
			if = {
				limit = { NOT = { faith.religious_head.puppeteer ?= root } }
				if = {
					limit = { faith.religious_head = { has_trait = cynical } }
					custom_tooltip = pam_canonize_saint_hof_chance_cynical_tt
				}
				else = {
					custom_tooltip = pam_canonize_saint_hof_chance_tt
				}
			}
			else = {
				custom_tooltip = pam_canonize_saint_hof_chance_puppet_tt
			}
		}
		if = {
			limit = { pam_faith_uses_cathedrals_trigger = yes }
			custom_tooltip = pam_canonize_saint_decision_choose_1_tt
		}
		else = {
			custom_tooltip = pam_canonize_saint_decision_choose_1_holy_site_tt
		}
		open_view_data = {
			view = decision_detail
			data = decision:pam_choose_consecration_site_decision
			player = root
		}
	}

	ai_potential = {
		always = no
	}
}

# The committing decision. For players it's step 2, opened from pam_canonize_saint_decision via open_view_data (enact_a_bull_decision chaining pattern) with the saint carried in var:pam_pending_saint. The AI reaches it directly (step 1 is player-only) and picks the saint in the effect. Cost + cooldown live here, so a player who backs out of the picker is never charged.
pam_choose_consecration_site_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_legitimacy.dds" # TODO_PAM_ALI placeholder art - TIT-74202
	}

	# Share the parent title so the panel header stays "Canonize a Saint" across the hop (movement_petition_decision pattern); desc/confirm_text still speak to the site step.
	title = pam_canonize_saint_decision

	decision_group_type = major
	is_invisible = yes
	should_create_alert = { always = no }

	# Invisible to players (reached via open_view_data from step 1), so this cadence and is_shown gate AI discovery only. Mirrors step 1.
	ai_check_interval_by_tier = {
		barony = 0
		county = 60
		duchy = 48
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	is_shown = {
		any_ancestor = {
			pam_is_canonization_candidate_trigger = { CANONIZER = root }
			even_if_dead = yes
		}
		pam_can_canonize_saint_trigger = yes
		has_pam_dlc_trigger = yes
	}

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { pam_faith_uses_cathedrals_trigger = yes }
				desc = pam_choose_consecration_site_decision_desc
			}
			desc = pam_choose_consecration_site_decision_desc_holy_site
		}
	}
	selection_tooltip = pam_choose_consecration_site_decision_tooltip
	confirm_text = pam_choose_consecration_site_decision_confirm

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = pam_choose_consecration_site_decision_select_tt
			exists = scope:selected_item
		}
	}

	# Same Requirements as step 1, shown again at the end of the chain - keep both in sync.
	is_valid = {
		custom_description = {
			text = pam_canonize_saint_decision_req_candidate
			any_ancestor = {
				even_if_dead = yes
				pam_is_canonization_candidate_trigger = { CANONIZER = root }
			}
		}
		pam_hof_thinks_well_of_canonizer_trigger = yes
		trigger_if = {
			limit = { pam_faith_uses_cathedrals_trigger = yes }
			custom_description = {
				text = pam_canonize_saint_decision_req_site
				pam_has_consecration_site_trigger = yes
			}
		}
		trigger_else = {
			custom_description = {
				text = pam_canonize_saint_decision_req_site_holy_site
				pam_has_consecration_site_trigger = yes
			}
		}
	}

	cooldown = { years = 25 }

	# Charged here (not on step 1), so backing out of the picker costs nothing.
	cost = {
		piety = pam_canonize_saint_piety_cost
		treasury_or_gold = pam_canonize_saint_gold_cost
	}

	widget = {
		# Reuses the vanilla realm-county picker GUI (renders any title, fine for baronies).
		gui = "decision_view_widget_select_realm_county"
		controller = select_scope_object
		show_from_start = yes
		decision_to_second_step_button = "SELECT"

		# List the real consecration sites first - cathedral faiths: each cathedral barony in realm, whoever owns it; others: holy-site baronies in realm. Occupied ones stay listed (greyed by is_item_valid) so the player sees which are taken. Once every real site is full, open up county-capital baronies neighbouring one so the dynasty still has somewhere to bury (one saint per province).
		setup_items = {
			scope:actor = {
				if = {
					limit = { pam_faith_uses_cathedrals_trigger = yes }
					if = {
						limit = { pam_character_has_cathedral_trigger = yes }
						domicile ?= { domicile_location = { barony ?= { add_to_list = item_list } } }
					}
					every_realm_province = {
						limit = {
							any_province_domicile = {
								owner = {
									this != scope:actor
									pam_character_has_cathedral_trigger = yes
								}
							}
						}
						barony = { add_to_list = item_list }
					}
				}
				else = {
					every_realm_province = {
						limit = { barony ?= { is_holy_site_of = scope:actor.faith } }
						barony = { add_to_list = item_list }
					}
				}
				if = {
					limit = { NOT = { pam_has_free_primary_site_trigger = yes } }
					every_realm_province = {
						limit = {
							is_county_capital = yes
							NOT = { has_travel_point_of_interest = poi_buried_saint }
							county = {
								any_neighboring_county = {
									pam_county_has_actor_primary_site_trigger = { ACTOR = scope:actor }
								}
							}
						}
						barony = { add_to_list = item_list }
					}
				}
			}
		}

		# Pre-select a free site: a free primary if one exists, otherwise a free adjacent county-capital barony.
		default_item = {
			scope:actor = {
				if = {
					limit = { pam_has_free_primary_site_trigger = yes }
					if = {
						limit = { pam_faith_uses_cathedrals_trigger = yes }
						if = {
							limit = {
								pam_character_has_cathedral_trigger = yes
								domicile ?= { domicile_location = { barony ?= { pam_site_free_trigger = yes } } }
							}
							domicile ?= { domicile_location = { barony ?= { save_scope_as = selected_item } } }
						}
						else = {
							random_realm_province = {
								limit = {
									barony ?= { pam_site_free_trigger = yes }
									any_province_domicile = { owner = { pam_character_has_cathedral_trigger = yes } }
								}
								barony = { save_scope_as = selected_item }
							}
						}
					}
					else = {
						random_realm_province = {
							limit = {
								barony ?= { is_holy_site_of = scope:actor.faith }
								NOT = { has_travel_point_of_interest = poi_buried_saint }
							}
							barony = { save_scope_as = selected_item }
						}
					}
				}
				else = {
					random_realm_province = {
						limit = {
							is_county_capital = yes
							NOT = { has_travel_point_of_interest = poi_buried_saint }
							county = {
								any_neighboring_county = {
									pam_county_has_actor_primary_site_trigger = { ACTOR = scope:actor }
								}
							}
						}
						barony = { save_scope_as = selected_item }
					}
				}
			}
		}

		is_item_valid = {
			tier = tier_barony
			# Occupied sites stay listed but can't be picked - one saint per province.
			custom_tooltip = {
				text = pam_choose_consecration_site_occupied_tt
				pam_site_free_trigger = yes
			}
		}

		# AI mirrors default_item: a free primary if one exists, otherwise a free adjacent county-capital barony.
		ai_select_item = {
			scope:actor = {
				if = {
					limit = { pam_has_free_primary_site_trigger = yes }
					if = {
						limit = { pam_faith_uses_cathedrals_trigger = yes }
						if = {
							limit = {
								pam_character_has_cathedral_trigger = yes
								domicile ?= { domicile_location = { barony ?= { pam_site_free_trigger = yes } } }
							}
							domicile ?= { domicile_location = { barony ?= { save_scope_as = selected_item } } }
						}
						else = {
							random_realm_province = {
								limit = {
									barony ?= { pam_site_free_trigger = yes }
									any_province_domicile = { owner = { pam_character_has_cathedral_trigger = yes } }
								}
								barony = { save_scope_as = selected_item }
							}
						}
					}
					else = {
						random_realm_province = {
							limit = {
								barony ?= { is_holy_site_of = scope:actor.faith }
								NOT = { has_travel_point_of_interest = poi_buried_saint }
							}
							barony = { save_scope_as = selected_item }
						}
					}
				}
				else = {
					random_realm_province = {
						limit = {
							is_county_capital = yes
							NOT = { has_travel_point_of_interest = poi_buried_saint }
							county = {
								any_neighboring_county = {
									pam_county_has_actor_primary_site_trigger = { ACTOR = scope:actor }
								}
							}
						}
						barony = { save_scope_as = selected_item }
					}
				}
			}
		}
	}

	effect = {
		save_scope_as = canonizer
		if = {
			# Player path: the saint was chosen in step 1 and carried over.
			limit = { exists = var:pam_pending_saint }
			var:pam_pending_saint = { save_scope_as = saint_candidate }
		}
		else = {
			# AI path (reaches this decision directly): pick the saint here, mirroring the player picker's worthiness weighting.
			random_ancestor = {
				even_if_dead = yes
				limit = {
					pam_is_canonization_candidate_trigger = { CANONIZER = root }
				}
				weight = {
					base = 1
					modifier = {
						add = pam_saint_worthiness_value
					}
				}
				save_scope_as = saint_candidate
			}
		}
		# The barony's province carries the burial bookkeeping / travel PoI (province-anchored model).
		scope:selected_item = {
			title_province = { save_scope_as = consecration_province }
		}
		remove_variable = pam_pending_saint
		pam_commit_canonization_effect = yes
	}

	ai_potential = {
		piety >= pam_canonize_saint_piety_cost
		gold >= pam_canonize_saint_gold_cost
	}

	ai_will_do = {
		base = 50
		modifier = {
			has_trait = zealous
			add = 50
		}
		modifier = {
			has_trait = cynical
			add = -40
		}
	}
}

# Extract a relic from a saint buried in lands you control. Capped per saint; the AI never takes the final relic.
pam_extract_relic_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_legitimacy.dds" # TODO_PAM_ALI placeholder art - TIT-74203
	}
	decision_group_type = religious

	sort_order = 104

	desc = pam_extract_relic_decision_desc
	selection_tooltip = pam_extract_relic_decision_tooltip
	confirm_text = pam_extract_relic_decision_confirm

	# Pick the buried saint in the character picker. Per-saint data lives on the burial province, so list the sites and surface each one's saint, mapped back in the effect. Widget root = none, so the extractor is scope:actor.
	widget = {
		gui = "decision_view_widget_select_character"
		controller = select_scope_object
		show_from_start = yes
		decision_to_second_step_button = "SELECT"

		setup_items = {
			scope:actor.faith = {
				every_saint = {
					limit = {
						saint_burial_location = {
							pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = scope:actor SAINT = prev }
						}
					}
					add_to_list = item_list
				}
			}
		}

		default_item = {
			scope:actor.faith = {
				random_saint = {
					limit = {
						saint_burial_location = {
							pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = scope:actor SAINT = prev }
						}
					}
					save_scope_as = selected_item
				}
			}
		}

		is_item_valid = {
			# custom_tooltip so the entry tooltip uses this line, not a failed auto-flatten of the helper's any_in_global_list.
			custom_tooltip = {
				text = pam_extract_relic_decision_item_valid_tt
				pam_saint_relic_drawable_trigger = { EXTRACTOR = scope:actor }
			}
		}

		ai_select_item = {
			scope:actor.faith = {
				random_saint = {
					limit = {
						saint_burial_location = {
							pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = scope:actor SAINT = prev }
						}
					}
					save_scope_as = selected_item
				}
			}
		}
	}

	ai_check_interval_by_tier = {
		barony = 0
		county = 48
		duchy = 36
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	is_shown = {
		is_landed = yes
		# Hide unless at least one drawable saint exists (empty picker otherwise).
		faith ?= {
			any_saint = {
				saint_burial_location = {
					pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = root SAINT = prev }
				}
			}
		}
		exists = faith
		is_ruler = yes
		has_pam_dlc_trigger = yes
	}

	# Transient availability only - generic failures on the confirm button.
	is_valid_showing_failures_only = {
		is_available_quick = {
			incapable = no
			alive = yes
			adult = yes
			imprisoned = no
		}
	}

	# Goal-facing requirement (also the execute guard); is_shown gates discovery on the same check. Eligibility owned by pam_can_extract_relic_from_saint_trigger - keep its loc in sync.
	is_valid = {
		custom_description = {
			text = pam_extract_relic_decision_req_saint
			faith = {
				any_saint = {
					saint_burial_location = {
						pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = root SAINT = prev }
					}
				}
			}
		}
		# Once a saint is picked, show how many of the per-saint relic cap they have left.
		trigger_if = {
			limit = { exists = scope:selected_item }
			custom_description = {
				text = pam_extract_relic_decision_req_saint_count
				# subject exposes the picked saint to the loc as CHARACTER (widget's selected_item isn't a loc-visible scope).
				subject = scope:selected_item
				scope:selected_item = { pam_saint_relic_drawable_trigger = { EXTRACTOR = root } }
			}
		}
	}

	cooldown = { years = 2 }

	cost = {
		piety = pam_extract_relic_piety_cost
		gold = pam_extract_relic_gold_cost
	}

	effect = {
		scope:selected_item = {
			save_scope_as = relic_saint
			saint_burial_location = {
				save_scope_as = extract_site
			}
		}

		if = {
			# AI: draw at once, silently (mirrors the canonize AI path).
			limit = { is_ai = yes }
			custom_tooltip = pam_extract_relic_decision_extract_tt
			pam_extract_relic_from_saint_effect = {
				SITE = scope:extract_site
			}
		}
		else = {
			# Fire an event telling the player the work has begun; its option starts the timer that delivers the relic (event 0016). scope:extract_site / relic_saint carry over.
			custom_tooltip = pam_extract_relic_decision_extract_tt
			trigger_event = pam_saint_events.0005
		}
	}

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 40
		modifier = {
			has_trait = zealous
			add = 20
		}
	}
}
