-- SimWishlist_Tooltip.lua
-- Injects upgrade % into item tooltips for items on your wishlist.

local function GetWishlistData(itemID)
    if not SimWishlistDB then return nil end
    local name  = UnitName("player") or "Unknown"
    local realm = GetRealmName() or "Unknown"
    local key   = name .. "-" .. realm
    local charData = SimWishlistDB[key]
    if not charData or not charData.items then return nil end

    itemID = tonumber(itemID)
    if not itemID then return nil end

    for _, item in ipairs(charData.items) do
        if item.itemID == itemID then
            return item
        end
    end
    return nil
end

-- Tier set name filter (same keywords as main lua)
local TIER_KEYWORDS = {
    "Relentless Rider's", "Devouring Reaver's", "Luminous Bloom",
    "Black Talon", "Primal Sentry's", "Voidbreaker's",
    "Ra-den's Chosen", "Luminant Verdict's", "Blind Oath's",
    "Grim Jest", "Primal Core", "Abyssal Immolator", "Night Ender",
}

local function IsTierPiece(itemID)
    local name = GetItemInfo(itemID)
    if not name then return false end
    for _, kw in ipairs(TIER_KEYWORDS) do
        if name:find(kw, 1, true) then return true end
    end
    return false
end

-- Wait for PLAYER_LOGIN so SavedVars exist, then register the hook
local loader = CreateFrame("Frame")
loader:RegisterEvent("PLAYER_LOGIN")
loader:SetScript("OnEvent", function(self)
    self:UnregisterAllEvents()

    if not TooltipDataProcessor then return end

    TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item, function(tooltip, data)
        if tooltip ~= GameTooltip and tooltip ~= ItemRefTooltip then return end

        local itemID
        if data and data.id then
            itemID = data.id
        elseif tooltip.GetItem then
            local _, link = tooltip:GetItem()
            if link then
                itemID = tonumber(link:match("item:(%d+)"))
            end
        end

        if not itemID then return end
        if IsTierPiece(itemID) then return end

        local item = GetWishlistData(itemID)
        if not item then return end

        local upg = item.upgrade or 0
        if upg <= 0 then return end

        local color = upg >= 2.0 and "44ff88" or upg >= 1.0 and "ffee00" or "ffffff"
        local line = string.format("|cffcc88ffSimWishlist:|r |cff%s+%.2f%% upgrade|r", color, upg)

        -- Check BiS
        -- We need to compute per-slot best across all items
        local name  = UnitName("player") or "Unknown"
        local realm = GetRealmName() or "Unknown"
        local key   = name .. "-" .. realm
        local charData = SimWishlistDB and SimWishlistDB[key]
        if charData and charData.items then
            local mySlot = nil
            local _, _, _, _, _, _, _, _, invType = GetItemInfo(itemID)
            if invType and invType ~= "" then
                mySlot = invType
            end

            if mySlot then
                local bestUpg = 0
                local bestID  = nil
                for _, it in ipairs(charData.items) do
                    if not IsTierPiece(it.itemID) then
                        local _, _, _, _, _, _, _, _, iType = GetItemInfo(it.itemID)
                        if iType == mySlot and (it.upgrade or 0) > bestUpg then
                            bestUpg = it.upgrade
                            bestID  = it.itemID
                        end
                    end
                end
                if bestID == itemID then
                    line = line .. " |cff00ff00[BiS]|r"
                end
            end
        end

        tooltip:AddLine(line)
        tooltip:Show()
    end)
end)