--[[
============================================================================
Enhanced Pet Tracker  -  Core/Commands.lua
============================================================================
Slash-command dispatch for /ept (alias /pettracker).

  /ept              toggle the Pet Tracker window
  /ept tooltip      toggle NPC pet-drop tooltips
  /ept minimap      toggle the minimap button
  /ept stats        print collection stats to chat
  /ept refresh      force a re-scan of owned pets
  /ept help         list commands

Dispatch is a small hash-map of handlers — the same registry idea ETB uses,
kept minimal here because there are only a handful of commands.
============================================================================
]]--

local ADDON_NAME, NS = ...
local EPT = NS.EPT
if not EPT then return end

local handlers = {}

handlers["help"] = function()
    EPT:Print("commands:")
    EPT:Print("  |cffffd100/ept|r — open / close the Pet Tracker")
    EPT:Print("  |cffffd100/ept tooltip|r — toggle NPC pet-drop tooltips")
    EPT:Print("  |cffffd100/ept minimap|r — toggle the minimap button")
    EPT:Print("  |cffffd100/ept stats|r — collection statistics")
    EPT:Print("  |cffffd100/ept refresh|r — re-scan owned pets")
end

handlers["tooltip"] = function()
    if EPT.PetTooltip then EPT.PetTooltip:Toggle() end
end

handlers["minimap"] = function()
    if EPT.Minimap then EPT.Minimap:Toggle() end
end

handlers["refresh"] = function()
    EPT.PetAPI:Refresh()
    EPT:Print("pet collection re-scanned.")
end

handlers["stats"] = function()
    local API = EPT.PetAPI
    local owned, total = API:GetProgress()
    EPT:Printf("tracked %d / %d pets (%.1f%%); %d total owned.",
        owned, total, total > 0 and owned / total * 100 or 0, API:GetOwnedCount())

    -- per-source breakdown
    local data, srcNames = EPT.PetData, EPT.PetSourceNames or {}
    if not data then return end
    local own, tot = {}, {}
    for spellID, rec in pairs(data) do
        local c = rec.src or "?"
        tot[c] = (tot[c] or 0) + 1
        if API:IsCollected(spellID) then own[c] = (own[c] or 0) + 1 end
    end
    for _, c in ipairs({ "B","R","V","P","C","F","H","W","K","L","M","?" }) do
        if tot[c] then
            EPT:Printf("  %s: |cff44ff44%d|r/%d",
                srcNames[c] or c, own[c] or 0, tot[c])
        end
    end
end

local function dispatch(msg)
    msg = (msg or ""):gsub("^%s+", ""):gsub("%s+$", "")
    local cmd = msg:match("^(%S+)")
    if not cmd then
        EPT.Browser:Toggle()
        return
    end
    local h = handlers[cmd:lower()]
    if h then
        h()
    else
        EPT:Print("unknown command '" .. cmd .. "'. Try |cffffd100/ept help|r.")
    end
end

SLASH_EPT1 = "/ept"
SLASH_EPT2 = "/pettracker"
SlashCmdList["EPT"] = dispatch

EPT:Debug("Commands", "slash commands registered (/ept, /pettracker)")
