-- TOG Tools — Slash Commands
-- All /togt command registration and handling lives here.
-- Add new subcommands to SlashHandler as modules are added.

local _, addon = ...
local Ace = addon.lib
local gv  = addon.gameVersion

-- ---------------------------------------------------------------------------
-- Registration
-- ---------------------------------------------------------------------------
hooksecurefunc(Ace, "OnInitialize", function()
    Ace:RegisterChatCommand("togt", "SlashHandler")
end)

-- ---------------------------------------------------------------------------
-- Handler
-- ---------------------------------------------------------------------------
function Ace:SlashHandler(input)
    input = strtrim(input or "")

    if input == "" then
        -- No subcommand — toggle the main window
        if addon.MainWindow then
            addon.MainWindow:Toggle()
        end

    elseif input == "np" or input == "nameprefix" then
        if addon.MainWindow then
            addon.MainWindow:Open("nameprefix")
        end

    elseif input == "al" or input == "addonload" then
        if addon.MainWindow then
            addon.MainWindow:Open("addonload")
        end

    elseif input == "mb" or input == "mail" or input == "mailbox" then
        if addon.MainWindow then
            addon.MainWindow:Open("mailbox")
        end

    elseif input == "ld" or input == "login" or input == "digest" then
        if addon.MainWindow then
            addon.MainWindow:Open("logindigest")
        end

    elseif input == "settings" or input == "config" then
        if addon.OpenSettings then
            addon:OpenSettings()
        end

    elseif input == "vc" or input == "versioncheck" then
        local VC = LibStub and LibStub:GetLibrary("VersionCheck-1.0", true)
        if gv.isRetail then
            Ace:Print("|cffff4444VersionCheck-1.0 is not available on Retail.|r")
        elseif not VC then
            Ace:Print("|cffff4444VersionCheck-1.0 not found — is the VersionCheck addon installed?|r")
        else
            local hostEntry = VC.hosts and VC.hosts[addon.name]
            if not hostEntry then
                Ace:Print("|cffff4444TOG Tools is not registered with VersionCheck-1.0.|r")
            else
                VC:FireBatch()
                Ace:Print("Version check broadcast sent — waiting 21 seconds for responses...")
                C_Timer.After(21, function()
                    local responses = hostEntry.VersionResponses or {}
                    local list = {}
                    for sender, version in pairs(responses) do
                        tinsert(list, { name = sender, version = tostring(version) })
                    end
                    table.sort(list, function(a, b)
                        local cmp = VC:CompareVersion(b.version, a.version)
                        if cmp ~= 0 then return cmp > 0 end
                        return a.name < b.name
                    end)
                    Ace:Print("Version check: " .. #list .. " guild member(s) responded")
                    for _, entry in ipairs(list) do
                        Ace:Print("  " .. entry.name .. ": " .. entry.version)
                    end
                end)
            end
        end

    else
        Ace:Print("|cffFF8000TOG Tools|r v" .. addon.Version)
        Ace:Print("/togt                — open the main window")
        Ace:Print("/togt np             — open Name Prefix settings")
        Ace:Print("/togt al             — open Addon Load monitor")
        Ace:Print("/togt mail           — open Mailbox Stale Watcher")
        Ace:Print("/togt login          — open Login Digest settings")
        Ace:Print("/togt settings       — open addon settings")
        Ace:Print("/togt vc             — check addon versions across the guild")
    end
end
