--[[
ETB CollectionTooltip — "Appearance collected/not collected" on item tooltips
Hooks GameTooltip, ItemRefTooltip, ShoppingTooltips
]]--
local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.CollectionTooltip = {}
local CT = ETB.CollectionTooltip
CT.enabled = true

local function OnTooltipSetItem(tooltip)
    if not CT.enabled then return end
    local _, link = tooltip:GetItem()
    if not link then return end
    local itemID = tonumber(link:match("item:(%d+)"))
    if not itemID then return end
    
    -- Only equippable items (classID 2=Weapon, 4=Armor)
    local _, _, _, _, _, classID = GetItemInfoInstant(itemID)
    if not classID or (classID ~= 2 and classID ~= 4) then return end
    
    local isCollected = false
    local hasAppearance = false
    
    -- Cross-class appearance check for MoP Classic
    if C_TransmogCollection and C_TransmogCollection.GetItemInfo then
        local ok, appearanceID, sourceID = pcall(C_TransmogCollection.GetItemInfo, itemID)
        -- Fallback: try with item link if bare itemID returned nil
        if ok and not appearanceID then
            local _, itemLink = GetItemInfo(itemID)
            if itemLink then
                ok, appearanceID, sourceID = pcall(C_TransmogCollection.GetItemInfo, itemLink)
            end
        end
        if ok then
            hasAppearance = (appearanceID ~= nil) or (sourceID ~= nil)
            
            -- PRIMARY: GetAllAppearanceSources (cross-class!) + PlayerHasTransmogItemModifiedAppearance
            if appearanceID and C_TransmogCollection.GetAllAppearanceSources then
                local aOK, allSources = pcall(C_TransmogCollection.GetAllAppearanceSources, appearanceID)
                if aOK and allSources and C_TransmogCollection.PlayerHasTransmogItemModifiedAppearance then
                    for _, sid in ipairs(allSources) do
                        local hasOK, hasIt = pcall(C_TransmogCollection.PlayerHasTransmogItemModifiedAppearance, sid)
                        if hasOK and hasIt then
                            isCollected = true
                            break
                        end
                    end
                end
            end
            
            -- FALLBACK: GetAppearanceSources (class-filtered)
            if not isCollected and appearanceID and C_TransmogCollection.GetAppearanceSources then
                local srcOK, sources = pcall(C_TransmogCollection.GetAppearanceSources, appearanceID)
                if srcOK and sources then
                    for _, src in ipairs(sources) do
                        if type(src) == "table" and src.isCollected then
                            isCollected = true
                            break
                        end
                    end
                end
            end
            
            -- LAST RESORT: specific sourceID
            if not isCollected and sourceID and C_TransmogCollection.PlayerHasTransmogItemModifiedAppearance then
                local pOK, result = pcall(C_TransmogCollection.PlayerHasTransmogItemModifiedAppearance, sourceID)
                if pOK and result then isCollected = true end
            end
        end
    end
    
    if hasAppearance then
        tooltip:AddLine(" ")
        if isCollected then
            tooltip:AddLine("|cff00ff00Appearance collected|r")
        else
            tooltip:AddLine("|cffff4444Appearance NOT collected|r")
            if ETB.SourceLookup then
                local srcText = ETB.SourceLookup:GetSourceText(itemID)
                if srcText then tooltip:AddLine("|cff888888Source: " .. srcText .. "|r") end
            end
        end
    end
    
    if ETB.Wishlist and ETB.Wishlist:HasItem(itemID) then
        tooltip:AddLine("|cffffcc00On your wishlist|r")
    end
    tooltip:Show()
end

function CT:Initialize()
    if not GameTooltip then return end
    GameTooltip:HookScript("OnTooltipSetItem", OnTooltipSetItem)
    if ItemRefTooltip then ItemRefTooltip:HookScript("OnTooltipSetItem", OnTooltipSetItem) end
    if ShoppingTooltip1 then ShoppingTooltip1:HookScript("OnTooltipSetItem", OnTooltipSetItem) end
    if ShoppingTooltip2 then ShoppingTooltip2:HookScript("OnTooltipSetItem", OnTooltipSetItem) end
    ETB:Debug("CollectionTooltip", "Tooltip hooks installed")
end
