# FRONTLINE AI - FIXED VERSION V3
# Added explicit priority values to ensure proper strategy activation
# Higher priority = overrides lower priority

############################################
# PRIORITY 1: STOP - Can't fight effectively
# Priority: 1000 (highest - emergency defensive mode)
############################################
ai_front_management_stop = {
	enable = {
		has_war = yes
		has_manpower < 5000
		OR = {
			surrender_progress > 0.3
			enemies_strength_ratio > 2.0
		}
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 1000  # ADDED: Highest priority - emergency mode
		ordertype = front
		execution_type = careful
		execute_order = yes
		manual_attack = no
	}
}

############################################
# PRIORITY 2: DESPERATION RUSH - Almost dead but can still win
# Priority: 900 (desperation gamble)
############################################
ai_front_management_rush_desperation = {
	enable = {
		has_war = yes
		has_manpower < 500
		surrender_progress > 0.4
		surrender_progress < 0.7
		enemies_strength_ratio < 0.9
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 900  # ADDED: Very high - desperation
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 3: FAILING RUSH - Losing but have advantage
# Priority: 800 (critical situation, but can push)
############################################
ai_front_management_rush_failing = {
	enable = {
		has_war = yes
		surrender_progress > 0.2
		surrender_progress < 0.4
		has_manpower > 499
		enemies_strength_ratio < 1.0
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 800  # ADDED: High priority - crisis mode
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 4: FAILING CAREFUL - Losing, enemy stronger
# Priority: 800 (same as Priority 3, but different conditions)
############################################
ai_front_management_careful_failing = {
	enable = {
		has_war = yes
		surrender_progress > 0.2
		surrender_progress < 0.4
		has_manpower > 499
		enemies_strength_ratio > 0.99
		enemies_strength_ratio < 2.0
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 800  # ADDED: Same as Priority 3 (mutually exclusive)
		ordertype = front
		execution_type = careful
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 5: NORMAL RUSH - Stable position, can attack majors
# Priority: 200 (normal aggressive warfare)
############################################
ai_front_management_rush = {
	enable = {
		has_war = yes
		any_enemy_country = { is_major = yes }
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio < 1.1
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 200  # ADDED: Normal offensive priority
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 6: NORMAL CAREFUL - Stable position, enemy majors stronger
# Priority: 200 (normal defensive warfare)
############################################
ai_front_management_careful = {
	enable = {
		has_war = yes
		any_enemy_country = { is_major = yes }
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio > 1.09
		enemies_strength_ratio < 2.0
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 200  # ADDED: Same as Priority 5 (mutually exclusive)
		ordertype = front
		execution_type = careful
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 7: RUSH VS MINORS - Aggressive vs small enemies
# Priority: 150 (less important than fighting majors)
############################################
ai_front_management_rush_vs_minors = {
	enable = {
		has_war = yes
		is_major = yes
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio < 1.5
		NOT = { any_enemy_country = { is_major = yes } }
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 150  # ADDED: Lower than major wars
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# PRIORITY 8: CAREFUL VS MINORS - Defensive vs surprisingly strong minors
# Priority: 150 (same as Priority 7)
############################################
ai_front_management_careful_vs_minors = {
	enable = {
		has_war = yes
		is_major = yes
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio > 1.49
		enemies_strength_ratio < 2.0
		NOT = { any_enemy_country = { is_major = yes } }
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 150  # ADDED: Same as Priority 7 (mutually exclusive)
		ordertype = front
		execution_type = careful
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# NEW: DEFAULT RUSH - When no specific strategy applies
# Priority: 50 (lowest main strategy priority)
############################################
ai_front_management_default_rush = {
	enable = {
		has_war = yes
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio < 1.2  # Slight advantage or even
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 50  # ADDED: Catch-all for undefined situations
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# NEW: DEFAULT BALANCED - When rush isn't viable
# Priority: 50 (same as default rush)
############################################
ai_front_management_default_balanced = {
	enable = {
		has_war = yes
		surrender_progress < 0.2
		has_manpower > 499
		enemies_strength_ratio > 1.19
		enemies_strength_ratio < 2.0
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		country_trigger = {
			has_war_with = FROM
		}
		priority = 50  # ADDED: Same as default rush
		ordertype = front
		execution_type = balanced
		execute_order = yes
		manual_attack = yes
	}
}

############################################
# UTILITY: Defend home when losing
# Priority: N/A (different strategy type)
############################################
defend_your_home_please = {
	enable = {
		surrender_progress > 0.1
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = spare_unit_factor
		value = -100
	}
	
	ai_strategy = {
		type = garrison
		value = -100
	}
}

############################################
# UTILITY: Don't waste troops on garrison if large empire
# Priority: N/A (different strategy type)
############################################
dont_waste_troops_please = {
	enable = {
		num_of_controlled_states > 70
	}
	
	abort_when_not_enabled = yes

	ai_strategy = {
		type = garrison
		value = -100
	}
}

############################################
# UTILITY: Close pockets aggressively
# Priority: 5000 (very high - tactical opportunity)
############################################
end_pocket = {
	enable = {
		has_war = yes
	}
	
	abort_when_not_enabled = yes
	
	ai_strategy = {
		type = front_control
		state_trigger = {
			OR = {
				AND = {
					is_controlled_by = FROM.FROM
					NOT = { is_fully_controlled_by = FROM.FROM }
				}
				controller = {
					has_war_with = FROM.FROM
				}
			}
			all_neighbor_state = {
				controller = {
					NOT = { has_war_with = FROM.FROM }
				}
			}
		}
		ratio = 0.0
		priority = 5000  # Keep very high - tactical priority
		ordertype = front
		execution_type = rush
		execute_order = yes
		manual_attack = yes
	}
	
	ai_strategy = {
		type = front_unit_request
		state_trigger = {
			OR = {
				AND = {
					is_controlled_by = FROM.FROM
					NOT = { is_fully_controlled_by = FROM.FROM }
				}
				controller = {
					has_war_with = FROM.FROM
				}
			}
			all_neighbor_state = {
				controller = {
					NOT = { has_war_with = FROM.FROM }
				}
			}
		}		
		value = 40 
	}	
}

############################################
# UTILITY: Strategic positioning
# Priority: N/A (different strategy type - unit_request)
############################################
forced_check_omega_lul = {
	enable = {
		has_war = yes
	}
	
	abort_when_not_enabled = yes
	
	# Deprioritize states without supply nodes
	ai_strategy = {
		type = front_unit_request
		state_trigger = {
			is_controlled_by = FROM.FROM
			NOT = {
				any_province_building_level = {
					province = {
						all_provinces = yes
					}
					building = supply_node
					level > 0
				}
			}
		}
		value = -5
	}
	
	# Prioritize capital area
	ai_strategy = {
		type = front_unit_request
		state_trigger = { 
			OR = {
				is_capital = yes
				any_neighbor_state = { is_capital = yes }
			}
		}
		value = 15
	}
	
	# Heavily prioritize own capital
	ai_strategy = {
		type = front_unit_request
		state_trigger = { 
			is_capital = yes
			owner = { tag = FROM.FROM }
		}
		value = 50
	}
	
	# Deprioritize low-value naval invasions
	ai_strategy = {
		type = invasion_unit_request
		state_trigger = {
			NOT = { has_state_flag = recent_naval_invasion_flag }
			check_variable = { state_strategic_value < 3 }
		}
		value = -5
	}
	
	ai_strategy = {
		type = invasion_unit_request
		state_trigger = { 
			NOT = { is_core_of = FROM.FROM }
			NOT = { has_state_flag = recent_naval_invasion_flag }
			check_variable = { state_strategic_value < 8 }
		}
		value = -3
	}
	
	ai_strategy = {
		type = invasion_unit_request
		state_trigger = { 
			NOT = { is_core_of = FROM.FROM }
			NOT = { has_state_flag = recent_naval_invasion_flag }
		}
		value = -3
	}
	
	ai_strategy = {
		type = invasion_unit_request
		state_trigger = { 
			NOT = { is_core_of = FROM.FROM }
			NOT = { has_state_flag = recent_naval_invasion_flag }
			NOT = {
				any_province_building_level = {
					province = {
						all_provinces = yes
					}
					building = supply_node
					level > 0
				}
			}
		}
		value = -2
	}
}

############################################
# UTILITY: Rush empty enemy frontlines
# Priority: 10000 (extremely high - tactical opportunity)
############################################
this_state_is_empty = {
	enable = {
		has_war = yes
	}
	
	abort_when_not_enabled = yes
	
	ai_strategy = {
		type = front_control
		state_trigger = {
			controller = {
				tag = FROM
				divisions_in_state = {
					state = PREV
					size < 1
				}
			}
		}
		ratio = 0.0
		priority = 10000  # RAISED from 5000 - highest priority (free real estate!)
		ordertype = front
		execution_type = rush
		execute_order = yes
	}
}