local addonName, NXR = ...

-- ============================================================================
-- Spec & class metadata (built at ADDON_LOADED)
-- ============================================================================

NXR.classData      = {}   -- classID -> { classID, className, classFileName, specs }
NXR.specData       = {}   -- specID  -> { specID, specName, icon, role, classID, className, classFileName }
NXR.roleSpecs      = {}   -- role    -> sorted array of spec entries
NXR.sortedClassIDs = {}   -- ordered array of classIDs

-- Ranged DPS specIDs (WoW 12.x) — all other DAMAGER specs are melee
local RANGED_SPEC_IDS = {
    [102]  = true,  -- Balance Druid
    [253]  = true,  -- Beast Mastery Hunter
    [254]  = true,  -- Marksmanship Hunter
    [62]   = true,  -- Arcane Mage
    [63]   = true,  -- Fire Mage
    [64]   = true,  -- Frost Mage
    [258]  = true,  -- Shadow Priest
    [262]  = true,  -- Elemental Shaman
    [265]  = true,  -- Affliction Warlock
    [266]  = true,  -- Demonology Warlock
    [267]  = true,  -- Destruction Warlock
    [1467] = true,  -- Devastation Evoker
    [1473] = true,  -- Augmentation Evoker
}

function NXR.BuildSpecData()
    wipe(NXR.classData)
    wipe(NXR.specData)
    wipe(NXR.roleSpecs)
    wipe(NXR.sortedClassIDs)

    NXR.roleSpecs.HEALER  = {}
    NXR.roleSpecs.DAMAGER = {}
    NXR.roleSpecs.MELEE   = {}
    NXR.roleSpecs.RANGED  = {}
    NXR.roleSpecs.TANK    = {}

    for i = 1, GetNumClasses() do
        local className, classFileName, classID = GetClassInfo(i)
        if classID then
            table.insert(NXR.sortedClassIDs, classID)
            local entry = {
                classID       = classID,
                className     = className,
                classFileName = classFileName,
                specs         = {},
            }

            for j = 1, GetNumSpecializationsForClassID(classID) do
                local specID, specName, _, icon, role =
                    GetSpecializationInfoForClassID(classID, j)
                if specID then
                    local s = {
                        specID        = specID,
                        specName      = specName,
                        icon          = icon,
                        role          = role,
                        classID       = classID,
                        className     = className,
                        classFileName = classFileName,
                    }
                    table.insert(entry.specs, s)
                    NXR.specData[specID] = s
                    if role == "DAMAGER" then
                        table.insert(NXR.roleSpecs.DAMAGER, s)
                        if RANGED_SPEC_IDS[specID] then
                            table.insert(NXR.roleSpecs.RANGED, s)
                        else
                            table.insert(NXR.roleSpecs.MELEE, s)
                        end
                    elseif NXR.roleSpecs[role] then
                        table.insert(NXR.roleSpecs[role], s)
                    end
                end
            end

            NXR.classData[classID] = entry
        end
    end

    -- Sort each role group by class name then spec name
    local sortFn = function(a, b)
        if a.className == b.className then
            return a.specName < b.specName
        end
        return a.className < b.className
    end
    for _, role in ipairs({"HEALER", "DAMAGER", "MELEE", "RANGED", "TANK"}) do
        table.sort(NXR.roleSpecs[role], sortFn)
    end

    NXR.Debug("BuildSpecData:", NXR.TableCount(NXR.specData), "specs across",
        #NXR.sortedClassIDs, "classes |",
        #NXR.roleSpecs.HEALER, "healers,",
        #NXR.roleSpecs.MELEE, "melee,",
        #NXR.roleSpecs.RANGED, "ranged,",
        #NXR.roleSpecs.TANK, "tanks")
end

-- ============================================================================
-- Challenge CRUD (Story 2-1)
-- ============================================================================

local function NextID()
    local max = 0
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id > max then max = c.id end
    end
    return max + 1
end

local function GenerateUID()
    return time() .. "-" .. math.random(100000, 999999)
end

function NXR.AddChallenge(data)
    local isFirst = #NelxRatedDB.challenges == 0
    local c = {
        id         = NextID(),
        uid        = data.uid or GenerateUID(),
        name       = data.name or "Untitled",
        goalRating = data.goalRating or 1800,
        brackets   = data.brackets or {},
        specs      = data.specs or {},
        classes    = data.classes or {},
    }
    if data.active ~= nil then
        c.active = data.active
    else
        c.active = isFirst
    end
    table.insert(NelxRatedDB.challenges, c)
    NXR.Debug("AddChallenge: id=" .. c.id, "'" .. c.name .. "'",
        "goal=" .. c.goalRating,
        "brackets=" .. NXR.TableCount(c.brackets),
        "specs=" .. NXR.TableCount(c.specs),
        "active=" .. tostring(c.active))
    if isFirst and NXR.RefreshOverlay then
        NXR.RefreshOverlay()
    end
    return c
end

function NXR.RemoveChallenge(id)
    local wasActive = false
    for i, c in ipairs(NelxRatedDB.challenges) do
        if c.id == id then
            wasActive = c.active
            if c.uid then
                NelxRatedDB.deletedChallengeUIDs = NelxRatedDB.deletedChallengeUIDs or {}
                NelxRatedDB.deletedChallengeUIDs[c.uid] = true
            end
            table.remove(NelxRatedDB.challenges, i)
            break
        end
    end
    if wasActive and NXR.RefreshOverlay then
        NXR.RefreshOverlay()
    end
end

function NXR.UpdateChallenge(id, data)
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id == id then
            if data.name ~= nil then c.name = data.name end
            if data.goalRating ~= nil then c.goalRating = data.goalRating end
            if data.brackets then c.brackets = data.brackets end
            if data.specs then c.specs = data.specs end
            if data.classes then c.classes = data.classes end
            if c.active and NXR.RefreshOverlay then
                NXR.RefreshOverlay()
            end
            return c
        end
    end
end

function NXR.SetActiveChallenge(id)
    NXR.Debug("SetActiveChallenge: id=" .. tostring(id))
    for _, c in ipairs(NelxRatedDB.challenges) do
        c.active = (c.id == id)
    end
    if NXR.RefreshOverlay then
        NXR.RefreshOverlay()
    end
end

function NXR.GetActiveChallenge()
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.active then return c end
    end
    return nil
end

-- ============================================================================
-- Manual spec/class completion (Story: Manual Spec Completion)
-- ============================================================================

function NXR.SetSpecCompleted(challengeID, specID, completed)
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id == challengeID then
            c.completedSpecs = c.completedSpecs or {}
            c.completedSpecs[specID] = completed and true or nil
            return
        end
    end
end

function NXR.IsSpecCompleted(challengeID, specID)
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id == challengeID then
            return c.completedSpecs and c.completedSpecs[specID] == true
        end
    end
    return false
end

function NXR.SetClassCompleted(challengeID, classID, completed)
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id == challengeID then
            c.completedClasses = c.completedClasses or {}
            c.completedClasses[classID] = completed and true or nil
            return
        end
    end
end

function NXR.IsClassCompleted(challengeID, classID)
    for _, c in ipairs(NelxRatedDB.challenges) do
        if c.id == challengeID then
            return c.completedClasses and c.completedClasses[classID] == true
        end
    end
    return false
end

-- ============================================================================
-- Initialization (called from Core.lua ADDON_LOADED)
-- ============================================================================

function NXR.InitChallenges()
    local challenges = NelxRatedDB.challenges

    -- Backfill UIDs for pre-9-6 challenges
    for _, c in ipairs(challenges) do
        if not c.uid then
            c.uid = GenerateUID()
            NXR.Debug("InitChallenges: backfilled uid for '" .. c.name .. "': " .. c.uid)
        end
    end

    if #challenges > 0 and not NXR.GetActiveChallenge() then
        challenges[1].active = true
        NXR.Debug("InitChallenges: auto-activated '" .. challenges[1].name .. "'")
    end
    NXR.Debug("InitChallenges:", #challenges, "challenges loaded")
end
