extends SceneTree

const BattleSceneScript := preload("res://scripts/scenes/battle_scene.gd")

const CAMPAIGN_PATH := "res://data/campaign/campaign.json"
const SCENARIO_ID := "001_yellow_turbans"
const SCENARIO_PATH := "res://data/scenarios/001_yellow_turbans.json"
const CONVERSATION_ID := "northern_woods_cache"
const SISHUI_SCENARIO_ID := "002_sishui_gate"
const SISHUI_SCENARIO_PATH := "res://data/scenarios/002_sishui_gate.json"
const SISHUI_CONVERSATION_ID := "sishui_quartermaster_antidote"
const BRANCH_SCENARIO_ID := "003_xingyang_ambush"
const BRANCH_SCENARIO_PATH := "res://data/scenarios/003_xingyang_ambush.json"
const QINGZHOU_SCENARIO_ID := "004_qingzhou_campaign"
const QINGZHOU_SCENARIO_PATH := "res://data/scenarios/004_qingzhou_campaign.json"
const PUYANG_SCENARIO_ID := "005_puyang_raid"
const PUYANG_SCENARIO_PATH := "res://data/scenarios/005_puyang_raid.json"
const PUYANG_CONVERSATION_ID := "puyang_raider_stores"
const PUYANG_PURSUIT_CONVERSATION_ID := "puyang_hardened_vanguard"
const PUYANG_REGROUP_CONVERSATION_ID := "puyang_qingzhou_reserve_wagon"
const DINGTAO_SCENARIO_ID := "006_dingtao_counterattack"
const DINGTAO_SCENARIO_PATH := "res://data/scenarios/006_dingtao_counterattack.json"
const DINGTAO_FORTIFIED_CONVERSATION_ID := "dingtao_fortified_reserves"
const DINGTAO_PRESSED_CONVERSATION_ID := "dingtao_pressed_vanguard"
const XIAN_SCENARIO_ID := "007_xian_emperor_escort"
const XIAN_SCENARIO_PATH := "res://data/scenarios/007_xian_emperor_escort.json"
const XIAN_FORTIFIED_CONVERSATION_ID := "xian_fortified_road_supplies"
const XIAN_PRESSED_CONVERSATION_ID := "xian_forced_march_canteens"
const WAN_SCENARIO_ID := "008_wan_castle_escape"
const WAN_SCENARIO_PATH := "res://data/scenarios/008_wan_castle_escape.json"
const WAN_COURT_CONVERSATION_ID := "wan_court_seal_postern"
const WAN_PURSUIT_CONVERSATION_ID := "wan_hard_pursuit_drums"
const WUCHAO_SCENARIO_ID := "013_wuchao_raid"
const WUCHAO_SCENARIO_PATH := "res://data/scenarios/013_wuchao_raid.json"
const SAVE_PATH := "user://campaign_save.json"
const MANUAL_SAVE_PATH := "user://campaign_manual_save.json"


func _init() -> void:
	var save_backup := _backup_user_file(SAVE_PATH)
	var manual_backup := _backup_user_file(MANUAL_SAVE_PATH)
	var failures: Array[String] = []

	_check_claim_updates_campaign_and_battle(failures)
	_check_shop_and_armory_preserve_claim(failures)
	_check_sishui_gate_camp_supplies(failures)
	_check_puyang_raid_camp_supplies(failures)
	_check_dingtao_branch_camp_conversations(failures)
	_check_xian_branch_camp_conversations(failures)
	_check_wan_branch_camp_conversations(failures)
	_check_manual_checkpoint_reverts_claim(failures)
	_check_completed_replay_cannot_claim(failures)
	_check_conditional_camp_conversations(failures)
	_check_qingzhou_branch_camp_conversations(failures)
	_check_wuchao_branch_camp_conversations(failures)
	_check_talk_menu_closes_on_cancel_and_reload(failures)

	if not _restore_user_file(SAVE_PATH, save_backup):
		failures.append("could not restore automatic campaign save")
	if not _restore_user_file(MANUAL_SAVE_PATH, manual_backup):
		failures.append("could not restore manual campaign save")

	if failures.is_empty():
		print("camp conversation rewards smoke ok")
		quit(0)
		return

	for failure in failures:
		push_error(failure)
	quit(1)


func _check_claim_updates_campaign_and_battle(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene(failures, "claim update")
	if scene == null:
		return
	var conversation: Dictionary = scene._camp_conversation_by_id(CONVERSATION_ID)
	if not _check_cache_conversation(failures, conversation):
		scene.free()
		return

	var before := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
	if not scene._format_camp_conversation_button_text(conversation).contains("available"):
		failures.append("camp conversation button should show available before claim")
	scene._apply_camp_conversation_effects(conversation)

	var campaign_after := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
	var battle_after := int(scene.state.get_inventory_snapshot().get("bean", 0))
	if campaign_after != before + 1:
		failures.append("camp conversation should add one Bean to campaign inventory")
	if battle_after != before + 1:
		failures.append("camp conversation should refresh loaded battle inventory")
	if not scene.campaign_state.has_claimed_camp_conversation_effects(SCENARIO_ID, CONVERSATION_ID):
		failures.append("camp conversation claim should be saved in campaign ledger")
	if not scene._format_camp_conversation_button_text(conversation).contains("claimed"):
		failures.append("camp conversation button should show claimed after claim")

	scene._apply_camp_conversation_effects(conversation)
	var duplicate_after := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
	if duplicate_after != campaign_after:
		failures.append("camp conversation should not duplicate rewards")
	scene.free()


func _check_shop_and_armory_preserve_claim(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene(failures, "shop and armory preservation")
	if scene == null:
		return
	var conversation: Dictionary = scene._camp_conversation_by_id(CONVERSATION_ID)
	if not _check_cache_conversation(failures, conversation):
		scene.free()
		return
	scene._apply_camp_conversation_effects(conversation)
	var claimed_beans := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))

	scene.campaign_state.gold = 1000
	var antidote: Dictionary = scene.state.get_item_def("antidote")
	var price := int(antidote.get("price", 0))
	if price <= 0 or not scene.campaign_state.try_buy_item("antidote", price, SCENARIO_ID, scene.state.get_shop_stock_limit("antidote")):
		failures.append("shop purchase after camp claim should save")
	else:
		scene.state.set_inventory_snapshot(scene.campaign_state.get_inventory_snapshot())
		var after_buy := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
		if after_buy != claimed_beans:
			failures.append("shop purchase should preserve camp-claimed Bean")

	if not scene.campaign_state.try_save_prebattle_loadout(scene.state.get_player_roster_snapshot(), scene.state.get_inventory_snapshot()):
		failures.append("armory save after camp claim should save")
	else:
		var after_armory := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
		if after_armory != claimed_beans:
			failures.append("armory save should preserve camp-claimed Bean")
		if not scene.campaign_state.has_claimed_camp_conversation_effects(SCENARIO_ID, CONVERSATION_ID):
			failures.append("armory save should preserve camp claim ledger")
	scene.free()


func _check_sishui_gate_camp_supplies(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene_for(failures, "Sishui camp supplies", SISHUI_SCENARIO_ID, SISHUI_SCENARIO_PATH)
	if scene == null:
		return
	var conversation: Dictionary = scene._camp_conversation_by_id(SISHUI_CONVERSATION_ID)
	if conversation.is_empty():
		failures.append("Sishui should expose quartermaster antidote conversation")
		scene.free()
		return
	if not scene._format_camp_conversation_button_text(conversation).contains("Supply Antidote"):
		failures.append("Sishui quartermaster button should preview Antidote supply")
	var before_antidote := int(scene.campaign_state.get_inventory_snapshot().get("antidote", 0))
	scene._apply_camp_conversation_effects(conversation)
	if int(scene.campaign_state.get_inventory_snapshot().get("antidote", 0)) != before_antidote + 1:
		failures.append("Sishui quartermaster should add Antidote to campaign inventory")
	if int(scene.state.get_inventory_snapshot().get("antidote", 0)) != before_antidote + 1:
		failures.append("Sishui quartermaster should refresh battle Antidote inventory")
	if not scene.campaign_state.has_claimed_camp_conversation_effects(SISHUI_SCENARIO_ID, SISHUI_CONVERSATION_ID):
		failures.append("Sishui quartermaster claim should be saved")
	scene.free()


func _check_puyang_raid_camp_supplies(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene_for(failures, "Puyang raid camp supplies", PUYANG_SCENARIO_ID, PUYANG_SCENARIO_PATH)
	if scene == null:
		return
	if scene._camp_conversation_by_id("puyang_night_raid_council").is_empty():
		failures.append("Puyang should expose the night raid council conversation")
	if scene._camp_conversation_by_id("puyang_dian_wei_shield").is_empty():
		failures.append("Puyang should expose Dian Wei's officer conversation")
	if not scene._camp_conversation_by_id(PUYANG_PURSUIT_CONVERSATION_ID).is_empty():
		failures.append("Puyang pursuit supplies should be hidden without pursuit flag")
	if not scene._camp_conversation_by_id(PUYANG_REGROUP_CONVERSATION_ID).is_empty():
		failures.append("Puyang regroup supplies should be hidden without regroup flag")
	var conversation: Dictionary = scene._camp_conversation_by_id(PUYANG_CONVERSATION_ID)
	if conversation.is_empty():
		failures.append("Puyang should expose raider stores conversation")
		scene.free()
		return
	var merchant: Dictionary = scene.state.get_shop_merchant()
	if str(merchant.get("name", "")) != "Puyang Night Trader":
		failures.append("Puyang shop merchant name should be present")
	var merchant_lines: Array = merchant.get("lines", [])
	if merchant_lines.size() < 2:
		failures.append("Puyang shop merchant should expose flavor lines")
	if scene._format_talk_status_text(scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
		failures.append("Puyang talk status should show one ready supply")
	if not scene._format_camp_conversation_button_text(conversation).contains("Supply Bean"):
		failures.append("Puyang raider stores button should preview Bean supply")
	var before_bean := int(scene.campaign_state.get_inventory_snapshot().get("bean", 0))
	scene._apply_camp_conversation_effects(conversation)
	if int(scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
		failures.append("Puyang raider stores should add Bean to campaign inventory")
	if int(scene.state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
		failures.append("Puyang raider stores should refresh battle Bean inventory")
	if not scene.campaign_state.has_claimed_camp_conversation_effects(PUYANG_SCENARIO_ID, PUYANG_CONVERSATION_ID):
		failures.append("Puyang raider stores claim should be saved")
	if scene._format_talk_status_text(scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
		failures.append("Puyang talk status should show claimed supply after claim")
	scene.free()

	var pursuit_scene = _new_prebattle_scene_for(
		failures,
		"Puyang pursuit camp supplies",
		PUYANG_SCENARIO_ID,
		PUYANG_SCENARIO_PATH,
		{"pursued_dong_zhuo": true}
	)
	if pursuit_scene != null:
		var vanguard: Dictionary = pursuit_scene._camp_conversation_by_id(PUYANG_PURSUIT_CONVERSATION_ID)
		if vanguard.is_empty():
			failures.append("Puyang pursuit flag should expose hardened vanguard supplies")
		if not pursuit_scene._camp_conversation_by_id(PUYANG_REGROUP_CONVERSATION_ID).is_empty():
			failures.append("Puyang pursuit flag should not expose Qingzhou reserve wagon")
		if not vanguard.is_empty():
			if pursuit_scene._format_talk_status_text(pursuit_scene._camp_conversation_entries()) != "Camp conversations | 4 topics | 2 supplies ready":
				failures.append("Puyang pursuit talk status should show two ready supplies")
			if not pursuit_scene._format_camp_conversation_button_text(vanguard).contains("Supply Wine"):
				failures.append("Puyang hardened vanguard button should preview Wine supply")
			var before_wine := int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
			pursuit_scene._apply_camp_conversation_effects(vanguard)
			if int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Puyang hardened vanguard should add Wine to campaign inventory")
			if int(pursuit_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Puyang hardened vanguard should refresh battle Wine inventory")
			if not pursuit_scene.campaign_state.has_claimed_camp_conversation_effects(PUYANG_SCENARIO_ID, PUYANG_PURSUIT_CONVERSATION_ID):
				failures.append("Puyang hardened vanguard claim should be saved")
		pursuit_scene.free()

	var regroup_scene = _new_prebattle_scene_for(
		failures,
		"Puyang regroup camp supplies",
		PUYANG_SCENARIO_ID,
		PUYANG_SCENARIO_PATH,
		{"regrouped_after_sishui": true}
	)
	if regroup_scene != null:
		var reserve: Dictionary = regroup_scene._camp_conversation_by_id(PUYANG_REGROUP_CONVERSATION_ID)
		if reserve.is_empty():
			failures.append("Puyang regroup flag should expose Qingzhou reserve wagon")
		if not regroup_scene._camp_conversation_by_id(PUYANG_PURSUIT_CONVERSATION_ID).is_empty():
			failures.append("Puyang regroup flag should not expose hardened vanguard")
		if not reserve.is_empty():
			if regroup_scene._format_talk_status_text(regroup_scene._camp_conversation_entries()) != "Camp conversations | 4 topics | 2 supplies ready":
				failures.append("Puyang regroup talk status should show two ready supplies")
			if not regroup_scene._format_camp_conversation_button_text(reserve).contains("Supply Panacea"):
				failures.append("Puyang Qingzhou reserve button should preview Panacea supply")
			var before_panacea := int(regroup_scene.campaign_state.get_inventory_snapshot().get("panacea", 0))
			regroup_scene._apply_camp_conversation_effects(reserve)
			if int(regroup_scene.campaign_state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Puyang Qingzhou reserve should add Panacea to campaign inventory")
			if int(regroup_scene.state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Puyang Qingzhou reserve should refresh battle Panacea inventory")
			if not regroup_scene.campaign_state.has_claimed_camp_conversation_effects(PUYANG_SCENARIO_ID, PUYANG_REGROUP_CONVERSATION_ID):
				failures.append("Puyang Qingzhou reserve claim should be saved")
		regroup_scene.free()


func _check_dingtao_branch_camp_conversations(failures: Array[String]) -> void:
	var base_scene = _new_prebattle_scene_for(failures, "Dingtao base camp", DINGTAO_SCENARIO_ID, DINGTAO_SCENARIO_PATH)
	if base_scene != null:
		if base_scene._camp_conversation_by_id("dingtao_countercharge_council").is_empty():
			failures.append("Dingtao should expose the countercharge council conversation")
		if base_scene._camp_conversation_by_id("dingtao_dian_wei_roadblock").is_empty():
			failures.append("Dingtao should expose Dian Wei's roadblock conversation")
		if not base_scene._camp_conversation_by_id(DINGTAO_FORTIFIED_CONVERSATION_ID).is_empty():
			failures.append("Dingtao fortified supplies should be hidden without fortified flag")
		if not base_scene._camp_conversation_by_id(DINGTAO_PRESSED_CONVERSATION_ID).is_empty():
			failures.append("Dingtao pressed supplies should be hidden without pressed flag")
		var merchant: Dictionary = base_scene.state.get_shop_merchant()
		if str(merchant.get("name", "")) != "Dingtao Field Sutler":
			failures.append("Dingtao shop merchant name should be present")
		var merchant_lines: Array = merchant.get("lines", [])
		if merchant_lines.size() < 2:
			failures.append("Dingtao shop merchant should expose flavor lines")
		if base_scene._format_talk_status_text(base_scene._camp_conversation_entries()) != "Camp conversations | 2 topics":
			failures.append("Dingtao base talk status should summarize two non-supply topics")
		base_scene.free()

	var fortified_scene = _new_prebattle_scene_for(
		failures,
		"Dingtao fortified camp",
		DINGTAO_SCENARIO_ID,
		DINGTAO_SCENARIO_PATH,
		{"fortified_yan_province": true}
	)
	if fortified_scene != null:
		var reserves: Dictionary = fortified_scene._camp_conversation_by_id(DINGTAO_FORTIFIED_CONVERSATION_ID)
		if reserves.is_empty():
			failures.append("fortified flag should expose Dingtao fortified reserves")
		if not fortified_scene._camp_conversation_by_id(DINGTAO_PRESSED_CONVERSATION_ID).is_empty():
			failures.append("fortified flag should not expose Dingtao pressed vanguard")
		if not reserves.is_empty():
			if fortified_scene._format_talk_status_text(fortified_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Dingtao fortified talk status should show one ready supply")
			if not fortified_scene._format_camp_conversation_button_text(reserves).contains("Supply Panacea"):
				failures.append("Dingtao fortified reserves button should preview Panacea supply")
			var before_panacea := int(fortified_scene.campaign_state.get_inventory_snapshot().get("panacea", 0))
			fortified_scene._apply_camp_conversation_effects(reserves)
			if int(fortified_scene.campaign_state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Dingtao fortified reserves should add Panacea to campaign inventory")
			if int(fortified_scene.state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Dingtao fortified reserves should refresh battle Panacea inventory")
			if not fortified_scene.campaign_state.has_claimed_camp_conversation_effects(DINGTAO_SCENARIO_ID, DINGTAO_FORTIFIED_CONVERSATION_ID):
				failures.append("Dingtao fortified reserves claim should be saved")
			if fortified_scene._format_talk_status_text(fortified_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Dingtao fortified talk status should show claimed supply after claim")
		fortified_scene.free()

	var pressed_scene = _new_prebattle_scene_for(
		failures,
		"Dingtao pressed camp",
		DINGTAO_SCENARIO_ID,
		DINGTAO_SCENARIO_PATH,
		{"pressed_lu_bu": true}
	)
	if pressed_scene != null:
		var vanguard: Dictionary = pressed_scene._camp_conversation_by_id(DINGTAO_PRESSED_CONVERSATION_ID)
		if vanguard.is_empty():
			failures.append("pressed flag should expose Dingtao pressed vanguard")
		if not pressed_scene._camp_conversation_by_id(DINGTAO_FORTIFIED_CONVERSATION_ID).is_empty():
			failures.append("pressed flag should not expose Dingtao fortified reserves")
		if not vanguard.is_empty():
			if pressed_scene._format_talk_status_text(pressed_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Dingtao pressed talk status should show one ready supply")
			if not pressed_scene._format_camp_conversation_button_text(vanguard).contains("Supply Wine"):
				failures.append("Dingtao pressed vanguard button should preview Wine supply")
			var before_wine := int(pressed_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
			pressed_scene._apply_camp_conversation_effects(vanguard)
			if int(pressed_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Dingtao pressed vanguard should add Wine to campaign inventory")
			if int(pressed_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Dingtao pressed vanguard should refresh battle Wine inventory")
			if not pressed_scene.campaign_state.has_claimed_camp_conversation_effects(DINGTAO_SCENARIO_ID, DINGTAO_PRESSED_CONVERSATION_ID):
				failures.append("Dingtao pressed vanguard claim should be saved")
			if pressed_scene._format_talk_status_text(pressed_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Dingtao pressed talk status should show claimed supply after claim")
		pressed_scene.free()


func _check_xian_branch_camp_conversations(failures: Array[String]) -> void:
	var base_scene = _new_prebattle_scene_for(failures, "Xian escort base camp", XIAN_SCENARIO_ID, XIAN_SCENARIO_PATH)
	if base_scene != null:
		if base_scene._camp_conversation_by_id("xian_escort_council").is_empty():
			failures.append("Xian escort should expose the escort council conversation")
		if base_scene._camp_conversation_by_id("xian_dian_wei_envoy_screen").is_empty():
			failures.append("Xian escort should expose Dian Wei's envoy screen conversation")
		if not base_scene._camp_conversation_by_id(XIAN_FORTIFIED_CONVERSATION_ID).is_empty():
			failures.append("Xian fortified supplies should be hidden without fortified flag")
		if not base_scene._camp_conversation_by_id(XIAN_PRESSED_CONVERSATION_ID).is_empty():
			failures.append("Xian pressed supplies should be hidden without pressed flag")
		var merchant: Dictionary = base_scene.state.get_shop_merchant()
		if str(merchant.get("name", "")) != "Escort Road Sutler":
			failures.append("Xian escort shop merchant name should be present")
		var merchant_lines: Array = merchant.get("lines", [])
		if merchant_lines.size() < 2:
			failures.append("Xian escort shop merchant should expose flavor lines")
		if base_scene._format_talk_status_text(base_scene._camp_conversation_entries()) != "Camp conversations | 2 topics":
			failures.append("Xian escort base talk status should summarize two non-supply topics")
		base_scene.free()

	var fortified_scene = _new_prebattle_scene_for(
		failures,
		"Xian escort fortified camp",
		XIAN_SCENARIO_ID,
		XIAN_SCENARIO_PATH,
		{"fortified_yan_province": true}
	)
	if fortified_scene != null:
		var supplies: Dictionary = fortified_scene._camp_conversation_by_id(XIAN_FORTIFIED_CONVERSATION_ID)
		if supplies.is_empty():
			failures.append("fortified flag should expose Xian fortified road supplies")
		if not fortified_scene._camp_conversation_by_id(XIAN_PRESSED_CONVERSATION_ID).is_empty():
			failures.append("fortified flag should not expose Xian forced march canteens")
		if not supplies.is_empty():
			if fortified_scene._format_talk_status_text(fortified_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Xian fortified talk status should show one ready supply")
			if not fortified_scene._format_camp_conversation_button_text(supplies).contains("Supply Bean"):
				failures.append("Xian fortified road supplies button should preview Bean supply")
			var before_bean := int(fortified_scene.campaign_state.get_inventory_snapshot().get("bean", 0))
			fortified_scene._apply_camp_conversation_effects(supplies)
			if int(fortified_scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Xian fortified road supplies should add Bean to campaign inventory")
			if int(fortified_scene.state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Xian fortified road supplies should refresh battle Bean inventory")
			if not fortified_scene.campaign_state.has_claimed_camp_conversation_effects(XIAN_SCENARIO_ID, XIAN_FORTIFIED_CONVERSATION_ID):
				failures.append("Xian fortified road supplies claim should be saved")
			if fortified_scene._format_talk_status_text(fortified_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Xian fortified talk status should show claimed supply after claim")
		fortified_scene.free()

	var pressed_scene = _new_prebattle_scene_for(
		failures,
		"Xian escort pressed camp",
		XIAN_SCENARIO_ID,
		XIAN_SCENARIO_PATH,
		{"pressed_lu_bu": true}
	)
	if pressed_scene != null:
		var canteens: Dictionary = pressed_scene._camp_conversation_by_id(XIAN_PRESSED_CONVERSATION_ID)
		if canteens.is_empty():
			failures.append("pressed flag should expose Xian forced march canteens")
		if not pressed_scene._camp_conversation_by_id(XIAN_FORTIFIED_CONVERSATION_ID).is_empty():
			failures.append("pressed flag should not expose Xian fortified road supplies")
		if not canteens.is_empty():
			if pressed_scene._format_talk_status_text(pressed_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Xian pressed talk status should show one ready supply")
			if not pressed_scene._format_camp_conversation_button_text(canteens).contains("Supply Wine"):
				failures.append("Xian forced march canteens button should preview Wine supply")
			var before_wine := int(pressed_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
			pressed_scene._apply_camp_conversation_effects(canteens)
			if int(pressed_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Xian forced march canteens should add Wine to campaign inventory")
			if int(pressed_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Xian forced march canteens should refresh battle Wine inventory")
			if not pressed_scene.campaign_state.has_claimed_camp_conversation_effects(XIAN_SCENARIO_ID, XIAN_PRESSED_CONVERSATION_ID):
				failures.append("Xian forced march canteens claim should be saved")
			if pressed_scene._format_talk_status_text(pressed_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Xian pressed talk status should show claimed supply after claim")
		pressed_scene.free()


func _check_wan_branch_camp_conversations(failures: Array[String]) -> void:
	var base_scene = _new_prebattle_scene_for(failures, "Wan Castle base camp", WAN_SCENARIO_ID, WAN_SCENARIO_PATH)
	if base_scene != null:
		if base_scene._camp_conversation_by_id("wan_escape_route_council").is_empty():
			failures.append("Wan Castle should expose the escape route council conversation")
		if base_scene._camp_conversation_by_id("wan_dian_wei_rear_guard").is_empty():
			failures.append("Wan Castle should expose Dian Wei's rear guard conversation")
		if not base_scene._camp_conversation_by_id(WAN_COURT_CONVERSATION_ID).is_empty():
			failures.append("Wan court seal supplies should be hidden without court flag")
		if not base_scene._camp_conversation_by_id(WAN_PURSUIT_CONVERSATION_ID).is_empty():
			failures.append("Wan hard pursuit supplies should be hidden without pursuit flag")
		var merchant: Dictionary = base_scene.state.get_shop_merchant()
		if str(merchant.get("name", "")) != "Wan Castle Sutler":
			failures.append("Wan Castle shop merchant name should be present")
		var merchant_lines: Array = merchant.get("lines", [])
		if merchant_lines.size() < 2:
			failures.append("Wan Castle shop merchant should expose flavor lines")
		if base_scene._format_talk_status_text(base_scene._camp_conversation_entries()) != "Camp conversations | 2 topics":
			failures.append("Wan Castle base talk status should summarize two non-supply topics")
		base_scene.free()

	var court_scene = _new_prebattle_scene_for(
		failures,
		"Wan Castle court seal camp",
		WAN_SCENARIO_ID,
		WAN_SCENARIO_PATH,
		{"secured_imperial_court": true}
	)
	if court_scene != null:
		var postern: Dictionary = court_scene._camp_conversation_by_id(WAN_COURT_CONVERSATION_ID)
		if postern.is_empty():
			failures.append("court seal flag should expose Wan postern supplies")
		if not court_scene._camp_conversation_by_id(WAN_PURSUIT_CONVERSATION_ID).is_empty():
			failures.append("court seal flag should not expose Wan hard pursuit drums")
		if not postern.is_empty():
			if court_scene._format_talk_status_text(court_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Wan court seal talk status should show one ready supply")
			if not court_scene._format_camp_conversation_button_text(postern).contains("Supply Bean"):
				failures.append("Wan court seal postern button should preview Bean supply")
			var before_bean := int(court_scene.campaign_state.get_inventory_snapshot().get("bean", 0))
			court_scene._apply_camp_conversation_effects(postern)
			if int(court_scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Wan court seal postern should add Bean to campaign inventory")
			if int(court_scene.state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Wan court seal postern should refresh battle Bean inventory")
			if not court_scene.campaign_state.has_claimed_camp_conversation_effects(WAN_SCENARIO_ID, WAN_COURT_CONVERSATION_ID):
				failures.append("Wan court seal postern claim should be saved")
			if court_scene._format_talk_status_text(court_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Wan court seal talk status should show claimed supply after claim")
		court_scene.free()

	var pursuit_scene = _new_prebattle_scene_for(
		failures,
		"Wan Castle hard pursuit camp",
		WAN_SCENARIO_ID,
		WAN_SCENARIO_PATH,
		{"pursued_li_jue_remnants": true}
	)
	if pursuit_scene != null:
		var drums: Dictionary = pursuit_scene._camp_conversation_by_id(WAN_PURSUIT_CONVERSATION_ID)
		if drums.is_empty():
			failures.append("hard pursuit flag should expose Wan hard pursuit drums")
		if not pursuit_scene._camp_conversation_by_id(WAN_COURT_CONVERSATION_ID).is_empty():
			failures.append("hard pursuit flag should not expose Wan postern supplies")
		if not drums.is_empty():
			if pursuit_scene._format_talk_status_text(pursuit_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Wan hard pursuit talk status should show one ready supply")
			if not pursuit_scene._format_camp_conversation_button_text(drums).contains("Supply Wine"):
				failures.append("Wan hard pursuit drums button should preview Wine supply")
			var before_wine := int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
			pursuit_scene._apply_camp_conversation_effects(drums)
			if int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Wan hard pursuit drums should add Wine to campaign inventory")
			if int(pursuit_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Wan hard pursuit drums should refresh battle Wine inventory")
			if not pursuit_scene.campaign_state.has_claimed_camp_conversation_effects(WAN_SCENARIO_ID, WAN_PURSUIT_CONVERSATION_ID):
				failures.append("Wan hard pursuit drums claim should be saved")
			if pursuit_scene._format_talk_status_text(pursuit_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Wan hard pursuit talk status should show claimed supply after claim")
		pursuit_scene.free()


func _check_manual_checkpoint_reverts_claim(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene(failures, "manual checkpoint")
	if scene == null:
		return
	if not scene.campaign_state.save_manual_game():
		failures.append("manual checkpoint before camp claim should save")
		scene.free()
		return
	var conversation: Dictionary = scene._camp_conversation_by_id(CONVERSATION_ID)
	scene._apply_camp_conversation_effects(conversation)
	if int(scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != 1:
		failures.append("camp claim should add Bean before manual restore")
	if not scene.campaign_state.load_manual_game():
		failures.append("manual checkpoint should load after camp claim")
	else:
		if int(scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != 0:
			failures.append("loading older manual checkpoint should revert camp reward inventory")
		if scene.campaign_state.has_claimed_camp_conversation_effects(SCENARIO_ID, CONVERSATION_ID):
			failures.append("loading older manual checkpoint should revert camp claim ledger")
	scene.free()


func _check_completed_replay_cannot_claim(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene(failures, "completed replay")
	if scene == null:
		return
	scene.campaign_state.completed_scenarios.append(SCENARIO_ID)
	var conversation: Dictionary = scene._camp_conversation_by_id(CONVERSATION_ID)
	scene._apply_camp_conversation_effects(conversation)
	if int(scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != 0:
		failures.append("completed replay should not claim camp supplies")
	if scene.campaign_state.has_claimed_camp_conversation_effects(SCENARIO_ID, CONVERSATION_ID):
		failures.append("completed replay should not write camp claim ledger")
	scene.free()


func _check_conditional_camp_conversations(failures: Array[String]) -> void:
	var no_flags_scene = _new_prebattle_scene_for(failures, "conditional camp no flags", BRANCH_SCENARIO_ID, BRANCH_SCENARIO_PATH)
	if no_flags_scene != null:
		if no_flags_scene._camp_conversation_by_id("xingyang_war_council").is_empty():
			failures.append("Xingyang should expose ungated war council conversation")
		if not no_flags_scene._camp_conversation_by_id("xingyang_forced_march").is_empty():
			failures.append("pursuit camp conversation should be hidden without pursuit flag")
		if not no_flags_scene._camp_conversation_by_id("xingyang_scout_screen").is_empty():
			failures.append("regroup camp conversation should be hidden without regroup flag")
		no_flags_scene.free()

	var pursuit_scene = _new_prebattle_scene_for(
		failures,
		"conditional camp pursuit",
		BRANCH_SCENARIO_ID,
		BRANCH_SCENARIO_PATH,
		{"pursued_dong_zhuo": true}
	)
	if pursuit_scene != null:
		if pursuit_scene._camp_conversation_by_id("xingyang_forced_march").is_empty():
			failures.append("pursuit flag should expose forced march conversation")
		if pursuit_scene._camp_conversation_by_id("xingyang_hasty_stores").is_empty():
			failures.append("pursuit flag should expose hasty stores conversation")
		if not pursuit_scene._camp_conversation_by_id("xingyang_scout_screen").is_empty():
			failures.append("pursuit flag should not expose regroup scout conversation")
		var hasty_stores: Dictionary = pursuit_scene._camp_conversation_by_id("xingyang_hasty_stores")
		var before_wine := int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
		pursuit_scene._apply_camp_conversation_effects(hasty_stores)
		if int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
			failures.append("pursuit hasty stores should add Wine to campaign inventory")
		if int(pursuit_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
			failures.append("pursuit hasty stores should refresh battle Wine inventory")
		pursuit_scene.free()

	var regroup_scene = _new_prebattle_scene_for(
		failures,
		"conditional camp regroup",
		BRANCH_SCENARIO_ID,
		BRANCH_SCENARIO_PATH,
		{"regrouped_after_sishui": true}
	)
	if regroup_scene != null:
		if regroup_scene._camp_conversation_by_id("xingyang_scout_screen").is_empty():
			failures.append("regroup flag should expose scout screen conversation")
		if regroup_scene._camp_conversation_by_id("xingyang_sishui_supply_train").is_empty():
			failures.append("regroup flag should expose Sishui supply train conversation")
		if not regroup_scene._camp_conversation_by_id("xingyang_forced_march").is_empty():
			failures.append("regroup flag should not expose pursuit forced march conversation")
		var supply_train: Dictionary = regroup_scene._camp_conversation_by_id("xingyang_sishui_supply_train")
		var before_panacea := int(regroup_scene.campaign_state.get_inventory_snapshot().get("panacea", 0))
		regroup_scene._apply_camp_conversation_effects(supply_train)
		if int(regroup_scene.campaign_state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
			failures.append("regroup Sishui supply train should add Panacea to campaign inventory")
		if int(regroup_scene.state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
			failures.append("regroup Sishui supply train should refresh battle Panacea inventory")
		regroup_scene.free()


func _check_qingzhou_branch_camp_conversations(failures: Array[String]) -> void:
	var base_scene = _new_prebattle_scene_for(failures, "Qingzhou base camp", QINGZHOU_SCENARIO_ID, QINGZHOU_SCENARIO_PATH)
	if base_scene != null:
		if base_scene._camp_conversation_by_id("qingzhou_settlement_council").is_empty():
			failures.append("Qingzhou should expose lasting army officer conversation")
		if base_scene._camp_conversation_by_id("qingzhou_village_screen").is_empty():
			failures.append("Qingzhou should expose village screen officer conversation")
		if not base_scene._camp_conversation_by_id("qingzhou_hardened_veterans").is_empty():
			failures.append("Qingzhou pursuit supplies should be hidden without pursuit flag")
		if not base_scene._camp_conversation_by_id("qingzhou_sishui_stores").is_empty():
			failures.append("Qingzhou Sishui stores should be hidden without regroup flag")
		var merchant: Dictionary = base_scene.state.get_shop_merchant()
		if str(merchant.get("name", "")) != "Qingzhou Road Merchant":
			failures.append("Qingzhou shop merchant name should be present")
		var merchant_lines: Array = merchant.get("lines", [])
		if merchant_lines.size() < 2:
			failures.append("Qingzhou shop merchant should expose flavor lines")
		if base_scene._format_talk_status_text(base_scene._camp_conversation_entries()) != "Camp conversations | 2 topics":
			failures.append("Qingzhou base talk status should summarize two non-supply topics")
		base_scene.free()

	var pursuit_scene = _new_prebattle_scene_for(
		failures,
		"Qingzhou pursuit camp",
		QINGZHOU_SCENARIO_ID,
		QINGZHOU_SCENARIO_PATH,
		{"pursued_dong_zhuo": true}
	)
	if pursuit_scene != null:
		var veterans: Dictionary = pursuit_scene._camp_conversation_by_id("qingzhou_hardened_veterans")
		if veterans.is_empty():
			failures.append("pursuit flag should expose Qingzhou hardened veterans")
		if not pursuit_scene._camp_conversation_by_id("qingzhou_sishui_stores").is_empty():
			failures.append("pursuit flag should not expose Qingzhou Sishui stores")
		if not veterans.is_empty():
			if pursuit_scene._format_talk_status_text(pursuit_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Qingzhou pursuit talk status should show one ready supply")
			if not pursuit_scene._format_camp_conversation_button_text(veterans).contains("Supply Wine"):
				failures.append("Qingzhou hardened veterans button should show Wine supply")
			var before_wine := int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0))
			pursuit_scene._apply_camp_conversation_effects(veterans)
			if int(pursuit_scene.campaign_state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Qingzhou hardened veterans should add Wine to campaign inventory")
			if int(pursuit_scene.state.get_inventory_snapshot().get("wine", 0)) != before_wine + 1:
				failures.append("Qingzhou hardened veterans should refresh battle Wine inventory")
			if not pursuit_scene.campaign_state.has_claimed_camp_conversation_effects(QINGZHOU_SCENARIO_ID, "qingzhou_hardened_veterans"):
				failures.append("Qingzhou hardened veterans claim should be saved")
			if pursuit_scene._format_talk_status_text(pursuit_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Qingzhou pursuit talk status should show claimed supply after claim")
		pursuit_scene.free()

	var regroup_scene = _new_prebattle_scene_for(
		failures,
		"Qingzhou regroup camp",
		QINGZHOU_SCENARIO_ID,
		QINGZHOU_SCENARIO_PATH,
		{"regrouped_after_sishui": true}
	)
	if regroup_scene != null:
		var stores: Dictionary = regroup_scene._camp_conversation_by_id("qingzhou_sishui_stores")
		if stores.is_empty():
			failures.append("regroup flag should expose Qingzhou Sishui stores")
		if not regroup_scene._camp_conversation_by_id("qingzhou_hardened_veterans").is_empty():
			failures.append("regroup flag should not expose Qingzhou hardened veterans")
		if not stores.is_empty():
			if regroup_scene._format_talk_status_text(regroup_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Qingzhou regroup talk status should show one ready supply")
			if not regroup_scene._format_camp_conversation_button_text(stores).contains("Supply Bean"):
				failures.append("Qingzhou Sishui stores button should show Bean supply")
			var before_bean := int(regroup_scene.campaign_state.get_inventory_snapshot().get("bean", 0))
			regroup_scene._apply_camp_conversation_effects(stores)
			if int(regroup_scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Qingzhou Sishui stores should add Bean to campaign inventory")
			if int(regroup_scene.state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Qingzhou Sishui stores should refresh battle Bean inventory")
			if not regroup_scene.campaign_state.has_claimed_camp_conversation_effects(QINGZHOU_SCENARIO_ID, "qingzhou_sishui_stores"):
				failures.append("Qingzhou Sishui stores claim should be saved")
			if regroup_scene._format_talk_status_text(regroup_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Qingzhou regroup talk status should show claimed supply after claim")
		regroup_scene.free()


func _check_wuchao_branch_camp_conversations(failures: Array[String]) -> void:
	var base_scene = _new_prebattle_scene_for(failures, "Wuchao base camp", WUCHAO_SCENARIO_ID, WUCHAO_SCENARIO_PATH)
	if base_scene != null:
		if base_scene._camp_conversation_by_id("wuchao_night_raid_council").is_empty():
			failures.append("Wuchao should expose the night raid council conversation")
		if base_scene._camp_conversation_by_id("wuchao_guo_jia_empty_bowl").is_empty():
			failures.append("Wuchao should expose Guo Jia's officer conversation")
		if not base_scene._camp_conversation_by_id("wuchao_fast_raid_supplies").is_empty():
			failures.append("Wuchao fast raid supplies should be hidden without the prepared raid flag")
		if not base_scene._camp_conversation_by_id("wuchao_guandu_reserve_wagon").is_empty():
			failures.append("Wuchao reserve wagon should be hidden without the strengthened defense flag")
		var merchant: Dictionary = base_scene.state.get_shop_merchant()
		if str(merchant.get("name", "")) != "Wuchao Night Trader":
			failures.append("Wuchao shop merchant name should be present")
		var merchant_lines: Array = merchant.get("lines", [])
		if merchant_lines.size() < 2:
			failures.append("Wuchao shop merchant should expose flavor lines")
		if base_scene._format_talk_status_text(base_scene._camp_conversation_entries()) != "Camp conversations | 2 topics":
			failures.append("Wuchao base talk status should summarize two non-supply topics")
		base_scene.free()

	var raid_scene = _new_prebattle_scene_for(
		failures,
		"Wuchao prepared raid camp",
		WUCHAO_SCENARIO_ID,
		WUCHAO_SCENARIO_PATH,
		{"prepared_wuchao_raid": true}
	)
	if raid_scene != null:
		var fast_supplies: Dictionary = raid_scene._camp_conversation_by_id("wuchao_fast_raid_supplies")
		if fast_supplies.is_empty():
			failures.append("prepared raid flag should expose Wuchao fast supplies")
		if not raid_scene._camp_conversation_by_id("wuchao_guandu_reserve_wagon").is_empty():
			failures.append("prepared raid flag should not expose Wuchao reserve wagon")
		if not fast_supplies.is_empty():
			if raid_scene._format_talk_status_text(raid_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Wuchao prepared raid talk status should show one ready supply")
			if not raid_scene._format_camp_conversation_button_text(fast_supplies).contains("Supply Bean"):
				failures.append("Wuchao fast supplies button should show Bean supply")
			var before_bean := int(raid_scene.campaign_state.get_inventory_snapshot().get("bean", 0))
			raid_scene._apply_camp_conversation_effects(fast_supplies)
			if int(raid_scene.campaign_state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Wuchao fast supplies should add Bean to campaign inventory")
			if int(raid_scene.state.get_inventory_snapshot().get("bean", 0)) != before_bean + 1:
				failures.append("Wuchao fast supplies should refresh battle Bean inventory")
			if not raid_scene.campaign_state.has_claimed_camp_conversation_effects(WUCHAO_SCENARIO_ID, "wuchao_fast_raid_supplies"):
				failures.append("Wuchao fast supplies claim should be saved")
			if raid_scene._format_talk_status_text(raid_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 claimed":
				failures.append("Wuchao prepared raid talk status should show claimed supply after claim")
		raid_scene.free()

	var defense_scene = _new_prebattle_scene_for(
		failures,
		"Wuchao strengthened defense camp",
		WUCHAO_SCENARIO_ID,
		WUCHAO_SCENARIO_PATH,
		{"strengthened_guandu_defense": true}
	)
	if defense_scene != null:
		var reserve_wagon: Dictionary = defense_scene._camp_conversation_by_id("wuchao_guandu_reserve_wagon")
		if reserve_wagon.is_empty():
			failures.append("strengthened defense flag should expose Wuchao reserve wagon")
		if not defense_scene._camp_conversation_by_id("wuchao_fast_raid_supplies").is_empty():
			failures.append("strengthened defense flag should not expose Wuchao fast supplies")
		if not reserve_wagon.is_empty():
			if defense_scene._format_talk_status_text(defense_scene._camp_conversation_entries()) != "Camp conversations | 3 topics | 1 supplies ready":
				failures.append("Wuchao strengthened defense talk status should show one ready supply")
			if not defense_scene._format_camp_conversation_button_text(reserve_wagon).contains("Supply Panacea"):
				failures.append("Wuchao reserve wagon button should show Panacea supply")
			var before_panacea := int(defense_scene.campaign_state.get_inventory_snapshot().get("panacea", 0))
			defense_scene._apply_camp_conversation_effects(reserve_wagon)
			if int(defense_scene.campaign_state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Wuchao reserve wagon should add Panacea to campaign inventory")
			if int(defense_scene.state.get_inventory_snapshot().get("panacea", 0)) != before_panacea + 1:
				failures.append("Wuchao reserve wagon should refresh battle Panacea inventory")
			if not defense_scene.campaign_state.has_claimed_camp_conversation_effects(WUCHAO_SCENARIO_ID, "wuchao_guandu_reserve_wagon"):
				failures.append("Wuchao reserve wagon claim should be saved")
		defense_scene.free()


func _check_talk_menu_closes_on_cancel_and_reload(failures: Array[String]) -> void:
	var scene = _new_prebattle_scene(failures, "talk menu close guards")
	if scene == null:
		return
	scene._create_hud()
	scene.briefing_panel.visible = true
	scene.talk_menu.visible = true
	var cancel_event := InputEventKey.new()
	cancel_event.keycode = KEY_ESCAPE
	scene._handle_key(cancel_event)
	if scene.talk_menu.visible:
		failures.append("Escape should close the pre-battle talk menu")
	scene.talk_menu.visible = true
	scene._reload_from_campaign_state()
	if scene.talk_menu.visible:
		failures.append("campaign reload should hide the pre-battle talk menu")
	scene.free()


func _new_prebattle_scene(failures: Array[String], label: String):
	return _new_prebattle_scene_for(failures, label, SCENARIO_ID, SCENARIO_PATH)


func _new_prebattle_scene_for(failures: Array[String], label: String, scenario_id: String, scenario_path: String, flag_overrides := {}):
	var scene = BattleSceneScript.new()
	if not scene.campaign_state.load_campaign(CAMPAIGN_PATH):
		failures.append("%s could not load campaign data" % label)
		scene.free()
		return null
	scene.campaign_state.start_new(scenario_id)
	if typeof(flag_overrides) == TYPE_DICTIONARY:
		for flag_name in flag_overrides.keys():
			scene.campaign_state.flags[str(flag_name)] = flag_overrides[flag_name]
	scene.active_scenario_id = scenario_id
	if not scene.state.load_battle(
		scenario_path,
		scene.campaign_state.get_roster_overrides(),
		scene.campaign_state.get_inventory_snapshot(),
		scene.campaign_state.get_flags_snapshot(),
		scene.campaign_state.get_joined_officers_snapshot()
	):
		failures.append("%s could not load scenario" % label)
		scene.free()
		return null
	return scene


func _check_cache_conversation(failures: Array[String], conversation: Dictionary) -> bool:
	if conversation.is_empty():
		failures.append("could not find cache camp conversation")
		return false
	var effects: Array = conversation.get("effects", [])
	if effects.is_empty():
		failures.append("cache camp conversation should expose effects")
		return false
	var effect: Dictionary = effects[0]
	if str(effect.get("type", "")) != "grant_item":
		failures.append("cache camp conversation effect should be grant_item")
	if str(effect.get("item_id", "")) != "bean":
		failures.append("cache camp conversation should grant Bean")
	if int(effect.get("count", 0)) != 1:
		failures.append("cache camp conversation should grant one Bean")
	return true


func _backup_user_file(path: String) -> Dictionary:
	var exists := FileAccess.file_exists(path)
	return {
		"exists": exists,
		"content": FileAccess.get_file_as_string(path) if exists else ""
	}


func _restore_user_file(path: String, backup: Dictionary) -> bool:
	if bool(backup.get("exists", false)):
		var file := FileAccess.open(path, FileAccess.WRITE)
		if file == null:
			return false
		file.store_string(str(backup.get("content", "")))
		return true
	if FileAccess.file_exists(path):
		return DirAccess.remove_absolute(ProjectSettings.globalize_path(path)) == OK
	return true
