extends SceneTree

# Check LightmapGI config on kit_demo.tscn scene.
# No bake possible at runtime in Godot 4.7 — LightmapGI baking is editor-only.
# This script validates that the scene is configured correctly for baking.
# 
# Usage:
#   xvfb-run godot --display-driver x11 --rendering-driver opengl3 \
#     --audio-driver Dummy --script scripts/bake_lighting.gd
# 
# Bake from editor:
#   Open kit_demo.tscn in Godot editor, select LightmapGI node,
#   click "Bake Lightmap" in the Inspector toolbar.

const SCENE_PATH := "res://assets/scenes/modular/kit_demo.tscn"

func _init():
	print("=== LightmapGI Config Check ===")
	print("Note: Godot 4.7 LightmapGI baking is editor-only.")
	print("This script validates configuration only.")
	print("")
	
	var scene = ResourceLoader.load(SCENE_PATH)
	if not scene:
		printerr("ERROR: Failed to load scene!")
		quit(1)
		return
	
	var instance = scene.instantiate()
	root.add_child(instance)
	
	var lightmap = _find_node(instance, "LightmapGI")
	if not lightmap or not (lightmap is LightmapGI):
		printerr("ERROR: LightmapGI node not found!")
		quit(1)
		return
	
	print("LightmapGI configuration:")
	match lightmap.quality:
		0: print("  Quality: Low")
		1: print("  Quality: Medium")
		2: print("  Quality: High")
		3: print("  Quality: Ultra")
	print("  Bounces: ", lightmap.bounces)
	print("  Texel Scale: ", lightmap.texel_scale)
	print("  Max Texture Size: ", lightmap.max_texture_size)
	print("  Interior: ", lightmap.interior)
	print("  Denoiser: ", lightmap.is_using_denoiser())
	print("")
	
	if lightmap.light_data != null:
		print("LightmapGI has baked data!")
	else:
		print("LightmapGI NOT YET BAKED.")
		print("To bake: Open scene in Godot editor > Select LightmapGI >")
		print("click 'Bake Lightmap' button in the Inspector toolbar.")
		print("")
		print("Or use the Godot editor menu: Scene > Bake LightmapGI")
	
	print("")
	print("=== Config check complete ===")
	quit(0)


func _find_node(parent: Node, name: String) -> Node:
	for child in parent.get_children():
		if child.name == name:
			return child
		var found = _find_node(child, name)
		if found:
			return found
	return null
