extends SceneTree

# Validate kit_demo.tscn scene structure.
# Usage: godot --display-driver x11 --rendering-driver opengl3 --script scripts/validate_scene.gd

func _init():
	print("=== Validating kit_demo.tscn ===")
	
	var scene = ResourceLoader.load("res://assets/scenes/modular/kit_demo.tscn")
	if not scene:
		printerr("ERROR: Could not load kit_demo.tscn!")
		quit(1)
		return
	
	print("Scene loaded successfully.")
	
	var instance = scene.instantiate()
	if not instance:
		printerr("ERROR: Could not instantiate scene!")
		quit(1)
		return
	
	root.add_child(instance)
	print("Scene instantiated successfully.")
	print("")
	
	# Validate key nodes
	var checks = _check_nodes(instance)
	
	# Print results
	var passed = 0
	var failed = 0
	for check in checks:
		var status = "✓" if check[1] else "✗"
		if check[1]:
			passed += 1
		else:
			failed += 1
		print("  ", status, " ", check[0])
	
	# General info
	var node_count = _count_nodes(instance)
	print("")
	print("Scene stats:")
	print("  Total nodes:         ", node_count)
	print("  Checks passed:       ", passed)
	print("  Checks failed:       ", failed)
	print("")
	
	if failed > 0:
		printerr("Validation FAILED — ", failed, " check(s) failed.")
		quit(1)
	else:
		print("Validation PASSED.")
		quit(0)


func _check_nodes(root_node: Node) -> Array:
	var r = []
	
	# Built-in lighting nodes
	r.append(["WorldEnvironment (Environment+Sky)", root_node.find_child("WorldEnvironment", true, false) != null])
	r.append(["DirectionalLight3D (SunLight)", root_node.find_child("SunLight", true, false) != null])
	r.append(["OmniLight3D (FillLight)", root_node.find_child("FillLight", true, false) != null])
	r.append(["ReflectionProbe", root_node.find_child("ReflectionProbe", true, false) != null])
	r.append(["LightmapGI", root_node.find_child("LightmapGI", true, false) != null])
	
	# Lightmap config
	var lm = root_node.find_child("LightmapGI", true, false)
	if lm:
		r.append(["LightmapGI.bounces = 3", lm.bounces == 3])
		r.append(["LightmapGI.interior = true", lm.interior == true])
		r.append(["LightmapGI.use_denoiser = true", lm.use_denoiser == true])
		r.append(["LightmapGI.texel_scale = 1.0", abs(lm.texel_scale - 1.0) < 0.01])
	
	# Module pieces (floor, walls, pillar, beam, accent panels)
	r.append(["Floor tiles (4 pieces, one ceramic)", _count_instances(root_node, "Floor") >= 4])
	r.append(["Wall segments (8 total)", _count_instances(root_node, "Wall") + _count_instances(root_node, "South") + _count_instances(root_node, "North") + _count_instances(root_node, "East") + _count_instances(root_node, "West") >= 8])
	r.append(["Pillar (center)", root_node.find_child("Pillar", true, false) != null])
	r.append(["Beam (overhead)", root_node.find_child("Beam", true, false) != null])
	r.append(["AccentRed panel", root_node.find_child("AccentRed", true, false) != null])
	r.append(["AccentBlue panel", root_node.find_child("AccentBlue", true, false) != null])
	r.append(["Doorway segment", root_node.find_child("SouthDoorway", true, false) != null])
	r.append(["Window segment", root_node.find_child("NorthWindow", true, false) != null])
	
	return r


func _count_nodes(parent: Node) -> int:
	var count = 1
	for child in parent.get_children():
		count += _count_nodes(child)
	return count


func _count_instances(parent: Node, name_prefix: String) -> int:
	var count = 0
	for child in parent.get_children():
		if child.name.begins_with(name_prefix):
			count += 1
	return count
