extends Node

## VRAM/Texture Memory Measurement Tool
## Add this as an autoload to measure memory usage at startup

var measurements := {}
var start_time := 0.0

func _ready():
	start_time = Time.get_ticks_msec()
	
	# Initial measurement (before anything loads)
	_record_measurement("initial")
	
	# Wait for scene tree to be ready
	await get_tree().process_frame
	_record_measurement("after_tree_ready")
	
	# Wait for main scene to fully load
	await get_tree().process_frame
	_record_measurement("after_main_scene_loaded")
	
	# Give textures time to upload to GPU
	await get_tree().process_frame
	await get_tree().process_frame
	_record_measurement("after_gpu_upload")
	
	# Print final report
	await get_tree().process_frame
	_print_report()

func _record_measurement(label: String):
	var elapsed_ms = Time.get_ticks_msec() - start_time
	var tex_mem = Performance.get_monitor(Performance.VIEWER_TEXTURE_MEMORY)
	var obj_count = Performance.get_monitor(Performance.OBJECT_COUNT)
	var node_count = Performance.get_monitor(Performance.OBJECT_NODE_COUNT)
	
	measurements[label] = {
		"elapsed_ms": elapsed_ms,
		"texture_memory_bytes": tex_mem,
		"texture_memory_mb": tex_mem / (1024.0 * 1024.0),
		"object_count": obj_count,
		"node_count": node_count
	}
	
	print("[%.2f ms] %s: VRAM=%.2f MB, Objects=%d, Nodes=%d" % [
		elapsed_ms / 1000.0,
		label,
		tex_mem / (1024.0 * 1024.0),
		obj_count,
		node_count
	])

func _print_report():
	print("\n" + "=" * 60)
	print("VRAM USAGE REPORT")
	print("=" * 60)
	
	var total_elapsed = Time.get_ticks_msec() - start_time
	
	for label in measurements:
		var m = measurements[label]
		print("\n[%s]" % label)
		print("  Elapsed: %.3f s" % (m.elapsed_ms / 1000.0))
		print("  Texture Memory: %.2f MB (%.2f KB)" % [m.texture_memory_mb, m.texture_memory_bytes / 1024.0])
		print("  Object Count: %d" % m.object_count)
		print("  Node Count: %d" % m.node_count)
	
	print("\n" + "=" * 60)
	print("TOTAL STARTUP TIME: %.3f s" % (total_elapsed / 1000.0))
	print("=" * 60)
	
	# Save to file if we have write access
	var file_path = "user://vram_measurement_%s.txt" % Time.get_datetime_string_from_system(false, true).replace(":", "-")
	var file = FileAccess.open(file_path, FileAccess.WRITE)
	if file:
		file.store_string("VRAM Measurement Report\n")
		file.store_string("Generated: %s\n\n" % Time.get_datetime_string_from_system())
		for label in measurements:
			var m = measurements[label]
			file.store_string("[%s]\n" % label)
			file.store_string("  Elapsed: %.3f s\n" % (m.elapsed_ms / 1000.0))
			file.store_string("  Texture Memory: %.2f MB\n" % m.texture_memory_mb)
			file.store_string("  Object Count: %d\n" % m.object_count)
			file.store_string("  Node Count: %d\n\n" % m.node_count)
		file.close()
		print("\nReport saved to: %s" % file_path)

## Call this manually at any point to get current VRAM usage
func get_current_vram_mb() -> float:
	return Performance.get_monitor(Performance.VIEWER_TEXTURE_MEMORY) / (1024.0 * 1024.0)

## Get detailed breakdown
func get_memory_stats() -> Dictionary:
	return {
		"texture_memory_mb": get_current_vram_mb(),
		"object_count": Performance.get_monitor(Performance.OBJECT_COUNT),
		"node_count": Performance.get_monitor(Performance.OBJECT_NODE_COUNT),
		"physics_objects_2d": Performance.get_monitor(Performance.PHYSICS_2D_OBJECT_COUNT),
		"physics_objects_3d": Performance.get_monitor(Performance.PHYSICS_3D_OBJECT_COUNT),
	}
