-- Lua FFI binding for Rust tracing logger
local ffi = require("ffi")

-- Load the dynamic library
local lib = ffi.load("lua_ffi_logger")

-- FFI declarations
ffi.cdef[[
    int init_logger(const char* log_path, const char* log_level, uint64_t interval, uint32_t max_kept, int64_t max_size);
    void log_trace(const char* message);
    void log_debug(const char* message);
    void log_info(const char* message);
    void log_warn(const char* message);
    void log_error(const char* message);
    void log_with_level(const char* level, const char* message);
]]

-- Logger class
local Logger = {}
Logger.__index = Logger

function Logger.new(log_path, log_level, interval, max_kept, max_size)
    local self = setmetatable({}, Logger)
    
    log_path = log_path or "logs/app.log"
    log_level = log_level or "info"
    interval = interval or 60 * 60  -- 默认1小时
    max_kept = max_kept or 24 * 7   -- 默认保留7天
    max_size = max_size or -1       -- 默认不按大小轮转
    
    local result = lib.init_logger(log_path, log_level, interval, max_kept, max_size)
    if result ~= 0 then
        error("Failed to initialize logger")
    end
    
    return self
end

function Logger:trace(message)
    lib.log_trace(message)
end

function Logger:debug(message)
    lib.log_debug(message)
end

function Logger:info(message)
    lib.log_info(message)
end

function Logger:warn(message)
    lib.log_warn(message)
end

function Logger:error(message)
    lib.log_error(message)
end

function Logger:log(level, message)
    lib.log_with_level(level, message)
end

-- Create a global logger instance
local function create_logger(log_path, log_level, interval, max_kept, max_size)
    return Logger.new(log_path, log_level, interval, max_kept, max_size)
end

-- Export the module
return {
    Logger = Logger,
    create_logger = create_logger
}
