-- TOG Tools — Logs (outer container + sub-category registry)
--
-- The Logs tab is a NEXUS — its body is a nested AceGUI TabGroup whose
-- sub-tabs are populated from `addon.logCategories`. This file owns:
--   * the outer tab registration (`addon.modules["logs"]`)
--   * the sub-category registry
--   * `addon:IsLogCategoryEnabled(subKey)` — gate helper for sub-engines
--   * `Logs:PruneEntries(entries)` — retention prune called by sub-engines
--
-- Sub-engines (Mail / Trade / Guild) self-register at file-load time:
--     addon.logCategories["mail"] = MailLog
-- Each entry should expose:
--     subKey     string   internal key matching the inner tab value
--     label      string   strip label
--     configKey  string   db.global section holding `enabled` + data
--     Draw(container)     AceGUI container for the sub-tab body
-- The sub-tab Draw is merged in by the corresponding GUI/<Name>SubTab.lua.

local _, addon = ...

local Logs = {}
addon.modules["logs"] = Logs

addon.logCategories = addon.logCategories or {}

-- Returns true iff the Logs master toggle is on AND the per-category
-- toggle is on. Sub-engines call this from every event hook so disabling
-- either flag silently kills the work. Defaults to true for unknown
-- categories so freshly-registered sub-engines aren't accidentally
-- suppressed before they declare a configKey.
function addon:IsLogCategoryEnabled(subKey)
    if not (self.DB and self.DB.global) then return true end
    if not self:IsModuleEnabled("logs") then return false end
    local cat = addon.logCategories and addon.logCategories[subKey]
    if not cat then return true end
    local section = self.DB.global[cat.configKey or ""]
    if not section then return true end
    if section.enabled == nil then return true end
    return section.enabled and true or false
end

-- Drop entries older than db.global.logs.retentionDays.
-- retentionDays == 0 disables pruning entirely.
--
-- ORDER-AGNOSTIC by design. This used to walk from the head while entries
-- were expired, which assumes head == oldest. That holds for the logs that
-- append on capture (Mail / Trade / Whisper / Gathering) but NOT for the
-- Guild Log, which sorts its bucket newest-first and then prunes — there the
-- walk hit the newest entry first, stopped immediately, and retention silently
-- never applied to guild entries at all. Compacting in place costs one full
-- pass instead of one-per-expired-entry, which is nothing at these sizes, and
-- removes an ordering contract every caller has to remember.
function Logs:PruneEntries(entries)
    if not entries or not entries[1] then return end
    local cfg = addon.DB and addon.DB.global and addon.DB.global.logs
    -- `or 90` looks like the classic Lua default-value trap, and is not one: 0 is
    -- TRUTHY in Lua, so a configured 0 survives `or 90` and disables pruning on the
    -- next line, which is what 0 is meant to do. Do not "fix" this to an explicit
    -- nil check -- it already behaves correctly, and the rewrite is what would
    -- break it.
    local days = cfg and cfg.retentionDays or 90
    if days <= 0 then return end
    local cutoff = (GetServerTime() or time()) - days * 86400
    local write = 0
    for read = 1, #entries do
        local e = entries[read]
        if (e.ts or 0) >= cutoff then
            write = write + 1
            entries[write] = e
        end
    end
    for i = #entries, write + 1, -1 do entries[i] = nil end
end

function Logs:OnEnable()
    -- No engine hooks here; sub-engines wire their own. This hook exists
    -- for module-pattern consistency.
end
