-- ============================================================
-- Asylum Compagnon — SpellInfo.lua
-- Ajoute SpellID + IconID aux infobulles de sorts ET d'auras
-- (buffs / debuffs), et ItemID + IconID aux infobulles d'objets.
-- À la manière de « idTip / SpellID ». Activable : NS.db.spellIDTooltip.
-- ============================================================

local ADDON_NAME, NS = ...

local SI = {}
NS.SpellInfo = SI

local ACCENT = "|cff" .. (NS.ACCENT or "a78bfa")

local function enabled()
    return not (NS.db and NS.db.spellIDTooltip == false)
end

local function isSecret(v)
    return issecretvalue and issecretvalue(v)
end

local function SpellTex(id)
    local fn = (C_Spell and C_Spell.GetSpellTexture) or GetSpellTexture
    if not fn then return nil end
    local ok, tex = pcall(fn, id)
    if ok then return tex end
end

local function ItemTex(id)
    local fn = (C_Item and C_Item.GetItemIconByID) or GetItemIcon
    if not fn then return nil end
    local ok, tex = pcall(fn, id)
    if ok then return tex end
end

-- ---------- Sorts (barre d'action, grimoire…) ----------
local function OnSpell(tt, data)
    if not enabled() then return end
    if tt ~= GameTooltip and tt ~= ItemRefTooltip then return end
    local id = data and data.id
    if not id or isSecret(id) then return end
    tt:AddDoubleLine(ACCENT .. "SpellID|r", tostring(id))
    local tex = SpellTex(id)
    if tex then tt:AddDoubleLine(ACCENT .. "IconID|r", tostring(tex)) end
    tt:Show()
end

-- ---------- Objets ----------
local function OnItem(tt, data)
    if not enabled() then return end
    if tt ~= GameTooltip and tt ~= ItemRefTooltip then return end
    local id = data and data.id
    if not id and tt.GetItem then
        local ok, _, link = pcall(tt.GetItem, tt)
        if ok and link then id = tonumber(link:match("item:(%d+)")) end
    end
    if not id then return end
    tt:AddDoubleLine(ACCENT .. "ItemID|r", tostring(id))
    local tex = ItemTex(id)
    if tex then tt:AddDoubleLine(ACCENT .. "IconID|r", tostring(tex)) end
    tt:Show()
end

-- ---------- Auras (buffs / debuffs) ----------
-- Les infobulles d'auras passent par SetUnitBuff/Debuff/Aura, pas par le
-- TooltipDataProcessor des sorts → on les accroche à part.
local function AddAura(tt, aura)
    if not enabled() or not aura then return end
    local sid = aura.spellId
    if not sid or isSecret(sid) then return end
    tt:AddDoubleLine(ACCENT .. "SpellID|r", tostring(sid))
    local tex = aura.icon or SpellTex(sid)
    if tex then tt:AddDoubleLine(ACCENT .. "IconID|r", tostring(tex)) end
    tt:Show()
end

local function HookAuras()
    if not C_UnitAuras then return end
    if GameTooltip.SetUnitBuff and C_UnitAuras.GetBuffDataByIndex then
        hooksecurefunc(GameTooltip, "SetUnitBuff", function(self, unit, index, filter)
            local ok, aura = pcall(C_UnitAuras.GetBuffDataByIndex, unit, index, filter)
            if ok then pcall(AddAura, self, aura) end
        end)
    end
    if GameTooltip.SetUnitDebuff and C_UnitAuras.GetDebuffDataByIndex then
        hooksecurefunc(GameTooltip, "SetUnitDebuff", function(self, unit, index, filter)
            local ok, aura = pcall(C_UnitAuras.GetDebuffDataByIndex, unit, index, filter)
            if ok then pcall(AddAura, self, aura) end
        end)
    end
    if GameTooltip.SetUnitAura and C_UnitAuras.GetAuraDataByIndex then
        hooksecurefunc(GameTooltip, "SetUnitAura", function(self, unit, index, filter)
            local ok, aura = pcall(C_UnitAuras.GetAuraDataByIndex, unit, index, filter)
            if ok then pcall(AddAura, self, aura) end
        end)
    end
end

local installed = false
local function Install()
    if installed then return end
    installed = true
    if TooltipDataProcessor and TooltipDataProcessor.AddTooltipPostCall and Enum and Enum.TooltipDataType then
        if Enum.TooltipDataType.Spell then
            TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Spell,
                function(tt, data) pcall(OnSpell, tt, data) end)
        end
        if Enum.TooltipDataType.Item then
            TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item,
                function(tt, data) pcall(OnItem, tt, data) end)
        end
    end
    pcall(HookAuras)
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function() pcall(Install) end)
