# Advance-phase effects, branching on wc_training_phase.
#
# Pacing: each advance starts a 30-day cooldown (wc_phase_cooldown_days).
# The Advance button is gated on cooldown <= 0 in the scripted_gui.
# Phase 3 is also a 30-day "final training period" - mastery does NOT fire
# on click 3. Instead, the on_daily action fires the mastery once the phase 3
# cooldown reaches 0 (see wc_grant_mastery_for_active_training_effect below
# and wc_on_actions.txt). The Conclude button only opens after that.
#
# Phase 0 -> 1: pay phase-1 equipment, set phase = 1, light tier-1 reward, start cooldown
# Phase 1 -> 2: pay phase-2 equipment, set phase = 2, light tier-2 reward, start cooldown
# Phase 2 -> 3: pay phase-3 equipment, set phase = 3, light tier-3 reward, start FINAL cooldown
#               (the mastery idea is granted by the on_daily handler when this cooldown ends)
#
# Training id mapping:
#   1 = Infantry Studies (track infantry)              - infantry_equipment 250/350/400 (1000 total)
#   2 = Artillery Support Studies (combat_support)     - artillery_equipment 30/50/70 (150 total)
#   3 = Armor Studies (armor)                          - light_tank_chassis 20/30/50 (100 total)
#   4 = Operational Studies (operations)               - infantry_equipment 250/350/400 (1000 total)
#                                                       + support_equipment 25/35/40 (100 total)

@wc_phase_cooldown_default = 30

# ============================================================
# Per-training advance effects
# ============================================================

# T1 Infantry Studies (id = 1) - 1000 inf total
wc_advance_t1_infantry_studies_effect = {
	if = {
		limit = { check_variable = { wc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -250 }
		set_variable = { wc_training_phase = 1 }
		set_variable = { wc_reward_t1_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -350 }
		set_variable = { wc_training_phase = 2 }
		set_variable = { wc_reward_t2_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -400 }
		set_variable = { wc_training_phase = 3 }
		set_variable = { wc_reward_t3_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
}

# T1 Artillery Support Studies (id = 2) - combat_support track. 150 art total.
wc_advance_t1_artillery_support_studies_effect = {
	if = {
		limit = { check_variable = { wc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = artillery_equipment amount = -30 }
		set_variable = { wc_training_phase = 1 }
		set_variable = { wc_reward_t1_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = artillery_equipment amount = -50 }
		set_variable = { wc_training_phase = 2 }
		set_variable = { wc_reward_t2_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = artillery_equipment amount = -70 }
		set_variable = { wc_training_phase = 3 }
		set_variable = { wc_reward_t3_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
}

# T1 Armor Studies (id = 3) - armor track. Uses light_tank_equipment - the
# entry-tier tank archetype - so countries can start this training as soon as
# they have a basic light tank line.
wc_advance_t1_armor_studies_effect = {
	if = {
		limit = { check_variable = { wc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = light_tank_chassis amount = -20 }
		set_variable = { wc_training_phase = 1 }
		set_variable = { wc_reward_t1_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = light_tank_chassis amount = -30 }
		set_variable = { wc_training_phase = 2 }
		set_variable = { wc_reward_t2_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = light_tank_chassis amount = -50 }
		set_variable = { wc_training_phase = 3 }
		set_variable = { wc_reward_t3_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
}

# T1 Operational Studies (id = 4) - operations track. Combined arms: each
# phase pays both infantry_equipment and support_equipment.
# 1000 inf + 100 sup total.
wc_advance_t1_operational_studies_effect = {
	if = {
		limit = { check_variable = { wc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -250 }
		add_equipment_to_stockpile = { type = support_equipment amount = -25 }
		set_variable = { wc_training_phase = 1 }
		set_variable = { wc_reward_t1_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -350 }
		add_equipment_to_stockpile = { type = support_equipment amount = -35 }
		set_variable = { wc_training_phase = 2 }
		set_variable = { wc_reward_t2_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { wc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = infantry_equipment amount = -400 }
		add_equipment_to_stockpile = { type = support_equipment amount = -40 }
		set_variable = { wc_training_phase = 3 }
		set_variable = { wc_reward_t3_frame = 2 }
		set_variable = { wc_phase_cooldown_days = @wc_phase_cooldown_default }
	}
}

# ============================================================
# Dispatchers
# ============================================================

# Advance dispatcher: maps wc_training_id to the right per-training advance effect.
# The outer guard makes this a no-op while the phase cooldown is still running
# OR the training has already reached phase 3 (the engine waits on cooldown
# elapsing and the on_daily mastery grant before the player can Conclude).
# This is belt-and-suspenders alongside the scripted_gui _click_enabled trigger.
wc_advance_active_training_effect = {
	if = {
		limit = {
			check_variable = { wc_training_active = 1 }
			check_variable = { wc_phase_cooldown_days < 1 }
			check_variable = { wc_training_phase < 3 }
			wc_can_pay_current_phase = yes
		}
		if = {
			limit = { check_variable = { wc_training_id = 1 } }
			wc_advance_t1_infantry_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { wc_training_id = 2 } }
			wc_advance_t1_artillery_support_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { wc_training_id = 3 } }
			wc_advance_t1_armor_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { wc_training_id = 4 } }
			wc_advance_t1_operational_studies_effect = yes
		}
	}
}

# Mastery-grant dispatcher: fires the 180-day add_daily_mastery for the active
# training. Called by the on_daily handler the moment phase 3's cooldown
# transitions to 0. Each branch maps a training id to its track and rate.
#
# Per-track raw rates (input-side multiplier per land_doctrine_tracks.txt):
#   infantry       multiplier 1.0  -> 0.03    raw  = effective 0.03/day
#   combat_support multiplier 8.0  -> 0.00375 raw  = effective 0.03/day
#   armor          multiplier 10.0 -> 0.003   raw  = effective 0.03/day
#   operations     multiplier 0.7  -> 0.0429  raw  = effective 0.03/day
#
# Mastery scales with the academy tier upgrade for the matching track.
# Scaling: T1 = base, T2 = 1.5x, T3 = 2x. Per-tier amounts are listed
# inline below so retuning is just a number swap. The tier vars live
# on the country and are bumped by wc_complete_upgrade_effect.
wc_grant_mastery_for_active_training_effect = {
	if = {
		limit = { check_variable = { wc_training_id = 1 } }
		if = {
			limit = { check_variable = { wc_infantry_tier = 3 } }
			add_daily_mastery = { amount = 0.256 days = 360 name = wc_t1_infantry_studies_complete track = infantry }
		}
		else_if = {
			limit = { check_variable = { wc_infantry_tier = 2 } }
			add_daily_mastery = { amount = 0.192 days = 210 name = wc_t1_infantry_studies_complete track = infantry }
		}
		else = {
			add_daily_mastery = { amount = 0.094 days = 180 name = wc_t1_infantry_studies_complete track = infantry }
		}
	}
	else_if = {
		limit = { check_variable = { wc_training_id = 2 } }
		if = {
			limit = { check_variable = { wc_artillery_tier = 3 } }
			add_daily_mastery = { amount = 0.24 days = 360 name = wc_t1_artillery_support_studies_complete track = combat_support }
		}
		else_if = {
			limit = { check_variable = { wc_artillery_tier = 2 } }
			add_daily_mastery = { amount = 0.19 days = 210 name = wc_t1_artillery_support_studies_complete track = combat_support }
		}
		else = {
			add_daily_mastery = { amount = 0.09 days = 180 name = wc_t1_artillery_support_studies_complete track = combat_support }
		}
	}
	else_if = {
		limit = { check_variable = { wc_training_id = 3 } }
		if = {
			limit = { check_variable = { wc_armor_tier = 3 } }
			add_daily_mastery = { amount = 0.2 days = 360 name = wc_t1_armor_studies_complete track = armor }
		}
		else_if = {
			limit = { check_variable = { wc_armor_tier = 2 } }
			add_daily_mastery = { amount = 0.15 days = 210 name = wc_t1_armor_studies_complete track = armor }
		}
		else = {
			add_daily_mastery = { amount = 0.09 days = 180 name = wc_t1_armor_studies_complete track = armor }
		}
	}
	else_if = {
		limit = { check_variable = { wc_training_id = 4 } }
		if = {
			limit = { check_variable = { wc_operational_tier = 3 } }
			add_daily_mastery = { amount = 0.32 days = 360 name = wc_t1_operational_studies_complete track = operations }
		}
		else_if = {
			limit = { check_variable = { wc_operational_tier = 2 } }
			add_daily_mastery = { amount = 0.24 days = 210 name = wc_t1_operational_studies_complete track = operations }
		}
		else = {
			add_daily_mastery = { amount = 0.09 days = 180 name = wc_t1_operational_studies_complete track = operations }
		}
	}

	# Training is fully complete - notify the player. Mastery is active,
	# the slot will free once they click Conclude in the war college.
	country_event = { id = wc_academy.3 hours = 0 }
}

# Conclude: clears the slot once the player acknowledges the completion screen.
# The 180-day mastery idea fired by wc_grant_mastery_for_active_training_effect
# is independent of this - it runs to completion regardless of whether the
# slot is occupied.
wc_conclude_active_training_effect = {
	set_variable = { wc_training_active = 0 }
	set_variable = { wc_training_id = 0 }
	set_variable = { wc_training_phase = 0 }
	set_variable = { wc_phase_cooldown_days = 0 }
	set_variable = { wc_reward_t1_frame = 1 }
	set_variable = { wc_reward_t2_frame = 1 }
	set_variable = { wc_reward_t3_frame = 1 }
}
