-- TOG Tools
-- Author: Pimptasty
-- A collection of lightweight utility tools for WoW Classic and Retail.

local addonName, addon = ...

-- ---------------------------------------------------------------------------
-- Addon global
-- ---------------------------------------------------------------------------
TOGTools       = TOGTools or {}
TOGTools.addon = addon
addon.name     = addonName

addon.BrandColor = "ffFF8000"   -- Orange, consistent with the TOG suite

local _GetAddOnMetadata = (C_AddOns and C_AddOns.GetAddOnMetadata) or GetAddOnMetadata
addon.Version = _GetAddOnMetadata(addonName, "Version") or "dev"

-- ---------------------------------------------------------------------------
-- Version flags
-- ---------------------------------------------------------------------------
local gv = {}
addon.gameVersion = gv
do
    local tocVersion = select(4, GetBuildInfo())
    gv.isVanilla = tocVersion < 20000
    gv.isTBC     = tocVersion >= 20000 and tocVersion < 30000
    gv.isWrath   = tocVersion >= 30000 and tocVersion < 40000
    gv.isCata    = tocVersion >= 40000 and tocVersion < 50000
    gv.isMists   = tocVersion >= 50000 and tocVersion < 60000
    gv.isClassic = gv.isVanilla or gv.isTBC or gv.isWrath or gv.isCata or gv.isMists
    gv.isRetail  = not gv.isClassic
    -- Retail 12.0+ uses the EventRegistry path for chat hooks.
    gv.isRetail120Plus = tocVersion >= 120000
end

-- ---------------------------------------------------------------------------
-- AceAddon
-- ---------------------------------------------------------------------------
local Ace = LibStub("AceAddon-3.0"):NewAddon(
    addonName,
    "AceConsole-3.0",
    "AceEvent-3.0",
    "AceHook-3.0"
)
addon.lib = Ace

-- ---------------------------------------------------------------------------
-- AceDB schema
-- ---------------------------------------------------------------------------
local DB_DEFAULTS = {
    char = {
        minimapPos  = 220,
        minimapHide = false,
        lastTab     = nil,         -- last-selected MainWindow tabKey
        frames = {
            mainWindow = { width = 640, height = 460 },
        },
    },
    global = {
        -- NamePrefix module settings — account-wide so the nickname only
        -- needs to be configured once and applies to every alt. Toons that
        -- shouldn't self-prefix rely on `hideIfCharName` (default true) to
        -- suppress the prefix when the nickname equals the character name.
        namePrefix = {
            enabled  = true,
            nickname = "",
            format   = "(%s): ",   -- %s is replaced with nickname
            guild    = true,
            officer  = true,
            party    = false,
            raid     = false,
            instance = false,
            channel  = "",         -- custom channel name filter (empty = off)
            skipExclamation  = true,
            hideIfCharName   = true,
        },
        -- Mailbox Stale Watcher: per-alt mail snapshots, account-wide so
        -- every alt's expiring mail is visible from any character.
        mailbox = {
            thresholdDays = 2,
            snapshots     = {},   -- [Name-Realm] = { snapshotAt, items }
        },
        -- Login Digest: one-line state-of-account summary at PLAYER_LOGIN.
        -- lastLogout is per-character (Name-Realm key) so each alt
        -- reports its own "since last seen" value.
        loginDigest = {
            enabled    = true,
            fields = {
                mail        = true,
                guildOnline = true,
                hearth      = true,
                bags        = true,
                lastLogout  = true,
            },
            lastLogout = {},   -- [Name-Realm] = serverTime
        },
    },
}

-- ---------------------------------------------------------------------------
-- Module registry — tabs are built from this table, sorted alphabetically
-- by their `label` field.  Each module file registers itself here.
-- ---------------------------------------------------------------------------
addon.modules = {}

-- ---------------------------------------------------------------------------
-- OnInitialize / OnEnable
-- ---------------------------------------------------------------------------
function Ace:OnInitialize()
    addon.DB = LibStub("AceDB-3.0"):New("TOGTools_DB", DB_DEFAULTS, true)

    -- One-shot migration: v0.3.2 moved namePrefix from char to global.
    -- If this character has a pre-migration char.namePrefix table with a
    -- non-empty nickname AND the new global table is still at defaults
    -- (empty nickname), copy the per-character settings up. Picks whichever
    -- alt logs in first after the upgrade; subsequent alts just inherit
    -- the now-global config. Leftover char.namePrefix is harmless.
    do
        local oldCfg = rawget(addon.DB.char, "namePrefix")
        local newCfg = addon.DB.global.namePrefix
        if oldCfg and oldCfg.nickname and oldCfg.nickname ~= ""
           and (not newCfg.nickname or newCfg.nickname == "") then
            for k, v in pairs(oldCfg) do newCfg[k] = v end
        end
    end

    -- Expose Version so VersionCheck-1.0 reads it from the host object directly.
    Ace.Version = addon.Version

    -- Register with VersionCheck-1.0 on Classic; the library is not yet
    -- available on Retail so we guard with the version flag.
    if not gv.isRetail then
        local VC = LibStub("VersionCheck-1.0", true)
        if VC then
            VC:Enable(Ace)
        end
    end

end

function Ace:OnEnable()
    -- Enable all registered modules
    for _, mod in pairs(addon.modules) do
        if mod.OnEnable then mod:OnEnable() end
    end
end
