extends SceneTree

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


func _init() -> void:
	var failures: Array[String] = []
	var scene = BattleSceneScript.new()
	if not scene.state.load_battle("res://data/scenarios/001_yellow_turbans.json"):
		push_error("Could not load smoke scenario.")
		quit(1)
		return
	scene.campaign_state.gold = 320

	scene.battle_result_summary = {
		"saved": true,
		"rewards_applied": true,
		"already_completed": false,
		"pending_choice": false,
		"gold": 100,
		"items": ["bean", "bean"],
		"joined_officers": ["dian_wei"],
		"left_officers": [],
		"progression_events": [
			{"type": "level_up", "name": "Cao Cao", "to_level": 2}
		],
		"next_scenario_title": "Sishui Gate",
		"choice_applied": true,
		"choice_label": ""
	}
	var result_text: String = scene._format_battle_result_summary()
	_check_contains(failures, "reward section", result_text, "Rewards")
	_check_contains(failures, "gold reward", result_text, "Gold +100")
	_check_contains(failures, "item reward", result_text, "Bean x2")
	_check_contains(failures, "roster section", result_text, "Joined: Dian Wei")
	_check_contains(failures, "growth section", result_text, "Cao Cao Lv.2")
	_check_contains(failures, "campaign section", result_text, "Next: Sishui Gate")

	scene.battle_result_summary = {
		"saved": false,
		"rewards_applied": false,
		"already_completed": false,
		"pending_choice": false,
		"gold": 0,
		"items": [],
		"joined_officers": [],
		"left_officers": [],
		"progression_events": [],
		"next_scenario_title": "Sishui Gate",
		"choice_applied": true,
		"choice_label": ""
	}
	var failed_save_text: String = scene._format_battle_result_summary()
	_check_contains(failures, "failed rewards", failed_save_text, "Not applied")
	_check_contains(failures, "failed save", failed_save_text, "Campaign save failed")

	scene.battle_result_summary = {
		"saved": true,
		"pending_choice": true,
		"next_scenario_title": "Qingzhou Campaign"
	}
	_check_contains(
		failures,
		"pending choice prompt",
		scene._format_battle_result_summary(),
		"Select a campaign response"
	)

	scene.battle_result_summary = {
		"saved": true,
		"already_completed": true,
		"next_scenario_title": "Current Battle"
	}
	_check_contains(
		failures,
		"replay rewards",
		scene._format_battle_result_summary(),
		"Already claimed"
	)

	var choice_text: String = scene._format_post_battle_choice_reward_text({
		"gold": 50,
		"items": ["war_axe"],
		"join_officers": ["dian_wei"]
	})
	_check_contains(failures, "choice gold", choice_text, "+50G")
	_check_contains(failures, "choice item", choice_text, "War Axe")
	_check_contains(failures, "choice join", choice_text, "Join Dian Wei")

	if failures.is_empty():
		scene.free()
		print("result summary smoke ok")
		quit(0)
		return

	scene.free()
	for failure in failures:
		push_error(failure)
	quit(1)


func _check_contains(failures: Array[String], label: String, text: String, expected: String) -> void:
	if text.contains(expected):
		return
	failures.append("%s expected `%s` in `%s`" % [label, expected, text])
