extends SceneTree


func _init() -> void:
	var paths_to_check: Array[String] = [
		# Mini characters (для отца + других NPC)
		"res://assets/models/characters/character-male-a.glb",
		"res://assets/models/characters/character-male-b.glb",
		"res://assets/models/characters/character-male-c.glb",
		"res://assets/models/characters/character-male-d.glb",
		"res://assets/models/characters/character-male-e.glb",
		"res://assets/models/characters/character-male-f.glb",
		"res://assets/models/characters/character-female-a.glb",
		# Furniture
		"res://assets/models/furniture/bedSingle.glb",
		"res://assets/models/furniture/desk.glb",
		"res://assets/models/furniture/chairDesk.glb",
		"res://assets/models/furniture/chair.glb",
		"res://assets/models/furniture/computerScreen.glb",
		"res://assets/models/furniture/computerKeyboard.glb",
		"res://assets/models/furniture/bookcaseClosedDoors.glb",
		"res://assets/models/furniture/cardboardBoxClosed.glb",
		"res://assets/models/furniture/kitchenFridgeLarge.glb",
		"res://assets/models/furniture/kitchenStove.glb",
		"res://assets/models/furniture/kitchenSink.glb",
		"res://assets/models/furniture/table.glb",
	]

	print("=== MODEL INSPECTION ===")
	for path in paths_to_check:
		var scene: PackedScene = load(path)
		if scene == null:
			print("FAIL load: %s" % path)
			continue
		var instance: Node = scene.instantiate()
		if instance == null:
			print("FAIL instantiate: %s" % path)
			continue
		var aabb: AABB = _get_total_aabb(instance)
		print("--- %s ---" % path.get_file())
		print("  size: x=%.3f y=%.3f z=%.3f" % [aabb.size.x, aabb.size.y, aabb.size.z])
		print("  pos: x=%.3f y=%.3f z=%.3f" % [aabb.position.x, aabb.position.y, aabb.position.z])
		var ap: AnimationPlayer = _find_anim_player(instance)
		if ap != null:
			var anims: PackedStringArray = ap.get_animation_list()
			print("  animations (%d total):" % anims.size())
			for name in anims:
				print("    - %s" % name)
		instance.queue_free()
	print("=== END ===")
	quit()


func _find_anim_player(node: Node) -> AnimationPlayer:
	if node is AnimationPlayer:
		return node as AnimationPlayer
	for child in node.get_children():
		var found: AnimationPlayer = _find_anim_player(child)
		if found != null:
			return found
	return null


func _get_total_aabb(node: Node) -> AABB:
	var bounds: AABB = AABB()
	var first: bool = true
	for mesh_node in _find_all_meshes(node):
		var aabb: AABB = mesh_node.get_aabb()
		if first:
			bounds = aabb
			first = false
		else:
			bounds = bounds.merge(aabb)
	return bounds


func _find_all_meshes(node: Node) -> Array:
	var result: Array = []
	if node is MeshInstance3D:
		result.append(node)
	for child in node.get_children():
		result.append_array(_find_all_meshes(child))
	return result
