--[[
============================================================================
ETB - ItemAPI.lua
============================================================================
Wrapper for Blizzard's item-related APIs.
Handles GetItemInfo, GetItemInfoInstant, and item caching.

USAGE:
    local api = ETB.ItemAPI
    local info = api:GetItemInfo(12345)
    local instant = api:GetInstantInfo(12345)
    api:RequestItem(12345)  -- Async request
============================================================================
]]--

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

ETB.ItemAPI = {}
local ItemAPI = ETB.ItemAPI

-- =============================================================================
-- ITEM INFO (Synchronous when cached, nil if not)
-- =============================================================================

function ItemAPI:GetItemInfo(itemID)
    if not itemID then return nil end
    
    local name, link, quality, itemLevel, minLevel, itemType, itemSubType,
          stackCount, equipLoc, texture, vendorPrice, classID, subclassID = GetItemInfo(itemID)
    
    if not name then
        return nil  -- Not cached yet
    end
    
    return {
        itemID = itemID,
        name = name,
        link = link,
        quality = quality,
        itemLevel = itemLevel,
        minLevel = minLevel,
        itemType = itemType,
        itemSubType = itemSubType,
        stackCount = stackCount,
        equipLoc = equipLoc,
        texture = texture,
        vendorPrice = vendorPrice,
        classID = classID,
        subclassID = subclassID,
    }
end

-- =============================================================================
-- INSTANT INFO (Always available, limited data)
-- =============================================================================

function ItemAPI:GetInstantInfo(itemID)
    if not itemID then return nil end
    if not GetItemInfoInstant then return nil end
    
    local itemID, itemType, itemSubType, equipLoc, texture, classID, subclassID = GetItemInfoInstant(itemID)
    
    if not itemID then
        return nil
    end
    
    return {
        itemID = itemID,
        itemType = itemType,
        itemSubType = itemSubType,
        equipLoc = equipLoc,
        texture = texture,
        classID = classID,
        subclassID = subclassID,
    }
end

-- =============================================================================
-- ASYNC REQUEST
-- =============================================================================

function ItemAPI:RequestItem(itemID)
    if not itemID then return end
    -- GetItemInfo triggers server request if item not cached
    GetItemInfo(itemID)
end

function ItemAPI:RequestItems(itemIDs)
    if not itemIDs then return end
    for _, itemID in ipairs(itemIDs) do
        self:RequestItem(itemID)
    end
end

-- =============================================================================
-- CONVENIENCE FUNCTIONS
-- =============================================================================

function ItemAPI:GetName(itemID)
    local info = self:GetItemInfo(itemID)
    return info and info.name or nil
end

function ItemAPI:GetLink(itemID)
    local info = self:GetItemInfo(itemID)
    return info and info.link or nil
end

function ItemAPI:GetQuality(itemID)
    local info = self:GetItemInfo(itemID)
    return info and info.quality or nil
end

function ItemAPI:GetTexture(itemID)
    -- Try instant first (always available)
    local instant = self:GetInstantInfo(itemID)
    if instant and instant.texture then
        return instant.texture
    end
    
    -- Fall back to full info
    local info = self:GetItemInfo(itemID)
    return info and info.texture or nil
end

function ItemAPI:GetEquipLoc(itemID)
    -- Try instant first (always available)
    local instant = self:GetInstantInfo(itemID)
    if instant and instant.equipLoc then
        return instant.equipLoc
    end
    
    -- Fall back to full info
    local info = self:GetItemInfo(itemID)
    return info and info.equipLoc or nil
end

function ItemAPI:GetSubType(itemID)
    -- Try instant first
    local instant = self:GetInstantInfo(itemID)
    if instant and instant.itemSubType then
        return instant.itemSubType
    end
    
    local info = self:GetItemInfo(itemID)
    return info and info.itemSubType or nil
end

function ItemAPI:GetClassAndSubclass(itemID)
    local instant = self:GetInstantInfo(itemID)
    if instant then
        return instant.classID, instant.subclassID
    end
    
    local info = self:GetItemInfo(itemID)
    if info then
        return info.classID, info.subclassID
    end
    
    return nil, nil
end

-- =============================================================================
-- ARMOR/WEAPON TYPE DETECTION
-- =============================================================================

-- Item class IDs
local ITEM_CLASS_WEAPON = 2
local ITEM_CLASS_ARMOR = 4

function ItemAPI:IsWeapon(itemID)
    local classID, _ = self:GetClassAndSubclass(itemID)
    return classID == ITEM_CLASS_WEAPON
end

function ItemAPI:IsArmor(itemID)
    local classID, _ = self:GetClassAndSubclass(itemID)
    return classID == ITEM_CLASS_ARMOR
end

function ItemAPI:GetArmorType(itemID)
    local classID, subclassID = self:GetClassAndSubclass(itemID)
    if classID ~= ITEM_CLASS_ARMOR then
        return nil
    end
    
    -- Armor subclass IDs: 1=Cloth, 2=Leather, 3=Mail, 4=Plate
    -- 0=Miscellaneous (cosmetic), 6=Shield
    if subclassID >= 1 and subclassID <= 4 then
        return subclassID
    end
    
    return nil  -- Cosmetic or shield
end

function ItemAPI:GetWeaponType(itemID)
    local classID, subclassID = self:GetClassAndSubclass(itemID)
    if classID ~= ITEM_CLASS_WEAPON then
        return nil
    end
    
    -- Get subtype name for weapon type mapping
    local subTypeName = self:GetSubType(itemID)
    if subTypeName and ETB.WeaponTypes then
        return ETB.WeaponTypes:FromSubtypeName(subTypeName)
    end
    
    return nil
end

-- =============================================================================
-- ITEM AVAILABILITY CHECK
-- =============================================================================

function ItemAPI:IsItemCached(itemID)
    local info = self:GetItemInfo(itemID)
    return info ~= nil
end

function ItemAPI:WaitForItem(itemID, callback, maxAttempts)
    maxAttempts = maxAttempts or 10
    local attempts = 0
    
    local function checkItem()
        attempts = attempts + 1
        local info = self:GetItemInfo(itemID)
        
        if info then
            callback(info)
        elseif attempts < maxAttempts then
            -- Request again and retry
            self:RequestItem(itemID)
            C_Timer.After(0.1, checkItem)
        else
            -- Give up, call with nil
            callback(nil)
        end
    end
    
    checkItem()
end

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

function ItemAPI:TestItem(itemID)
    ETB:Print(string.format("|cff00ffff[ItemAPI]|r Testing item %d:", itemID))
    
    local instant = self:GetInstantInfo(itemID)
    if instant then
        ETB:Print(string.format("  Instant: equipLoc=%s, classID=%s, subclassID=%s",
            tostring(instant.equipLoc), tostring(instant.classID), tostring(instant.subclassID)))
    else
        ETB:Print("  Instant: NOT AVAILABLE")
    end
    
    local info = self:GetItemInfo(itemID)
    if info then
        ETB:Print(string.format("  Full: name=%s, quality=%d, itemType=%s",
            info.name, info.quality or 0, info.itemType or "nil"))
    else
        ETB:Print("  Full: NOT CACHED (requesting...)")
        self:RequestItem(itemID)
    end
end

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