﻿# Dedicated effect for the Heir winning the crisis war
succession_crisis_war_victory_transfer_effect = {
	# 1. Initialize the change
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = crisis_change
		add_claim_on_loss = yes
	}

	# 2. Identify the core components
	scope:crisis_destination = {
		county = {
			save_scope_as = new_capital_location
			kingdom = {
				save_scope_as = crisis_capital_kingdom
			}
		}
	}

	# Add the Empire/Primary Title
	scope:crisis_title = {
		add_to_list = titles_to_transfer
		scope:defender = {
			every_vassal = {
				limit = {
					this != scope:attacker
				}
				add_to_list = vassals_to_transfer
			}
		}
	}

	# Add the Capital County and its de jure Kingdom
	# This ensures the winner isn't landless and owns the "home" kingdom
	scope:crisis_capital_kingdom = {
		add_to_list = titles_to_transfer
		scope:defender = {
			every_vassal = {
				limit = {
					this != scope:attacker
				}
				add_to_list = vassals_to_transfer
			}
		}
	}
	
	scope:new_capital_location = {
		add_to_list = titles_to_transfer
	}

	# 4. Execute Title Transfers
	every_in_list = {
		list = titles_to_transfer
		change_title_holder = {
			holder = scope:attacker
			change = scope:crisis_change
		}
	}

	# 5. Execute Vassal Transfers
	every_in_list = {
		list = vassals_to_transfer
		change_liege = {
			liege = scope:attacker
			change = scope:crisis_change
		}
	}

	# 6. Resolve and Finalize
	resolve_title_and_vassal_change = scope:crisis_change

	# Move the Winner's Capital
	if = {
		limit = {
			scope:crisis_title.tier >= scope:attacker.primary_title.tier
			exists = scope:new_capital_location
			scope:new_capital_location.holder = scope:attacker
		}
		scope:attacker = {
			set_realm_capital = scope:new_capital_location
		}
	}
}