extends SceneTree

# Test script for the debug_manager logging system
# This script validates all log levels, filtering, and formatting functionality
# Usage: Add to scene or autoload temporarily to run tests


func _initialize():
	# Wait a frame for debug_manager to initialize
	await process_frame
	test_logging_system()
	quit()


func test_logging_system():
	print("=== Starting Logging System Tests ===")

	# Get DebugManager reference once
	var debug_manager = root.get_node("DebugManager")

	# Test 1: Basic log level functionality
	test_basic_logging(debug_manager)

	# Test 2: Log level filtering
	test_log_level_filtering(debug_manager)

	# Test 3: Category functionality
	test_category_logging(debug_manager)

	# Test 4: Debug mode integration
	test_debug_mode_integration(debug_manager)

	print("=== Logging System Tests Complete ===")


func test_basic_logging(debug_manager):
	print("\n--- Test 1: Basic Log Level Functionality ---")

	# Reset to INFO level for consistent testing
	debug_manager.set_log_level(debug_manager.LogLevel.INFO)

	debug_manager.log_trace("TRACE: This should not appear (below INFO level)")
	debug_manager.log_debug("DEBUG: This should not appear (below INFO level)")
	debug_manager.log_info("INFO: This message should appear")
	debug_manager.log_warn("WARN: This warning should appear")
	debug_manager.log_error("ERROR: This error should appear")
	debug_manager.log_fatal("FATAL: This fatal error should appear")


func test_log_level_filtering(debug_manager):
	print("\n--- Test 2: Log Level Filtering ---")

	# Test DEBUG level
	print("Setting log level to DEBUG...")
	debug_manager.set_log_level(debug_manager.LogLevel.DEBUG)
	debug_manager.log_trace("TRACE: Should not appear (below DEBUG)")
	debug_manager.log_debug("DEBUG: Should appear with debug enabled")
	debug_manager.log_info("INFO: Should appear")

	# Test ERROR level (very restrictive)
	print("Setting log level to ERROR...")
	debug_manager.set_log_level(debug_manager.LogLevel.ERROR)
	debug_manager.log_debug("DEBUG: Should not appear (below ERROR)")
	debug_manager.log_warn("WARN: Should not appear (below ERROR)")
	debug_manager.log_error("ERROR: Should appear")
	debug_manager.log_fatal("FATAL: Should appear")

	# Reset to INFO for remaining tests
	debug_manager.set_log_level(debug_manager.LogLevel.INFO)


func test_category_logging(debug_manager):
	print("\n--- Test 3: Category Functionality ---")

	debug_manager.log_info("Message without category")
	debug_manager.log_info("Message with TEST category", "TEST")
	debug_manager.log_info("Message with LOGGING category", "LOGGING")
	debug_manager.log_warn("Warning with VALIDATION category", "VALIDATION")
	debug_manager.log_error("Error with SYSTEM category", "SYSTEM")


func test_debug_mode_integration(debug_manager):
	print("\n--- Test 4: Debug Mode Integration ---")

	# Set to TRACE level to test debug mode dependency
	debug_manager.set_log_level(debug_manager.LogLevel.TRACE)

	var original_debug_state = debug_manager.is_debug_enabled()

	# Test with debug mode OFF
	debug_manager.set_debug_enabled(false)
	print("Debug mode OFF - TRACE and DEBUG should not appear:")
	debug_manager.log_trace("TRACE: Should NOT appear (debug mode OFF)")
	debug_manager.log_debug("DEBUG: Should NOT appear (debug mode OFF)")
	debug_manager.log_info("INFO: Should appear regardless of debug mode")

	# Test with debug mode ON
	debug_manager.set_debug_enabled(true)
	print("Debug mode ON - TRACE and DEBUG should appear:")
	debug_manager.log_trace("TRACE: Should appear (debug mode ON)")
	debug_manager.log_debug("DEBUG: Should appear (debug mode ON)")
	debug_manager.log_info("INFO: Should still appear")

	# Restore original debug state
	debug_manager.set_debug_enabled(original_debug_state)
	debug_manager.set_log_level(debug_manager.LogLevel.INFO)


# Helper function to validate log level enum values
func test_log_level_enum(debug_manager):
	print("\n--- Log Level Enum Values ---")
	print("TRACE: ", debug_manager.LogLevel.TRACE)
	print("DEBUG: ", debug_manager.LogLevel.DEBUG)
	print("INFO: ", debug_manager.LogLevel.INFO)
	print("WARN: ", debug_manager.LogLevel.WARN)
	print("ERROR: ", debug_manager.LogLevel.ERROR)
	print("FATAL: ", debug_manager.LogLevel.FATAL)
