--[[
============================================================================
ETB - TransmogAPI.lua
============================================================================
Wrapper for Blizzard's C_TransmogCollection API.
Provides safe access with fallbacks for MoP Classic compatibility.

USAGE:
    local api = ETB.TransmogAPI
    local appearances = api:GetCategoryAppearances(1)
    local sources = api:GetAppearanceSources(visualID)
    local isCollected = api:IsAppearanceCollected(visualID)
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.TransmogAPI = {}
local TransmogAPI = ETB.TransmogAPI

-- =============================================================================
-- API AVAILABILITY CHECKS
-- =============================================================================

function TransmogAPI:IsAvailable()
    return C_TransmogCollection ~= nil
end

function TransmogAPI:HasFunction(funcName)
    return self:IsAvailable() and C_TransmogCollection[funcName] ~= nil
end

-- =============================================================================
-- CATEGORY FUNCTIONS
-- =============================================================================

function TransmogAPI:GetCategories()
    if not self:HasFunction("GetCategories") then
        return {}
    end
    
    local success, result = pcall(C_TransmogCollection.GetCategories)
    if success then
        return result or {}
    end
    
    ETB:Debug("TransmogAPI", "GetCategories failed")
    return {}
end

function TransmogAPI:GetCategoryInfo(categoryID)
    if not self:HasFunction("GetCategoryInfo") then
        return nil
    end
    
    local success, result = pcall(C_TransmogCollection.GetCategoryInfo, categoryID)
    if success then
        return result
    end
    
    ETB:Debug("TransmogAPI", "GetCategoryInfo failed for category " .. tostring(categoryID))
    return nil
end

-- =============================================================================
-- APPEARANCE FUNCTIONS
-- =============================================================================

function TransmogAPI:GetCategoryAppearances(categoryID)
    if not self:HasFunction("GetCategoryAppearances") then
        return {}
    end
    
    local success, result = pcall(C_TransmogCollection.GetCategoryAppearances, categoryID)
    if success then
        return result or {}
    end
    
    ETB:Debug("TransmogAPI", "GetCategoryAppearances failed for category " .. tostring(categoryID))
    return {}
end

function TransmogAPI:GetAppearanceSources(visualID)
    if not self:HasFunction("GetAppearanceSources") then
        return {}
    end
    
    local success, result = pcall(C_TransmogCollection.GetAppearanceSources, visualID)
    if success then
        return result or {}
    end
    
    ETB:Debug("TransmogAPI", "GetAppearanceSources failed for visualID " .. tostring(visualID))
    return {}
end

function TransmogAPI:GetSourceInfo(sourceID)
    if not self:HasFunction("GetSourceInfo") then
        return nil
    end
    
    local success, result = pcall(C_TransmogCollection.GetSourceInfo, sourceID)
    if success then
        return result
    end
    
    ETB:Debug("TransmogAPI", "GetSourceInfo failed for sourceID " .. tostring(sourceID))
    return nil
end

function TransmogAPI:GetAppearanceSourceInfo(sourceID)
    if not self:HasFunction("GetAppearanceSourceInfo") then
        return nil
    end
    
    local success, result = pcall(C_TransmogCollection.GetAppearanceSourceInfo, sourceID)
    if success then
        return result
    end
    
    return nil
end

-- =============================================================================
-- COLLECTION STATUS
-- =============================================================================

function TransmogAPI:IsAppearanceCollected(visualID)
    -- Try PlayerHasTransmog first (most reliable)
    if self:HasFunction("PlayerHasTransmog") then
        local success, result = pcall(C_TransmogCollection.PlayerHasTransmog, visualID)
        if success then
            return result
        end
    end
    
    -- Fallback: Check appearance sources
    local sources = self:GetAppearanceSources(visualID)
    for _, source in ipairs(sources) do
        if source.isCollected then
            return true
        end
    end
    
    return false
end

function TransmogAPI:GetCollectedCount(categoryID)
    local appearances = self:GetCategoryAppearances(categoryID)
    local collected = 0
    local total = #appearances
    
    for _, appearance in ipairs(appearances) do
        if appearance.isCollected then
            collected = collected + 1
        end
    end
    
    return collected, total
end

-- =============================================================================
-- ILLUSION FUNCTIONS (if available)
-- =============================================================================

function TransmogAPI:GetIllusions()
    if not self:HasFunction("GetIllusions") then
        return {}
    end
    
    local success, result = pcall(C_TransmogCollection.GetIllusions)
    if success then
        return result or {}
    end
    
    return {}
end

-- =============================================================================
-- TRANSMOG SET FUNCTIONS
-- =============================================================================

function TransmogAPI:GetSets()
    if not C_TransmogSets then return {} end
    if not C_TransmogSets.GetAllSets then return {} end
    
    local success, result = pcall(C_TransmogSets.GetAllSets)
    if success then
        return result or {}
    end
    
    return {}
end

function TransmogAPI:GetSetInfo(setID)
    if not C_TransmogSets then return nil end
    if not C_TransmogSets.GetSetInfo then return nil end
    
    local success, result = pcall(C_TransmogSets.GetSetInfo, setID)
    if success then
        return result
    end
    
    return nil
end

-- =============================================================================
-- UTILITY FUNCTIONS
-- =============================================================================

function TransmogAPI:GetVisualIDFromSourceID(sourceID)
    local info = self:GetSourceInfo(sourceID)
    return info and info.visualID or nil
end

function TransmogAPI:GetItemIDFromSourceID(sourceID)
    local info = self:GetSourceInfo(sourceID)
    return info and info.itemID or nil
end

-- =============================================================================
-- DIAGNOSTIC
-- =============================================================================

function TransmogAPI:PrintAvailableFunctions()
    ETB:Print("|cff00ffff[TransmogAPI]|r Available functions:")
    
    if not self:IsAvailable() then
        ETB:Print("  |cffff0000C_TransmogCollection not available!|r")
        return
    end
    
    local functions = {
        "GetCategories", "GetCategoryInfo", "GetCategoryAppearances",
        "GetAppearanceSources", "GetSourceInfo", "GetAppearanceSourceInfo",
        "PlayerHasTransmog", "GetIllusions",
    }
    
    for _, funcName in ipairs(functions) do
        local available = self:HasFunction(funcName)
        local status = available and "|cff00ff00YES|r" or "|cffff0000NO|r"
        ETB:Print(string.format("  %s: %s", funcName, status))
    end
    
    -- Check C_TransmogSets separately
    if C_TransmogSets then
        ETB:Print("  C_TransmogSets: |cff00ff00YES|r")
    else
        ETB:Print("  C_TransmogSets: |cffff0000NO|r")
    end
end

ETB:Debug("TransmogAPI", "TransmogAPI.lua loaded")
