﻿sas_ennoblement_interaction = {
	icon = sas_ennoblement
	category = interaction_category_diplomacy
 	common_interaction = yes
	interface_priority = 150
	desc = sas_ennoblement_interaction_desc

 	cooldown = { years = 5 }

	is_shown = {
		OR = {
			has_game_rule = sas_all_modified_perks
			has_game_rule = spy_and_interactions
		}
		scope:recipient != scope:actor

		scope:actor = {
			is_landed = yes
			has_perk = accomplished_forger_perk
		}

		scope:recipient = {
			is_lowborn = yes
			is_courtier_of = scope:actor
			NOT = { has_trait = devoted }
			NOT = { has_trait = order_member }
			NOT = { #Not your bishop
				scope:actor = {
					cp:councillor_court_chaplain = scope:recipient
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			gold >= medium_gold_value
			prestige >= medium_prestige_value
		}
		custom_description = {
			text = recently_ennobled
			scope:actor = {
				NOT = { exists = var:recently_ennobled }
			}
		}
	}

	cost = {
		prestige = medium_prestige_value
		gold = medium_gold_value
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			if = {
				limit = {
					scope:recipient.ai_honor >= 75
					can_add_hook = {
						target = scope:recipient
						type = loyalty_hook
					}
				}
				add_hook = {
					type = loyalty_hook
					target = scope:recipient
				}
			}
			else_if = {
				limit = {
					can_add_hook = {
						target = scope:recipient
						type = favor_hook
					}
				}
				add_hook = {
					type = favor_hook
					target = scope:recipient
				}
			}
		}

		scope:recipient = {
			custom_tooltip = sas_ennoblement_tt

			add_opinion = {
				target = scope:actor
				opinion = 50
				modifier = sas_ennoblement_opinion
			}
			create_character_memory = {
				type = sas_ennoblement_memory
			}
			if = {
				limit = {
					NOT = {
						has_trait = loyal
					}
				}
				add_trait = loyal
			}
			if = {
				limit = {
					has_trait = disloyal
				}
				remove_trait = disloyal
			}
		}

		hidden_effect = {
			scope:recipient = {
				create_dynamic_title = {
					tier = duchy
					name = sas_ennoblement_title_name
				}
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:new_title = {
					set_capital_county = scope:actor.capital_county
					set_landless_title = yes
					set_destroy_on_gain_same_tier = yes
					set_no_automatic_claims = yes
					set_can_be_named_after_dynasty = no
					change_title_holder = {
						holder = prev
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				scope:new_title = {
					generate_coa = yes
				}
				destroy_title = scope:new_title

			}
			scope:actor = {
				add_courtier = scope:recipient
			}
		}
	}

	is_available = {
		has_perk = accomplished_forger_perk
	}
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_targets = {
		ai_recipients = spouses
		ai_recipients = scripted_relations
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 120
		kingdom = 120
		empire = 120
		hegemony = 120
	}

	ai_will_do = {
		base = -1000

		modifier = {
			add = 1100
			scope:recipient = {
				OR = {
					has_secret_relation_lover = scope:actor
					has_relation_lover = scope:actor
					has_relation_soulmate = scope:actor
					has_relation_friend = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
		}
	}
}
