-- TOG Tools — Logs outer tab (UI)
-- Body is a nested AceGUI TabGroup whose sub-tabs come from
-- `addon.logCategories`. Each sub-category exposes `subKey`, `label`,
-- `configKey`, and `Draw(container)`. Disabled sub-categories drop out
-- of the strip the same way disabled outer modules drop out of the
-- main window's strip.
--
-- Deep linking: callers (slash commands, future minimap entries) set
-- `Tab.pendingSubTab = "<subKey>"` before opening; this Draw reads and
-- clears it, falling back to the last-viewed sub-tab (per-character SV
-- via `db.char.frames.logsActiveSubTab`).

local _, addon = ...
local AceGUI = LibStub("AceGUI-3.0")

local Tab = addon.modules["logs"]
Tab.tabKey      = "logs"
Tab.configKey   = "logs"
Tab.label       = "Logs"
Tab.WINDOW_SIZE = { width = 820, height = 520, minWidth = 740, minHeight = 380 }
Tab.help = {
    title = "Logs",
    lines = {
        "Persistent history of activity across multiple log types. Each sub-tab is a different category — switch with the inner tab strip at the top.",
        "|cffffd700Mail|r — every mail received and sent: who, what, when, how much. Multi-item mails taken within a 5-second window are merged into one entry.",
        "|cffffd700Trade|r — every completed player-to-player trade (coming in a follow-on patch).",
        "|cffffd700Guild|r — guild roster events (joins / leaves / promotes) persisted beyond Blizzard's built-in ~100-entry cap (coming in a follow-on patch).",
        " ",
        "|cffffd700Filters:|r each sub-tab has its own character + date range filters above the table. Click a column header to sort; click again to reverse.",
        "|cffffd700Settings:|r turn the whole Logs feature on/off (or individual log types) via |cffffd700ESC > Options > AddOns > TOG Tools > Modules|r.",
    },
}

-- Per-character last-viewed sub-tab. Stored under db.char.frames so it
-- rides alongside the main window's other per-char UI state.
local function getActiveSubTab()
    local frames = addon.DB and addon.DB.char and addon.DB.char.frames
    return frames and frames.logsActiveSubTab
end
local function setActiveSubTab(key)
    local frames = addon.DB and addon.DB.char and addon.DB.char.frames
    if frames then frames.logsActiveSubTab = key end
end

local function BuildInnerTabDefs()
    local defs = {}
    for key, cat in pairs(addon.logCategories or {}) do
        if cat.label and addon:IsLogCategoryEnabled(key) then
            tinsert(defs, { value = key, text = cat.label })
        end
    end
    table.sort(defs, function(a, b) return a.text < b.text end)
    return defs
end

function Tab:Draw(container)
    container:SetLayout("Fill")

    local defs = BuildInnerTabDefs()
    if #defs == 0 then
        local note = AceGUI:Create("Label")
        note:SetFullWidth(true)
        note:SetText("|cffffaa55No log categories enabled.|r\n\nEnable Mail / Trade / Guild via |cffffd700ESC > Options > AddOns > TOG Tools > Modules|r.")
        container:AddChild(note)
        return
    end

    -- Resolution order: caller-supplied pending sub-tab → SV-cached last
    -- viewed → alphabetically first.
    local start = self.pendingSubTab or getActiveSubTab() or defs[1].value
    self.pendingSubTab = nil
    local valid = false
    for _, d in ipairs(defs) do if d.value == start then valid = true break end end
    if not valid then start = defs[1].value end

    local tg = AceGUI:Create("TabGroup")
    tg:SetTabs(defs)
    tg:SetLayout("Fill")
    tg:SetFullWidth(true)
    tg:SetFullHeight(true)
    tg:SetCallback("OnGroupSelected", function(widget, _, value)
        setActiveSubTab(value)
        widget:ReleaseChildren()
        local cat = addon.logCategories[value]
        if cat and cat.Draw then
            cat:Draw(widget)
        else
            local fs = AceGUI:Create("Label")
            fs:SetFullWidth(true)
            fs:SetText("Sub-tab \"" .. tostring(value) .. "\" — not yet implemented")
            widget:AddChild(fs)
        end
    end)
    container:AddChild(tg)
    tg:SelectTab(start)
end
