-- APIProbe.lua — diagnostic for the post-Siege-of-Orgrimmar API changes.
-- Shipped in the addon (v15.46.0) so it's always available. Touches no addon
-- behavior. Run:  /etbapi        -> data-source APIs (Encounter Journal, etc.)
--                 /etbapi apply  -> transmog APPLY surface (diagnoses "won't apply sets")
local _, NS = ...
local function p(m) DEFAULT_CHAT_FRAME:AddMessage("|cff33ddff[ETB API]|r " .. tostring(m)) end
local function yn(b) return b and "|cff44ff44YES|r" or "|cffff5555no|r" end
local function isfn(t, k) return t and type(t[k]) == "function" end

local function probeData()
    p("---- data-source probe ----")
    local EJns = _G.C_EncounterJournal
    p("C_EncounterJournal: " .. yn(EJns ~= nil))
    local haveEJ = isfn(_G,"EJ_SelectInstance") and isfn(_G,"EJ_GetNumLoot") and isfn(_G,"EJ_GetLootInfoByIndex")
    p("  EJ_SelectInstance/GetNumLoot/GetLootInfoByIndex: " .. yn(haveEJ))
    if haveEJ then
        for _, inst in ipairs({ {362,"ToT"}, {369,"SoO"}, {316,"MSV"} }) do
            pcall(_G.EJ_SelectInstance, inst[1])
            local cnt = select(1, _G.EJ_GetNumLoot()) or 0
            p(string.format("  %s(%d): EJ_GetNumLoot=%s", inst[2], inst[1], tostring(cnt)))
            if cnt and cnt > 0 then
                local t = { pcall(_G.EJ_GetLootInfoByIndex, 1) }
                p("    loot[1] name=" .. tostring(t[2]) .. " itemID=" .. tostring(t[7] or t[8] or t[6]))
            end
        end
    end
    local TS = _G.C_TransmogSets
    if isfn(TS,"GetAllSets") then local ok,s = pcall(TS.GetAllSets); p("C_TransmogSets.GetAllSets: "..tostring(ok and s and #s or "0")) else p("C_TransmogSets.GetAllSets: "..yn(false)) end
    local TC = _G.C_TransmogCollection
    if isfn(TC,"GetCategoryAppearances") then local ok,a = pcall(TC.GetCategoryAppearances,1); p("C_TransmogCollection cat1 appearances: "..tostring(ok and a and #a or "0")) end
    p("C_Reputation "..yn(_G.C_Reputation~=nil).." | C_AchievementInfo "..yn(_G.C_AchievementInfo~=nil).." | C_TradeSkillUI "..yn(_G.C_TradeSkillUI~=nil))
    p("---- end (copy back to Claude) ----")
end

local function probeApply()
    p("---- transmog APPLY surface ----")
    p("Run this AT a transmogrifier NPC for the IsAtTransmogNPC check.")
    local CT = _G.C_Transmog
    p("C_Transmog: " .. yn(CT ~= nil))
    if CT then
        for _, fn in ipairs({"SetPending","ApplyAllPending","GetPending","ClearPending","IsAtTransmogNPC","GetCost","GetSlotInfo"}) do
            p("  C_Transmog."..fn..": "..yn(isfn(CT,fn)))
        end
        if isfn(CT,"IsAtTransmogNPC") then local ok,r = pcall(CT.IsAtTransmogNPC); p("  IsAtTransmogNPC() -> "..tostring(ok and r)) end
    end
    local TU = _G.TransmogUtil
    p("TransmogUtil: " .. yn(TU ~= nil))
    if TU then
        for _, fn in ipairs({"GetTransmogLocation","CreateTransmogLocation","CreateTransmogPendingInfo","GetTransmogPendingInfo"}) do
            p("  TransmogUtil."..fn..": "..yn(isfn(TU,fn)))
        end
    end
    local E = _G.Enum
    p("Enum.TransmogType "..yn(E and E.TransmogType~=nil)..
      " | Enum.TransmogModification "..yn(E and E.TransmogModification~=nil)..
      " | Enum.TransmogPendingType "..yn(E and E.TransmogPendingType~=nil))
    p("TransmogrifyFrame shown "..yn(_G.TransmogrifyFrame and _G.TransmogrifyFrame:IsShown())..
      " | WardrobeFrame shown "..yn(_G.WardrobeFrame and _G.WardrobeFrame:IsShown()))
    p("---- end (copy back to Claude) ----")
end

SLASH_ETBAPI1 = "/etbapi"
SlashCmdList.ETBAPI = function(msg)
    msg = (msg or ""):lower():gsub("%s+","")
    if msg == "apply" then probeApply() else probeData() end
end
