--[[
============================================================================
Enhanced Mount Tracker  -  Core/Commands.lua
============================================================================
Slash-command dispatch for /emt (alias /mounttracker).

  /emt              toggle the Mount Tracker window
  /emt tooltip      toggle NPC mount-drop tooltips
  /emt minimap      toggle the minimap button
  /emt stats        print collection stats to chat
  /emt refresh      force a re-scan of owned mounts
  /emt 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 EMT = NS.EMT
if not EMT then return end

local handlers = {}

handlers["help"] = function()
    EMT:Print("commands:")
    EMT:Print("  |cffffd100/emt|r — open / close the Mount Tracker")
    EMT:Print("  |cffffd100/emt tooltip|r — toggle NPC mount-drop tooltips")
    EMT:Print("  |cffffd100/emt minimap|r — toggle the minimap button")
    EMT:Print("  |cffffd100/emt stats|r — collection statistics")
    EMT:Print("  |cffffd100/emt audit|r — compare our data to the Mount Journal")
    EMT:Print("  |cffffd100/emt refresh|r — re-scan owned mounts")
end

handlers["audit"] = function()
    local API = EMT.MountAPI
    local inBoth, dataOnly, journalOnly, usingJournal = API:Audit()
    EMT:Printf("data audit — detection path: |cffffffff%s|r",
        usingJournal and "C_MountJournal" or "GetCompanionInfo (fallback)")
    EMT:Printf("  %d of our %d mounts matched in the journal.",
        inBoth, inBoth + #dataOnly)
    if #dataOnly > 0 then
        EMT:Printf("  |cffff8800%d|r in our data but NOT in this client's journal "
            .. "(removed / wrong-version):", #dataOnly)
        local shown = {}
        for i = 1, math.min(8, #dataOnly) do shown[i] = dataOnly[i] end
        EMT:Print("    spellIDs: " .. table.concat(shown, ", ")
            .. (#dataOnly > 8 and " ..." or ""))
    end
    if not usingJournal then
        EMT:Print("  |cff888888(journal unavailable — cannot list mounts the "
            .. "client has that our data lacks)|r")
    elseif #journalOnly > 0 then
        EMT:Printf("  |cffffcc00%d|r mounts in the journal but NOT in our data "
            .. "(coverage gap):", #journalOnly)
        for i = 1, math.min(12, #journalOnly) do
            local e = journalOnly[i]
            EMT:Printf("    %s |cff888888(%d)|r", e.name or "?", e.spellID)
        end
        if #journalOnly > 12 then
            EMT:Printf("    ...and %d more.", #journalOnly - 12)
        end
    else
        EMT:Print("  |cff44ff44No coverage gap — our data covers every journal "
            .. "mount.|r")
    end
end

handlers["tooltip"] = function()
    if EMT.MountTooltip then EMT.MountTooltip:Toggle() end
end

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

handlers["refresh"] = function()
    EMT.MountAPI:Refresh()
    EMT:Print("mount collection re-scanned.")
end

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

    -- per-source breakdown
    local data, srcNames = EMT.MountData, EMT.MountSourceNames 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
            EMT: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
        EMT.Browser:Toggle()
        return
    end
    local h = handlers[cmd:lower()]
    if h then
        h()
    else
        EMT:Print("unknown command '" .. cmd .. "'. Try |cffffd100/emt help|r.")
    end
end

SLASH_EMT1 = "/emt"
SLASH_EMT2 = "/mounttracker"
SlashCmdList["EMT"] = dispatch

EMT:Debug("Commands", "slash commands registered (/emt, /mounttracker)")
