extends Node

# Log Manager - Centralized logging system with category-based filtering
# Categories can be enabled/disabled independently for debugging

# Log categories
const CATEGORY_NETWORK = "NETWORK"
const CATEGORY_GAMEPLAY = "GAMEPLAY"
const CATEGORY_UI = "UI"
const CATEGORY_DUNGEON = "DUNGEON"
const CATEGORY_ENEMY = "ENEMY"
const CATEGORY_PLAYER = "PLAYER"
const CATEGORY_DOOR = "DOOR"
const CATEGORY_INVENTORY = "INVENTORY"
const CATEGORY_DEFAULT = "DEFAULT"

# Enable/disable flags for each category
var enabled_categories: Dictionary = {
	CATEGORY_NETWORK: true,  # Enable network logs for debugging
	CATEGORY_GAMEPLAY: false,
	CATEGORY_UI: false,
	CATEGORY_DUNGEON: false,
	CATEGORY_ENEMY: false,
	CATEGORY_PLAYER: false,
	CATEGORY_DOOR: false,
	CATEGORY_INVENTORY: false,
	CATEGORY_DEFAULT: false,  # Disable default/unclassified logs
}

# Logging helper to determine host/joiner prefix
func _log_prefix() -> String:
	var network_manager = get_node_or_null("/root/NetworkManager")
	if network_manager and network_manager.is_hosting:
		return "[H] "
	else:
		return "[J] "

func log(message: String, category: String = CATEGORY_DEFAULT):
	"""Log a message if the category is enabled"""
	if enabled_categories.get(category, false):
		print(_log_prefix() + "[" + category + "] " + message)

func log_error(message: String, category: String = CATEGORY_DEFAULT):
	"""Log an error message if the category is enabled"""
	if enabled_categories.get(category, false):
		push_error(_log_prefix() + "[" + category + "] " + message)

func set_category_enabled(category: String, enabled: bool):
	"""Enable or disable a log category"""
	enabled_categories[category] = enabled

func is_category_enabled(category: String) -> bool:
	"""Check if a category is enabled"""
	return enabled_categories.get(category, false)

func enable_all_categories():
	"""Enable all log categories"""
	for category in enabled_categories:
		enabled_categories[category] = true

func disable_all_categories():
	"""Disable all log categories"""
	for category in enabled_categories:
		enabled_categories[category] = false

func enable_network_only():
	"""Enable only network-related logs (useful for debugging multiplayer)"""
	disable_all_categories()
	enabled_categories[CATEGORY_NETWORK] = true

# Example usage:
# LogManager.log("This is a network message", LogManager.CATEGORY_NETWORK)
# LogManager.log("This is a gameplay message", LogManager.CATEGORY_GAMEPLAY)
# LogManager.set_category_enabled(LogManager.CATEGORY_GAMEPLAY, true)  # Enable gameplay logs
# LogManager.enable_network_only()  # Disable all except network
#
# NOTE: Many scripts still use print() directly. To suppress those logs,
# you can temporarily redirect print() output or update scripts to use LogManager.
# For now, network-related scripts (NetworkManager, MatchboxClient, RoomRegistry)
# use LogManager, while gameplay scripts still use print() directly.
