extends SceneTree

## Headless M01 contract and wiring regression for camera-independent streaming.

const StreamingFocusScript = preload("res://src/domain/streaming/streaming_focus.gd")
const GodotWorldPositionScript = preload("res://src/domain/coordinates/godot_world_position.gd")

const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"
const FACADE_PATH := "res://src/render/world_render_facade.gd"
const RUNTIME_SCENE_PATHS: Array[String] = [
	"res://src/scenes/streaming/eastern_kingdoms_streaming.tscn",
	"res://src/scenes/streaming/kalimdor_streaming.tscn",
]
const CAPTURE_TOOL_PATHS: Array[String] = [
	"res://src/tools/capture_render_checkpoints.gd",
	"res://src/tools/probe_render_camera_occluders.gd",
	"res://src/tools/probe_render_terrain_height.gd",
]


func _initialize() -> void:
	var failures: Array[String] = []
	_verify_scene_free_focus_value(failures)
	_verify_loader_boundary(failures)
	_verify_facade_boundary(failures)
	_verify_runtime_scene_wiring(failures)
	_verify_capture_tool_wiring(failures)

	if not failures.is_empty():
		for failure in failures:
			push_error("STREAMING_FOCUS: %s" % failure)
		quit(1)
		return

	print("STREAMING_FOCUS PASS contract=1 runtime_scenes=2 capture_tools=3")
	quit(0)


func _verify_scene_free_focus_value(failures: Array[String]) -> void:
	var position = GodotWorldPositionScript.new(17199.159666667, 83.5312, 26016.616666667)
	var focus = StreamingFocusScript.new(position)
	_expect_true(focus.world_position == position, "focus retains typed position", failures)
	_expect_near(focus.world_position.x_units, 17199.159666667, "focus X", failures)
	_expect_near(focus.world_position.y_units, 83.5312, "focus Y", failures)
	_expect_near(focus.world_position.z_units, 26016.616666667, "focus Z", failures)

	var focus_source := _read_text("res://src/domain/streaming/streaming_focus.gd", failures)
	_expect_true(not focus_source.contains("extends Node"), "focus does not inherit Node", failures)
	_expect_true(not focus_source.contains("extends Resource"), "focus does not inherit Resource", failures)
	_expect_true(not focus_source.contains("Vector3"), "focus does not expose Vector3", failures)


func _verify_loader_boundary(failures: Array[String]) -> void:
	var loader_source := _read_text(LOADER_PATH, failures)
	for required_text in [
		"@export var streaming_focus_source_path: NodePath",
		"func set_streaming_focus(streaming_focus: StreamingFocus) -> void:",
		"func refresh_streaming_focus(force: bool = false) -> bool:",
	]:
		_expect_true(loader_source.contains(required_text), "loader contains %s" % required_text, failures)

	for forbidden_text in [
		"get_viewport().get_camera_3d()",
		"child is Camera3D and child.current",
		"func _get_stream_camera()",
	]:
		_expect_true(not loader_source.contains(forbidden_text), "loader omits %s" % forbidden_text, failures)


func _verify_facade_boundary(failures: Array[String]) -> void:
	var facade_source := _read_text(FACADE_PATH, failures)
	for required_text in [
		"class_name WorldRenderFacade",
		"func set_streaming_focus(streaming_focus: StreamingFocus) -> void:",
		"func refresh_streaming_focus(force: bool = false) -> bool:",
	]:
		_expect_true(facade_source.contains(required_text), "facade contains %s" % required_text, failures)


func _verify_runtime_scene_wiring(failures: Array[String]) -> void:
	for scene_path in RUNTIME_SCENE_PATHS:
		var scene_source := _read_text(scene_path, failures)
		_expect_true(
			scene_source.contains('streaming_focus_source_path = NodePath("../ThirdPersonPlayer")'),
			"%s uses player focus" % scene_path,
			failures
		)


func _verify_capture_tool_wiring(failures: Array[String]) -> void:
	for tool_path in CAPTURE_TOOL_PATHS:
		var tool_source := _read_text(tool_path, failures)
		_expect_true(tool_source.contains('render_facade.set("streaming_focus_source_path"'), "%s sets explicit focus source" % tool_path, failures)
		_expect_true(tool_source.contains('render_facade.call("refresh_streaming_focus", true)'), "%s uses facade refresh" % tool_path, failures)
		_expect_true(not tool_source.contains('world.call("_refresh_streaming_targets_at"'), "%s avoids private refresh" % tool_path, failures)


func _read_text(path: String, failures: Array[String]) -> String:
	var file := FileAccess.open(path, FileAccess.READ)
	if file == null:
		failures.append("cannot open %s" % path)
		return ""
	return file.get_as_text()


func _expect_near(actual_value: float, expected_value: float, label: String, failures: Array[String]) -> void:
	if absf(actual_value - expected_value) > 0.000001:
		failures.append("%s expected %.9f, got %.9f" % [label, expected_value, actual_value])


func _expect_true(actual_value: bool, label: String, failures: Array[String]) -> void:
	if not actual_value:
		failures.append("%s expected true" % label)
