-- Logger.lua
-- Self-contained logger for Inventory Tetris B42 Patch

local Logger = {}

-- Ensure global config exists with safe defaults
if not _G.InventoryTetrisPatchConfig then
    _G.InventoryTetrisPatchConfig = {
        DebugLog = false,
        Version = "v0.0.0"
    }
else
    -- Set safe defaults if partial config exists
    _G.InventoryTetrisPatchConfig.DebugLog = _G.InventoryTetrisPatchConfig.DebugLog ~= false
    _G.InventoryTetrisPatchConfig.Version = _G.InventoryTetrisPatchConfig.Version or "v0.0.0"
end

-- Main logging function
function Logger.log(message, prefix)
    -- Check debug flag with fallback
    local debugLog = _G.InventoryTetrisPatchConfig and 
                     _G.InventoryTetrisPatchConfig.DebugLog ~= false
    
    if debugLog then
        -- Get version with fallback
        local version = "v?"
        if _G.InventoryTetrisPatchConfig and _G.InventoryTetrisPatchConfig.Version then
            version = _G.InventoryTetrisPatchConfig.Version
        end
        
        local logString = "[InventoryTetrisB42Patch " .. version .. "] "
        
        if prefix then
            logString = logString .. "[" .. prefix .. "] "
        end
        
        print(logString .. tostring(message))
    end
end

-- Error logging (always logs regardless of debug setting)
function Logger.error(message)
    local version = "v?"
    if _G.InventoryTetrisPatchConfig and _G.InventoryTetrisPatchConfig.Version then
        version = _G.InventoryTetrisPatchConfig.Version
    end
    
    print("[InventoryTetrisB42Patch " .. version .. "] [ERROR] " .. tostring(message))
end

-- Warning messages
function Logger.warn(message)
    Logger.log(message, "WARN")
end

-- Debug messages (only if debug is enabled)
function Logger.debug(message)
    Logger.log(message, "DEBUG")
end

-- Success messages
function Logger.success(message)
    Logger.log(message, "SUCCESS")
end

-- Initialization check
function Logger.isInitialized()
    return _G.InventoryTetrisPatchConfig ~= nil
end

return Logger