------------------------------------------------------------
-- Inspect Icon Resolver v3.1
-- Auto-detection, fallback & debug logging
------------------------------------------------------------

InspectUI_MissingIcons = InspectUI_MissingIcons or {}
InspectUI_IconCache    = InspectUI_IconCache or {}

local ICON_BASE     = "media/textures/ui/InspectStats/"
local ICON_PREFIX   = "Inspect_stat_"
local ICON_SUFFIX   = ".png"
local ICON_FALLBACK_32 = ICON_BASE .. "32/" .. ICON_PREFIX .. "fallback" .. ICON_SUFFIX
local ICON_FALLBACK_64 = ICON_BASE .. "64/" .. ICON_PREFIX .. "fallback" .. ICON_SUFFIX

-- Ruta de compatibilidad con los iconos antiguos (media/ui/Inspect_stat_*.png)
local LEGACY_BASE   = "media/ui/"

local ICON_ALIAS = {
    crit = "criticalChance",
    critChance = "criticalChance",
    displayCategory = "type",
    weaponSubtype = "type",
    weaponSkills = "skills",
    usesLeft = "uses",
    lightDistance = "lightdist",
    lightStrength = "lightstr",
    fluidAmount = "fluidCapacity",
    fluidPoison = "posion",
    taintedWater = "tainted",
    bookBoredom = "reducBoredom",
    bookStress = "reducStress",
    accuracy = "hitChance",
    noiseRadius = "soundRadius",
    biteDefense = "bitedefense",
}

local function normalizeSize(size)
    local n = tonumber(size) or 32
    if n <= 32 then return 32 end
    return 64
end

local function getFallbackTexture(size)
    local fbPath = (size >= 64) and ICON_FALLBACK_64 or ICON_FALLBACK_32
    local fb = getTexture(fbPath)
    if fb then return fb end
    return getTexture(ICON_FALLBACK_32) or getTexture(ICON_FALLBACK_64)
end

------------------------------------------------------------
-- InspectUI_getStatIcon(statId, size)
-- size opcional: 32 (default) o 64. Proba rutas con carpeta o sufijo.
------------------------------------------------------------
function InspectUI_getStatIcon(statId, size)
    if not statId then
        return getFallbackTexture(32)
    end

    local requestedSize = tonumber(size) or 32
    local resolvedSize = normalizeSize(requestedSize)
    local resolvedId = ICON_ALIAS[tostring(statId)] or tostring(statId)

    -- cache key incluye el tamaño solicitado
    local cacheKey = tostring(resolvedId) .. ":" .. tostring(resolvedSize)
    if InspectUI_IconCache[cacheKey] ~= nil then
        return InspectUI_IconCache[cacheKey]
    end

    local candidates = {
        ICON_BASE .. tostring(resolvedSize) .. "/" .. ICON_PREFIX .. resolvedId .. ICON_SUFFIX,
        ICON_BASE .. ICON_PREFIX .. resolvedId .. "_" .. tostring(resolvedSize) .. ICON_SUFFIX,
        ICON_BASE .. "32/" .. ICON_PREFIX .. resolvedId .. ICON_SUFFIX,
        ICON_BASE .. "64/" .. ICON_PREFIX .. resolvedId .. ICON_SUFFIX,
        ICON_BASE .. ICON_PREFIX .. resolvedId .. ICON_SUFFIX,
        LEGACY_BASE .. ICON_PREFIX .. resolvedId .. ICON_SUFFIX,
    }

    local tex = nil
    for _, path in ipairs(candidates) do
        tex = getTexture(path)
        if tex then
            InspectUI_IconCache[cacheKey] = tex
            return tex
        end
    end

    -- Log solo 1 vez por statId+size
    if not InspectUI_MissingIcons[cacheKey] then
        print("[ItemInspection] Missing icon for stat: "
            .. tostring(statId) .. " (resolved=" .. tostring(resolvedId) .. ") size=" .. tostring(requestedSize)
            .. " (tried: " .. table.concat(candidates, "; ") .. ")")
        InspectUI_MissingIcons[cacheKey] = true
    end

    local fb = getFallbackTexture(resolvedSize)
    InspectUI_IconCache[cacheKey] = fb
    return fb
end
