## Debug Manager - Global Debug and Logging System
##
## Provides centralized debug functionality and structured logging for the Skelly project.
## Manages debug state, overlay visibility, and log levels with formatted output.
## Replaces direct print() and push_error() calls with structured logging system.

extends Node

signal debug_toggled(enabled: bool)

enum LogLevel { TRACE = 0, DEBUG = 1, INFO = 2, WARN = 3, ERROR = 4, FATAL = 5 }

var debug_enabled: bool = false
var debug_overlay_visible: bool = false
var current_log_level: LogLevel = LogLevel.INFO


func _ready() -> void:
	"""Initialize the DebugManager on game startup"""
	log_info("DebugManager loaded")


func toggle_debug() -> void:
	"""Toggle debug mode on/off and emit signal to connected systems"""
	debug_enabled = !debug_enabled
	debug_toggled.emit(debug_enabled)
	log_info("Debug mode: " + ("ON" if debug_enabled else "OFF"))


func set_debug_enabled(enabled: bool) -> void:
	"""Set debug mode to specific state without toggling"""
	if debug_enabled != enabled:
		debug_enabled = enabled
		debug_toggled.emit(debug_enabled)


func is_debug_enabled() -> bool:
	"""Check if debug mode is currently enabled"""
	return debug_enabled


func toggle_overlay() -> void:
	"""Toggle debug overlay visibility"""
	debug_overlay_visible = !debug_overlay_visible


func set_overlay_visible(visible: bool) -> void:
	"""Set debug overlay visibility to specific state"""
	debug_overlay_visible = visible


func is_overlay_visible() -> bool:
	"""Check if debug overlay is currently visible"""
	return debug_overlay_visible


func set_log_level(level: LogLevel) -> void:
	"""Set minimum log level for output filtering"""
	current_log_level = level
	log_info("Log level set to: " + _log_level_to_string(level))


func get_log_level() -> LogLevel:
	"""Get current minimum log level"""
	return current_log_level


func _should_log(level: LogLevel) -> bool:
	"""Determine if message should be logged based on current log level"""
	return level >= current_log_level


func _log_level_to_string(level: LogLevel) -> String:
	"""Convert LogLevel enum to string representation"""
	match level:
		LogLevel.TRACE:
			return "TRACE"
		LogLevel.DEBUG:
			return "DEBUG"
		LogLevel.INFO:
			return "INFO"
		LogLevel.WARN:
			return "WARN"
		LogLevel.ERROR:
			return "ERROR"
		LogLevel.FATAL:
			return "FATAL"
		_:
			return "UNKNOWN"


func _format_log_message(level: LogLevel, message: String, category: String = "") -> String:
	"""Format log message with timestamp, level, category, and content"""
	var timestamp = Time.get_datetime_string_from_system()
	var level_str = _log_level_to_string(level)
	var category_str = (" [" + category + "]") if category != "" else ""
	return "[%s] %s%s: %s" % [timestamp, level_str, category_str, message]


func log_trace(message: String, category: String = "") -> void:
	"""Log trace-level message (lowest priority, only shown in debug mode)"""
	if _should_log(LogLevel.TRACE):
		var formatted = _format_log_message(LogLevel.TRACE, message, category)
		if debug_enabled:
			print(formatted)


func log_debug(message: String, category: String = "") -> void:
	"""Log debug-level message (development information, only shown in debug mode)"""
	if _should_log(LogLevel.DEBUG):
		var formatted = _format_log_message(LogLevel.DEBUG, message, category)
		if debug_enabled:
			print(formatted)


func log_info(message: String, category: String = "") -> void:
	"""Log info-level message (general information, always shown)"""
	if _should_log(LogLevel.INFO):
		var formatted = _format_log_message(LogLevel.INFO, message, category)
		print(formatted)


func log_warn(message: String, category: String = "") -> void:
	"""Log warning-level message (potential issues that don't break functionality)"""
	if _should_log(LogLevel.WARN):
		var formatted = _format_log_message(LogLevel.WARN, message, category)
		print(formatted)
		push_warning(formatted)


func log_error(message: String, category: String = "") -> void:
	"""Log error-level message (serious issues that may break functionality)"""
	if _should_log(LogLevel.ERROR):
		var formatted = _format_log_message(LogLevel.ERROR, message, category)
		print(formatted)
		push_error(formatted)


func log_fatal(message: String, category: String = "") -> void:
	"""Log fatal-level message (critical errors that prevent normal operation)"""
	if _should_log(LogLevel.FATAL):
		var formatted = _format_log_message(LogLevel.FATAL, message, category)
		print(formatted)
		push_error(formatted)
