local addonName, TOAST = ...

local GOLD = { 0.878, 0.745, 0.365 }  -- #E0BE5D

TOAST.RANK_COLORS = {
    ["ABYSSAL JELLYLORD"]       = { 0.8118, 0.0745, 0.4431 },  -- #cf1371
    ["CALYPSO EXPEDITION LEAD"] = { 1.0000, 0.5020, 0.7490 },  -- #ff80bf
    ["HADAL SYSTEMS OVERSEER"]  = { 0.4353, 0.3333, 0.6392 },  -- #6f55a3
    ["SUBNAUTIC COMMS SPEC."]   = { 0.7137, 0.6157, 0.9137 },  -- #b69de9
    ["AQUATIC OPS ENGINEER"]    = { 0.0000, 0.3059, 0.3922 },  -- #004e64
    ["SENIOR DIVE TECHNICIAN"]  = { 0.0941, 0.4549, 0.8039 },  -- #1874cd
    ["SHALLOW WATER SPEC."]     = { 0.2000, 0.6000, 1.0000 },  -- #3399ff
    ["ALBINO DOLPHIN HANDLER"]  = { 0.2392, 0.6824, 0.9608 },  -- #3daef5
    ["DECKHAND"]                = { 0.4980, 0.8588, 1.0000 },  -- #7fdbff
}

local alreadyProcessed = false

local memberLookup
local leaderboardLookup

local function BuildLookups()
    memberLookup = {}
    for _, entry in ipairs(TOAST.members) do
        memberLookup[entry.name] = entry
    end

    leaderboardLookup = {}
    for _, entry in ipairs(TOAST.leaderboard) do
        leaderboardLookup[entry.name] = entry
    end
end

local function AddSeamenTooltipLines(tooltip, charName)
    if tooltip and tooltip._toastPresenceOnly then return end
    if alreadyProcessed then return end
    alreadyProcessed = true

    if not memberLookup then
        BuildLookups()
    end

    local lbEntry = leaderboardLookup[charName]
    local memberEntry = memberLookup[charName]
    if not lbEntry and not memberEntry then return end

    tooltip:AddLine(" ")

    -- Line 1: prefix icon + label + discord name (left), leaderboard position (right)
    local discordNickname = (lbEntry and lbEntry.discord_nickname) or memberEntry.discord_nickname
    local p, discordName = TOAST.ParseDiscordPrefix(discordNickname)
    local left1
    if p then
        left1 = string.format("|T%s:20:20|t|cffCCCCCC%s|r |cffE8E0D4%s|r", p.tex, p.label, discordName)
    else
        left1 = string.format("|cffE8E0D4%s|r", discordName)
    end
    local right1 = lbEntry and string.format("|cffE0BE5D#%d/%d|r", lbEntry.rank, #TOAST.leaderboard) or ""
    tooltip:AddDoubleLine(left1, right1, 1, 1, 1, 1, 1, 1)

    -- Line 2: seamen rank (left), known key (right)
    local keyRealm = (memberEntry and memberEntry.realm) or (lbEntry and lbEntry.realm)
    local keyData = TOAST.GetCrewKeyDisplay and TOAST.GetCrewKeyDisplay(charName, keyRealm)
    local keyStr = keyData and string.format("+%d %s", keyData.level, keyData.short) or nil
    local seamenRank = memberEntry and memberEntry.rank

    if seamenRank or keyStr then
        local rankColor = (seamenRank and TOAST.RANK_COLORS[seamenRank]) or GOLD
        tooltip:AddDoubleLine(seamenRank or "", keyStr or "",
            rankColor[1], rankColor[2], rankColor[3], 1, 1, 1)
    end

    tooltip:AddLine(" ")
    tooltip:Show()
end

TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, function(tooltip, data)
    pcall(function()
        if tooltip ~= GameTooltip then return end
        if alreadyProcessed then return end
        local name, unit = tooltip:GetUnit()
        if not unit or not UnitIsPlayer(unit) then return end
        if not name then return end
        local charName = name:match("^([^%-]+)")
        AddSeamenTooltipLines(tooltip, charName)
    end)
end)

hooksecurefunc(GameTooltip, "Show", function(tooltip)
    pcall(function()
        if tooltip._toastPresenceOnly then return end
        if alreadyProcessed then return end
        local firstLine = GameTooltipTextLeft1 and GameTooltipTextLeft1:GetText()
        if not firstLine or firstLine == "" then return end
        if firstLine:match("^%[") or firstLine:match("^%d") then return end
        local charName = firstLine:match("^([^%-]+)")
        if not charName then return end
        charName = charName:match("^%s*(.-)%s*$")
        if not memberLookup then BuildLookups() end
        if memberLookup[charName] or leaderboardLookup[charName] then
            AddSeamenTooltipLines(tooltip, charName)
        end
    end)
end)

GameTooltip:HookScript("OnTooltipCleared", function()
    alreadyProcessed = false
end)
