-- PVP_Data.lua : Central PVP data lookup module for A.X.I.O.M.
-- Handles self-scan via GetPersonalRatedInfo() and cached DB lookups.

local _, NS = ...
local GR = NS.GR or _G.GR

-- PVP Rating Color Tiers
function GR:PvpRatingColor(rating)
    if not rating or rating == 0 then return "|cff9d9d9d" end
    if rating >= 2400 then return "|cffff8000" end      -- Gladiator+ (orange/legendary)
    if rating >= 2100 then return "|cffa335ee" end      -- Duelist (purple/epic)
    if rating >= 1800 then return "|cff0070dd" end      -- Rival (blue/rare)
    if rating >= 1600 then return "|cff1eff00" end      -- Challenger (green/uncommon)
    if rating >= 1400 then return "|cffffffff" end      -- Combatant (white)
    return "|cff9d9d9d"                                  -- Unranked (gray)
end

-- =========================================================================
-- Self-Scan (local player only)
-- Uses GetPersonalRatedInfo(bracketIndex)
-- Bracket indices: 1=2v2, 2=3v3, 4=RBG, 7=Solo Shuffle
-- =========================================================================
function GR:ScanSelfPvp()
    if not GRS_PvpDB then GRS_PvpDB = {} end

    local myName = UnitName("player")
    local myRealm = GetRealmName()
    if not myName or not myRealm then return end
    local key = myName .. "-" .. myRealm

    local r2, sb2 = GetPersonalRatedInfo(1)
    local r3, sb3 = GetPersonalRatedInfo(2)
    local rr, sbr = GetPersonalRatedInfo(4)
    local rs, sbs = GetPersonalRatedInfo(7)

    r2 = r2 or 0; sb2 = sb2 or 0
    r3 = r3 or 0; sb3 = sb3 or 0
    rr = rr or 0; sbr = sbr or 0
    rs = rs or 0; sbs = sbs or 0

    local best = math.max(sb2, sb3, sbr, sbs)
    local expBest = best  -- for self, season best IS exp

    GRS_PvpDB[key] = {
        pvp2v2  = r2,
        pvp3v3  = r3,
        pvpSolo = rs,
        pvpRbg  = rr,
        pvpBest = best,
        pvpExp  = expBest,
        pvpSpec = "-",
        source  = "self",
        time    = time(),
    }
end

-- =========================================================================
-- MAIN LOOKUP: GR:GetPvpData(name, realm)
-- Checks GRS_PvpDB (self-scan, broadcast, inspection)
-- =========================================================================
function GR:GetPvpData(name, realm)
    if not name or not realm then return nil end

    if GRS_PvpDB then
        local key = name .. "-" .. realm
        local cached = GRS_PvpDB[key]
        if cached then return cached end
    end

    return nil
end
