﻿# Attach a legacy seal only when this ruler owns exactly one artifact matching
# the original type, rarity, and all three starting modifiers.
aureliani_seal_find_legacy_effect = {
	if = {
		limit = {
		house = { NOT = { exists = var:aureliani_seal_artifact } }
			any_character_artifact = {
				count = 1
				artifact_type = miscellaneous
				rarity = famed
				has_artifact_modifier = artifact_monthly_learning_lifestyle_xp_3_modifier
				has_artifact_modifier = artifact_monthly_dynasty_prestige_4_modifier
				has_artifact_modifier = artifact_general_opinion_1_modifier
			}
		}
		random_character_artifact = {
			limit = {
				artifact_type = miscellaneous
				rarity = famed
				has_artifact_modifier = artifact_monthly_learning_lifestyle_xp_3_modifier
				has_artifact_modifier = artifact_monthly_dynasty_prestige_4_modifier
				has_artifact_modifier = artifact_general_opinion_1_modifier
			}
			save_scope_as = aureliani_legacy_seal_candidate
		}
		scope:aureliani_legacy_seal_candidate = {
			set_variable = { name = aureliani_dynastic_seal value = 1 }
		}
		house = {
			set_variable = { name = aureliani_seal_artifact value = scope:aureliani_legacy_seal_candidate }
		}
	}
}

# Mutate the original artifact only. Missing references or destroyed artifacts
# are left alone, and stage markers make repeat calls safe.
aureliani_seal_apply_tuscany_effect = {
	if = {
		limit = { house = { exists = var:aureliani_seal_artifact } }
		house.var:aureliani_seal_artifact = {
			if = {
				limit = { has_variable = aureliani_dynastic_seal NOT = { has_variable = aureliani_seal_tuscany_masterwork } }
				if = {
					limit = { NOR = { rarity = famed rarity = illustrious } }
					set_artifact_rarity = masterwork
				}
				set_artifact_name = aureliani_seal_tuscany
				set_artifact_description = aureliani_seal_tuscany_desc
				add_artifact_modifier = artifact_stewardship_1_modifier
				set_variable = { name = aureliani_seal_tuscany_masterwork value = 1 }
			}
		}
	}
}

aureliani_seal_apply_italy_effect = {
	if = {
		limit = { house = { exists = var:aureliani_seal_artifact } }
		house.var:aureliani_seal_artifact = {
			if = {
				limit = { has_variable = aureliani_dynastic_seal NOT = { has_variable = aureliani_seal_italian_famed } }
				if = {
					limit = { NOT = { rarity = illustrious } }
					set_artifact_rarity = famed
				}
				set_artifact_name = aureliani_seal_italy
				set_artifact_description = aureliani_seal_italy_desc
				add_artifact_modifier = artifact_monthly_prestige_1_modifier
				set_variable = { name = aureliani_seal_italian_famed value = 1 }
			}
		}
	}
}

aureliani_seal_apply_rome_effect = {
	if = {
		limit = { house = { exists = var:aureliani_seal_artifact } }
		house.var:aureliani_seal_artifact = {
			if = {
				limit = { has_variable = aureliani_dynastic_seal NOT = { has_variable = aureliani_seal_roman_illustrious } }
				set_artifact_rarity = illustrious
				set_artifact_name = aureliani_seal_rome
				set_artifact_description = aureliani_seal_rome_desc
				add_artifact_modifier = artifact_diplomacy_1_modifier
				set_variable = { name = aureliani_seal_roman_illustrious value = 1 }
			}
		}
	}
}

# Public sync can be called from milestones or a low-frequency Senate entry
# point. Legacy adoption is attempted once per call only when the exact match
# is unique in the current ruler's own artifact collection.
aureliani_seal_sync_effect = {
	aureliani_seal_find_legacy_effect = yes
	if = { limit = { house = { has_variable = aureliani_tuscany_secured } } aureliani_seal_apply_tuscany_effect = yes }
	if = { limit = { house = { has_variable = aureliani_italian_crown_secured } } aureliani_seal_apply_italy_effect = yes }
	if = { limit = { house = { has_variable = aureliani_roman_goal_completed } } aureliani_seal_apply_rome_effect = yes }
}
