# Primary-culture *set* match (order irrelevant).
uw_exact_same_primary_cultures = {
	custom_tooltip = {
		text = uw_exact_same_primary_cultures_tt
		NOT = {
			any_primary_culture = {
				NOT = { is_primary_culture_of = scope:target_country }
			}
		}
		NOT = {
			scope:target_country = {
				any_primary_culture = {
					NOT = { is_primary_culture_of = root }
				}
			}
		}
	}
}

uw_share_any_primary_culture = {
	custom_tooltip = {
		text = uw_share_any_primary_culture_tt
		any_primary_culture = {
			is_primary_culture_of = scope:target_country
		}
	}
}

# Same heritage trait (e.g. heritage_han, heritage_east_slavic) — not tradition.
uw_share_heritage_trait = {
	custom_tooltip = {
		text = uw_share_heritage_trait_tt
		any_primary_culture = {
			shares_heritage_trait_with_any_primary_culture = scope:target_country
		}
	}
}

uw_share_heritage_trait_group = {
	custom_tooltip = {
		text = uw_share_heritage_trait_group_tt
		any_primary_culture = {
			shares_heritage_trait_group_with_any_primary_culture = scope:target_country
		}
	}
}

# Culture gate driven by game rule (strict → loose).
uw_culture_requirement_met = {
	trigger_if = {
		limit = { has_game_rule = uw_culture_exact }
		uw_exact_same_primary_cultures = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_culture_share_primary }
		uw_share_any_primary_culture = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_culture_share_heritage }
		uw_share_heritage_trait = yes
	}
	trigger_else = {
		# uw_culture_share_heritage_group (broadest)
		uw_share_heritage_trait_group = yes
	}
}

# Relations gate driven by game rule.
uw_relations_requirement_met = {
	trigger_if = {
		limit = { has_game_rule = uw_relations_none }
		always = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_relations_80 }
		scope:target_country = { "relations(root)" < 80 }
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_relations_50 }
		scope:target_country = { "relations(root)" < 50 }
	}
	trigger_else = {
		# uw_relations_20 (default)
		scope:target_country = { "relations(root)" < 20 }
	}
}

# Direct *land* border only (province-neighbour states).
# Do NOT use is_adjacent_to_country — that includes one-sea-node adjacency
# (e.g. Prussia ↔ Lübeck across the Baltic) and subject-chain adjacency.
uw_has_direct_land_border = {
	custom_tooltip = {
		text = uw_has_direct_land_border_tt
		any_scope_state = {
			any_neighbouring_state = {
				owner = scope:target_country
			}
		}
	}
}

# Structural eligibility of the target. Call from actor with scope:target_country set.
uw_union_war_basic_target = {
	scope:target_country = {
		NOT = { this = root }
		NOT = { is_country_type = decentralized }
		is_subject = no
		is_revolutionary = no
		is_secessionist = no
		aggressive_diplomatic_plays_permitted = yes
		NOT = { has_war_with = root }
		NOT = { is_diplomatic_play_enemy_of = root }
		NOT = { has_truce_with = root }
	}
	uw_has_direct_land_border = yes
	uw_relations_requirement_met = yes
}

uw_union_war_valid_target = {
	uw_union_war_basic_target = yes
	uw_culture_requirement_met = yes
}

# AI army PP gate driven by game rule (default 3x).
uw_ai_army_power_sufficient = {
	trigger_if = {
		limit = { has_game_rule = uw_ai_army_ignore }
		always = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_ai_army_10x }
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 10
		}
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_ai_army_5x }
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 5
		}
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_ai_army_2x }
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 2
		}
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_ai_army_1x }
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 1
		}
	}
	trigger_else_if = {
		limit = { has_game_rule = uw_ai_army_half }
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 0.5
		}
	}
	trigger_else = {
		# uw_ai_army_3x (default)
		army_power_projection >= {
			value = scope:target_country.army_power_projection
			multiply = 3
		}
	}
}

uw_ai_should_union_war = {
	uw_ai_army_power_sufficient = yes
}

# AI must not launch if estimated post-war infamy would reach/exceed 100.
uw_ai_infamy_safe = {
	OR = {
		has_game_rule = uw_infamy_none
		NOT = {
			infamy >= {
				value = 100
				subtract = uw_estimated_union_war_infamy
			}
		}
	}
}

# Someone else (not us / our subjects) holds a primary-culture homeland state.
uw_has_unowned_primary_culture_homeland = {
	any_country = {
		NOT = { this = root }
		NOT = { is_subject_of = root }
		any_scope_state = {
			is_homeland_of_country_cultures = root
		}
	}
}

# Direct land neighbour holds a primary-culture homeland state.
uw_has_adjacent_unowned_primary_culture_homeland = {
	any_scope_state = {
		any_neighbouring_state = {
			is_homeland_of_country_cultures = root
			NOR = {
				owner = root
				owner = { is_subject_of = root }
			}
		}
	}
}

# Shared possible block for all Union War diplomatic plays.
uw_dp_union_war_common_possible = {
	NOT = { is_country_type = decentralized }
	is_subject = no
	aggressive_diplomatic_plays_permitted = yes
	scope:target_country = {
		NOT = { is_country_type = decentralized }
		is_subject = no
		aggressive_diplomatic_plays_permitted = yes
	}
}
