# ==================================================================
# ADD WARGOAL DURING WAR — SCRIPTED GUIs
#
# Scope: country (player country)
# Context variable on player: adding_wargoal_target_country (country)
#   Set when the player clicks "Add Wargoal" on an enemy from war_panel.
#   Cleared when a wargoal is added, or when the player hits Back.
#
# Cooldown: war_goal_add_cooldown variable (6 months, existing behaviour)
#
# These SGUIs are invoked from war_panel.gui buttons. Each add_X SGUI
# mirrors the accept_effect of the corresponding diplomatic action in
# add_wargoal.txt (now deprecated in favour of this GUI flow).
# ==================================================================
 
# ── OPEN / CLOSE THE ADD-WARGOAL SUB-PANEL ──────────────────────────────
 
awg_open_panel = {
	scope = country
	is_shown = { always = yes }
 
	# Only usable while at war, not going well enough, and not on cooldown.
	is_valid = {
		any_scope_war = {
			any_war_participant = { THIS = ROOT }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			any_enemy_in_war = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		
	}
 
	effect = {
		set_variable = { name = awg_panel_open }
	}
 
	ai_is_valid = { always = no }
}
 
awg_close_panel = {
	scope = country
	is_shown = { always = yes }
 
	effect = {
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
		remove_variable = awg_state_mode
	}
 
	ai_is_valid = { always = no }
}
 
awg_is_panel_open = {
	scope = country
	is_shown = {
		has_variable = awg_panel_open
	}
}
 
# ── SELECT ENEMY COUNTRY ─────────────────────────────────────────────────
 
awg_select_enemy = {
	scope = country
	saved_scopes = { target_country }
 
	is_shown = { always = yes }
 
	is_valid = {
		scope:target_country = {
			is_secessionist = no
			is_revolutionary = no
			enemy_occupation > 0.15
		}
		ROOT = {
			NOT = { has_variable = war_goal_add_cooldown }
		}
	}
 
	effect = {
		set_variable = { name = awg_selected_enemy value = scope:target_country }
	}
 
	ai_is_valid = { always = no }
}
 
awg_clear_selected_enemy = {
	scope = country
	is_shown = { always = yes }
 
	effect = {
		remove_variable = awg_selected_enemy
		remove_variable = awg_state_mode
	}
 
	ai_is_valid = { always = no }
}
 
awg_has_selected_enemy = {
	scope = country
	is_shown = {
		has_variable = awg_selected_enemy
	}
}
 
# ── SHARED AVAILABILITY CHECK ────────────────────────────────────────────
# All add_X SGUIs below share this gating logic. We factor it into a helper.
 
awg_can_add = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = {
				enemy_occupation > 0.15
			}
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		is_subject = no
	}
}
 
# ── ADD-WARGOAL SETTERS ──────────────────────────────────────────────────
# Each sets the 6-month cooldown and calls add_war_goal on the shared
# random_diplomatic_play scope the war belongs to.
 
# ─ CONQUER STATE ─
# Requires a state to be selected beforehand via awg_select_state.
awg_add_conquest = {
	scope = country
	saved_scopes = { target_state }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		exists = scope:target_state
		scope:target_state = {
			owner = ROOT.var:awg_selected_enemy
			is_under_colonization = no
			NOT = { is_target_of_wargoal = ROOT.var:awg_selected_enemy }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = conquer_state
				target_state = scope:target_state
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 1 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 1 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ RETURN STATE ─
awg_add_return_state = {
	scope = country
	saved_scopes = { target_state }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		exists = scope:target_state
		scope:target_state = {
			owner = ROOT.var:awg_selected_enemy
			is_under_colonization = no
			NOT = { is_target_of_wargoal = ROOT.var:awg_selected_enemy }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = return_state
				target_state = scope:target_state
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 2 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 2 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ TREATY PORT ─
awg_add_treaty_port = {
	scope = country
	saved_scopes = { target_state }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		exists = scope:target_state
		scope:target_state = {
			owner = ROOT.var:awg_selected_enemy
			is_under_colonization = no
			NOT = { is_target_of_wargoal = ROOT.var:awg_selected_enemy }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = take_treaty_port
				target_state = scope:target_state
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 3 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 3 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ BAN SLAVERY ─ (no state target needed)
awg_add_ban_slavery = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = ban_slavery
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = ban_slavery
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 11 max = 12 min = 5 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 4 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 4 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ REGIME CHANGE ─ (iraqi_freedom)
awg_add_regime_change = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = regime_change
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = regime_change
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 16 max = 14 min = 6 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 5 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 5 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ CONTAINMENT ─ (contain_threat)
awg_add_containment = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = contain_threat
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = contain_threat
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 6 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 6 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ INVESTMENT RIGHTS ─ (standard_oil → foreign_investment_rights)
awg_add_investment_rights = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = foreign_investment_rights
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = foreign_investment_rights
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 16 max = 12 min = 4 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 7 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 7 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ HUMILIATION ─
awg_add_humiliation = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = humiliation
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = humiliation
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 24 max = 18 min = 4 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 8 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 8 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ DOMINION ─ (make_dominion)
awg_add_dominion = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		var:awg_selected_enemy = {
			is_subject = no
		}
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = make_dominion
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = make_dominion
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 40 max = 46 min = 32 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 9 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 9 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ PROTECTORATE ─ (make_protectorate)
awg_add_protectorate = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		var:awg_selected_enemy = {
			is_subject = no
		}
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = make_protectorate
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = make_protectorate
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 42 max = 45 min = 36 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 10 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 10 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ WAR REPARATIONS ─
awg_add_war_reps = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = war_reparations
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = war_reparations
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 8 max = 10 min = 3 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 11 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 11 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ OPEN MARKET ─
awg_add_open_market = {
	scope = country
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		custom_tooltip = {
			text = "AWG_ALREADY_ADDED"
			NOT = {
				war_participant_has_war_goal_of_type_against = {
					type = open_market
					target = var:awg_selected_enemy
				}
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = open_market
				primary_demand = no
				target_country = ROOT.var:awg_selected_enemy
			}
		}
		create_incident = {
			country = ROOT
			value = { value = 10 max = 12 min = 4 }
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 12 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 12 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ LIBERATE SUBJECT ─
awg_add_liberate_subject = {
	scope = country
	saved_scopes = { target_country }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		exists = scope:target_country
		scope:target_country = {
			is_subject_of = ROOT.var:awg_selected_enemy
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = liberate_subject
				primary_demand = no
				target_country = scope:target_country
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 13 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 13 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ─ TRANSFER SUBJECT ─
awg_add_transfer_subject = {
	scope = country
	saved_scopes = { target_country }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		exists = scope:target_country
		scope:target_country = {
			is_subject_of = ROOT.var:awg_selected_enemy
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
		random_diplomatic_play = {
			limit = {
				is_war = yes
				any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
				any_scope_play_involved = { THIS = ROOT }
			}
			add_war_goal = {
				holder = ROOT
				type = transfer_subject
				primary_demand = no
				target_country = scope:target_country
			}
		}
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		set_variable = { name = awg_notif_type value = 14 }
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			set_variable = { name = awg_notif_type value = 14 }
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
	}
 
	ai_is_valid = { always = no }
}
 
# ── STATE MODE SELECTION ─────────────────────────────────────────────────
# awg_state_mode: 1 = conquer, 2 = return, 3 = treaty port
 
awg_set_mode_conquest = {
	scope = country
	is_shown = { always = yes }
	effect = {
		set_variable = { name = awg_state_mode value = 1 }
	}
	ai_is_valid = { always = no }
}
 
awg_set_mode_return = {
	scope = country
	is_shown = { always = yes }
	effect = {
		set_variable = { name = awg_state_mode value = 2 }
	}
	ai_is_valid = { always = no }
}
 
awg_set_mode_treaty_port = {
	scope = country
	is_shown = { always = yes }
	effect = {
		set_variable = { name = awg_state_mode value = 3 }
	}
	ai_is_valid = { always = no }
}
 
awg_clear_state_mode = {
	scope = country
	is_shown = { always = yes }
	effect = {
		remove_variable = awg_state_mode
	}
	ai_is_valid = { always = no }
}
 
awg_has_state_mode = {
	scope = country
	is_shown = {
		has_variable = awg_state_mode
	}
}
 
awg_state_mode_is_1 = {
	scope = country
	is_shown = {
		has_variable = awg_state_mode
		var:awg_state_mode = 1
	}
}
 
awg_state_mode_is_2 = {
	scope = country
	is_shown = {
		has_variable = awg_state_mode
		var:awg_state_mode = 2
	}
}
 
awg_state_mode_is_3 = {
	scope = country
	is_shown = {
		has_variable = awg_state_mode
		var:awg_state_mode = 3
	}
}
 
# ── STATE VALIDITY CHECK ─────────────────────────────────────────────────
# Used to check if a specific state is a valid target for the selected mode.
# Treaty port additionally requires the state to be coastal.
 
awg_is_state_valid = {
	scope = country
	saved_scopes = { target_state }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		has_variable = awg_state_mode
		exists = scope:target_state
		scope:target_state = {
			owner = ROOT.var:awg_selected_enemy
			is_under_colonization = no
			NOT = { is_target_of_wargoal = ROOT.var:awg_selected_enemy }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		# Treaty port requires coastal
		trigger_if = {
			limit = { var:awg_state_mode = 3 }
			custom_tooltip = {
				text = "AWG_NOT_COASTAL"
				scope:target_state = { is_coastal = yes }
			}
		}
		trigger_if = {
			limit = { var:awg_state_mode = 2 }
			custom_tooltip = {
				text = "AWG_NOT_CLAIMED"
				scope:target_state = { has_claim = ROOT }
			}
		}
	}
}
 
# ── STATE-TARGETING WARGOAL CONFIRM ──────────────────────────────────────
# Executed when the player clicks a state. Adds the wargoal based on the
# current awg_state_mode, applies infamy via create_incident, then closes.
 
awg_confirm_state_wargoal = {
	scope = country
	saved_scopes = { target_state }
	is_shown = { always = yes }
 
	is_valid = {
		has_variable = awg_selected_enemy
		has_variable = awg_state_mode
		exists = scope:target_state
		scope:target_state = {
			owner = ROOT.var:awg_selected_enemy
			is_under_colonization = no
			NOT = { is_target_of_wargoal = ROOT.var:awg_selected_enemy }
		}
		custom_tooltip = {
			text = "WAR_NOT_WELL_ENOUGH"
			var:awg_selected_enemy = { enemy_occupation > 0.15 }
		}
		custom_tooltip = {
			text = "WARGOAL_ON_COOLDOWN"
			NOT = { has_variable = war_goal_add_cooldown }
		}
		has_war_with = var:awg_selected_enemy
		trigger_if = {
			limit = { var:awg_state_mode = 3 }
			custom_tooltip = {
				text = "AWG_NOT_COASTAL"
				scope:target_state = { is_coastal = yes }
			}
		}
	}
 
	effect = {
		set_variable = { name = war_goal_add_cooldown months = 6 }
 
		# ── CONQUER ──
		if = {
			limit = { var:awg_state_mode = 1 }
			random_diplomatic_play = {
				limit = {
					is_war = yes
					any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
					any_scope_play_involved = { THIS = ROOT }
				}
				add_war_goal = {
					holder = ROOT
					type = conquer_state
					target_state = scope:target_state
					primary_demand = no
					target_country = ROOT.var:awg_selected_enemy
				}
			}
			create_incident = {
				country = ROOT
				value = {
					value = 777
					divide = 36
					max = 50
					min = 3
				}
			}
		}
		# ── RETURN ──
		else_if = {
			limit = { var:awg_state_mode = 2 }
			random_diplomatic_play = {
				limit = {
					is_war = yes
					any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
					any_scope_play_involved = { THIS = ROOT }
				}
				add_war_goal = {
					holder = ROOT
					type = return_state
					target_state = scope:target_state
					primary_demand = no
					target_country = ROOT.var:awg_selected_enemy
				}
			}
			create_incident = {
				country = ROOT
				value = {
					value = 550
					divide = 42
					max = 50
					min = 3
				}
			}
		}
		# ── TREATY PORT ──
		else_if = {
			limit = { var:awg_state_mode = 3 }
			random_diplomatic_play = {
				limit = {
					is_war = yes
					any_scope_play_involved = { THIS = ROOT.var:awg_selected_enemy }
					any_scope_play_involved = { THIS = ROOT }
				}
				add_war_goal = {
					holder = ROOT
					type = take_treaty_port
					target_state = scope:target_state
					primary_demand = no
					target_country = ROOT.var:awg_selected_enemy
				}
			}
			create_incident = {
				country = ROOT
				value = {
					value = 675
					divide = 32
					max = 50
					min = 5
				}
			}
		}
 
		save_scope_as = awg_initiator
		var:awg_selected_enemy = { save_scope_as = awg_target }
		if = {
			limit = { var:awg_state_mode = 1 }
			set_variable = { name = awg_notif_type value = 1 }
		}
		else_if = {
			limit = { var:awg_state_mode = 2 }
			set_variable = { name = awg_notif_type value = 2 }
		}
		else_if = {
			limit = { var:awg_state_mode = 3 }
			set_variable = { name = awg_notif_type value = 3 }
		}
		post_notification = awg_wargoal_added_notification
		var:awg_selected_enemy = {
			if = {
				limit = { ROOT.var:awg_state_mode = 1 }
				set_variable = { name = awg_notif_type value = 1 }
			}
			else_if = {
				limit = { ROOT.var:awg_state_mode = 2 }
				set_variable = { name = awg_notif_type value = 2 }
			}
			else_if = {
				limit = { ROOT.var:awg_state_mode = 3 }
				set_variable = { name = awg_notif_type value = 3 }
			}
			post_notification = awg_wargoal_added_enemy_notification
		}
		remove_variable = awg_panel_open
		remove_variable = awg_selected_enemy
		remove_variable = awg_state_mode
	}
 
	ai_is_valid = { always = no }
}
 
# ── COUNTRY-ONLY WARGOAL CONFIRMATION WRAPPERS ───────────────────────────
# These are called by PopupManager.AskConfirmation to show a "are you sure?"
# before adding the wargoal. They wrap the existing awg_add_X SGUIs.
# Since Vic3 PopupManager.AskConfirmation only works with commands,
# we use the tooltip on the button to show infamy and the SGUI Execute directly.
# The confirmation popup uses: onclick = "[PopupManager.AskConfirmation(GetScriptedGui('X').Execute(...))]"

# ── PER-TYPE STAGING SGUIs ───────────────────────────────────────────────
# Each sets awg_pending_type to a numeric value and awg_confirm_pending
# to trigger the confirmation popup. Keeps the country wargoal is_valid
# checks so buttons grey out appropriately.
 
awg_stage_war_reps = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = war_reparations target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 1 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_open_market = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = open_market target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 2 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_ban_slavery = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = ban_slavery target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 3 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_regime_change = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = regime_change target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 4 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_containment = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = contain_threat target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 5 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_investment_rights = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = foreign_investment_rights target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 6 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_humiliation = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = humiliation target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 7 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_dominion = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		var:awg_selected_enemy = { is_subject = no }
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = make_dominion target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 8 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}
 
awg_stage_protectorate = {
	scope = country
	is_shown = { always = yes }
	is_valid = {
		has_variable = awg_selected_enemy
		custom_tooltip = { text = "WAR_NOT_WELL_ENOUGH" var:awg_selected_enemy = { enemy_occupation > 0.15 } }
		custom_tooltip = { text = "WARGOAL_ON_COOLDOWN" NOT = { has_variable = war_goal_add_cooldown } }
		has_war_with = var:awg_selected_enemy
		var:awg_selected_enemy = { is_subject = no }
		custom_tooltip = { text = "AWG_ALREADY_ADDED" NOT = { war_participant_has_war_goal_of_type_against = { type = make_protectorate target = var:awg_selected_enemy } } }
	}
	effect = { set_variable = { name = awg_pending_type value = 9 } set_variable = { name = awg_confirm_pending } }
	ai_is_valid = { always = no }
}