﻿agot_invite_to_spy_network_interaction = {
	category = interaction_category_friendly
	icon = secret
	desc = agot_invite_to_spy_network_interaction_desc
	cooldown_against_recipient = { years = 5 }
	is_shown = {
		NOT = {
			scope:actor = {
				is_target_in_variable_list = {
					name = spy_network_members
					target = scope:recipient
				}
			}
		}
		scope:actor = {
			can_use_spy_network = yes
		}
		scope:recipient = {
			is_courtier = yes
			is_pool_guest = no
			is_landed = no
			court_owner = { is_ruler = yes }
			OR = {
				is_adult = yes
				AND = {
					age >= 8
					scope:actor = {
						house ?= { has_house_modifier = sas_expand_spy_network_house_modifier }
					}
				}
			}
			is_imprisoned = no
			NOT = { has_trait = incapable }
		}
	}

	is_valid = {
		NOT = {
			scope:actor = {
				is_target_in_variable_list = {
					name = spy_network_members
					target = scope:recipient
				}
			}
		}
		scope:actor.highest_held_title_tier > scope:recipient.highest_held_title_tier
		scope:recipient = {
			is_courtier = yes
			is_pool_guest = no
			is_landed = no
			court_owner = { is_ruler = yes }
			OR = {
				is_adult = yes
				AND = {
					age >= 8
					scope:actor = {
						house ?= { has_house_modifier = sas_expand_spy_network_house_modifier }
					}
				}
			}
			is_imprisoned = no
			NOT = { has_trait = incapable }
		}
	}

	on_accept = {
		hidden_effect = {
			scope:actor = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = invite_to_spy_network_toast_title
					left_icon = scope:actor
					right_icon = scope:recipient
					custom_tooltip = invite_to_spy_network_toast_desc
				}
			}
		}
		
		scope:actor = {
			add_prestige = -10
		}
		scope:actor = {
			custom_tooltip = invite_to_spy_network_interaction_target_added_to_network.tt
		}
		scope:recipient = {
			add_prestige = 20
		}
		scope:recipient = {
			custom_tooltip = invite_to_spy_network_interaction_target_becomes_spy.tt
		}
		
		scope:actor = {
			add_to_variable_list = {
				name = spy_network_members
				target = scope:recipient
			}
		}
		if = {
			limit = { always = scope:gift }
			scope:actor = {
				pay_short_term_gold = {
					gold = bribe_value
					target = scope:recipient
				}
				stress_impact = {
					greedy = minor_stress_impact_gain
				}
			}
		}
		if = {
			limit = { always = scope:hook }
			scope:actor = {
				use_hook = scope:recipient
			}
		}
		scope:actor = {
			stress_impact = {
				paranoid = miniscule_stress_impact_loss	# Paranoid characters like having informants
				honest = minor_stress_impact_gain
			}
		}
	}

	ai_accept = {
		base = -60

		# tier modifier
		modifier = {
			scope:actor = {
				has_perk = digging_for_dirt_perk
			}
			add = {
				value = 15
			}
			desc = DIGGING_FOR_DIRT_PERK_EFFECT
		}
		modifier = {
			scope:actor = {
				has_perk = subtle_desire_perk
			}
			scope:recipient = {
				is_attracted_to_gender_of = scope:actor
			}
			add = {
				value = 25
			}
			desc = SUBTLE_DESIRE_PERK_EFFECT
		}
		
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient.liege
					value >= 1
				}
			}
			add = {
				value = 5
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value >= 2
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value >= 3
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value >= 4
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value >= 5
							}
						}
					}
					add = 5
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient.liege
					value <= -1
				}
			}
			add = {
				value = -5
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value <= -2
							}
						}
					}
					add = -5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value <= -3
							}
						}
					}
					add = -5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value <= -4
							}
						}
					}
					add = -5
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:recipient.liege
								value <= -5
							}
						}
					}
					add = -5
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}

		# Previously been discovered
		modifier = {
			add = -100
			scope:recipient = {
				has_character_flag = spy_was_discovered
			}
			desc = PREVIOUSLY_DISCOVERED_REASON
		}

		# Opinion Modifier - numbers chosen so that high opinion alone is never enough to recruit
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
			desc = AI_OPINION_REASON
		}

		opinion_modifier = {
			trigger = {
				NOT = { scope:recipient.liege = scope:actor }
			}
			who = scope:recipient
			opinion_target = scope:recipient.liege
			multiplier = -0.5
			desc = AI_OPINION_REASON
		}

		# Trait modifiers
		modifier = {
			add = 20
			scope:recipient = {
				has_trait = deceitful
			}
			desc = DECEITFUL_REASON
		}
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = honest
			}
			desc = HONEST_REASON
		}
		modifier = {
			add = -30
			scope:recipient = {
				has_trait = shy
			}
			desc = SHY_REASON
		}
		modifier = {
			add = -30
			scope:recipient = {
				has_trait = craven
			}
			desc = CRAVEN_REASON
		}
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = lazy
			}
			desc = LAZY_REASON
		}
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = just
			}
			desc = JUST_REASON
		}
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = trusting
			}
			desc = TRUSTING_REASON
		}
		modifier = {
			add = -10
			scope:recipient = {
				has_trait = paranoid
			}
			desc = PARANOID_REASON
		}
		modifier = {
			add = 10
			scope:recipient = {
				has_trait = drunkard
			}
			desc = DRUNKARD_REASON
		}
		modifier = {
			add = 40
			scope:recipient = {
				has_trait = contrite
			}
			desc = CONTRITE_REASON
		}

		modifier = { # Intimidated (minor dread)
			add = 50
			trigger = {
				scope:recipient = {
					target_is_liege_or_above = scope:actor
					dread_modified_ai_boldness = {
						dreaded_character = scope:actor
						value < -20
					}
					NOT = {
						dread_modified_ai_boldness = {
							dreaded_character = scope:actor
							value < -45
						}
					}
					# Note: value is calculated manually because has_dread_level_towards only works for vassals
				}
			}
			desc = INTIMIDATED_REASON
		}

		modifier = { # Cowed (major dread)
			add = 100
			trigger = {
				scope:recipient = {
					target_is_liege_or_above = scope:actor
					dread_modified_ai_boldness = {
						dreaded_character = scope:actor
						value < -45
					}
					# Note: value is calculated manually because has_dread_level_towards only works for vassals
				}
			}
			desc = COWED_REASON
		}

		# Modifier based on sender's intrigue skill
		compare_modifier = {
			trigger = {
				always = yes
			}
			target = scope:actor
			value = intrigue
			multiplier = 2.0
			offset = 0
			min = 0
			desc = AGOT_RECRUIT_SPY_SKILL_MOD
		}

		compare_modifier = {
			trigger = {
				scope:actor = {
					spy_network_over_limit_recruitment_penalty > 0
				}
			}
			target = scope:actor
			value = spy_network_over_limit_recruitment_penalty
			multiplier = -1.0
			desc = AGOT_RECRUIT_SPY_CAP_MODIFIER
		}

		modifier = {
			add = 50
			scope:hook = yes
			desc = SCHEME_HOOK_USED
		}

		modifier = {
			add = bribe_acceptance_value
			scope:gift = yes
			desc = "SCHEME_WITH_GIFT"
		}
	}

	send_option = {
		is_valid = {
			scope:actor = {
				gold >= bribe_value
			}
		}
		flag = gift
		localization = SPY_RECRUIT_GOLD
		current_description = {
			desc = SPY_RECRUIT_GOLD_VALID
		}
	}

	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = GENERIC_SPEND_A_HOOK
	}

	send_options_exclusive = no
	ai_frequency = 0
}

agot_remove_from_spy_network_interaction = {
	category = interaction_category_friendly
	icon = secret
	desc = agot_remove_from_spy_network_interaction_desc
	use_diplomatic_range = no
	is_shown = {
		scope:actor = {
			is_target_in_variable_list = {
				name = spy_network_members
				target = scope:recipient
			}
			can_use_spy_network = yes
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			add_prestige = -10
		}
		scope:actor = {
			custom_tooltip = dismiss_from_spy_network_interaction_target_remove_from_network.tt
		}
		scope:recipient = {
			custom_tooltip = dismiss_from_spy_network_interaction_target_loses_spy.tt
		}
	}

	on_send = {
		scope:actor = {
			remove_list_variable = {
				name = spy_network_members
				target = scope:recipient
			}
			send_interface_toast = {
				type = event_toast_effect_bad
				title = dismiss_from_spy_network_toast_title
				left_icon = scope:actor
				right_icon = scope:recipient
				custom_tooltip = dismiss_from_spy_network_toast_desc
			}
		}
	}
	ai_frequency = 0
}

agot_check_for_potential_spies_interaction = {
	category = interaction_category_hostile
	icon = secret
	desc = agot_check_for_potential_spies_interaction_desc
	cooldown = { days = 30 }
	common_interaction = yes

	is_shown = {
		scope:actor = {
			can_use_spy_network = yes
		}
	}

	is_valid = {
		scope:recipient = {
			custom_tooltip = {
				text = potential_spy_in_court
				any_courtier = {
					is_ai = yes
					OR = {
						is_adult = yes
						AND = {
							age >= 8
							scope:actor = {
								house ?= { has_house_modifier = sas_expand_spy_network_house_modifier }
							}
						}
					}
					is_imprisoned = no
					is_courtier = yes
					is_pool_guest = no
					is_landed = no
					court_owner = { is_ruler = yes }
					NOT = { has_trait = incapable }
					scope:actor = {
						is_character_interaction_potentially_accepted = {
							recipient = PREV
							interaction = agot_invite_to_spy_network_interaction
						}
					}
				}
			}
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			custom_tooltip = check_for_potential_spies_actor.tt
		}
	}

	on_send = {
		scope:recipient = {
			random_courtier = {
				limit = {
					is_ai = yes
					OR = {
						is_adult = yes
						AND = {
							age >= 8
							scope:actor = {
								house ?= { has_house_modifier = sas_expand_spy_network_house_modifier }
							}
						}
					}
					is_imprisoned = no
					is_courtier = yes
					is_pool_guest = no
					is_landed = no
					court_owner = { is_ruler = yes }
					NOT = { has_trait = incapable }
					scope:actor = {
						is_character_interaction_potentially_accepted = {
							recipient = PREV
							interaction = agot_invite_to_spy_network_interaction
						}
					}
				}
				weight = {
					base = 1
					modifier = {
						add = {
							value = 0
							add = intrigue
						}
					}
				}
				save_scope_as = potential_new_spy
				scope:actor = {
					trigger_event = {
						id = agot_spy_network.0800
						days = { 7 14 }
					}
				}
			}
		}
	}
	ai_frequency = 0
}