-- =============================================================
-- Specs/EvokerPreservation.lua
-- Nem: Healer Alerts — Preservation Evoker spec module.
-- =============================================================

local NHA = _G.NemHealerAlerts
if not NHA then return end

do return end

local pairs, ipairs       = pairs, ipairs
local GetTime             = GetTime
local UnitExists          = UnitExists
local C_UnitAuras         = C_UnitAuras
local issecretvalue       = NHA.issecretvalue

-- =============================================================
-- Spell IDs
-- =============================================================
local ECHO_ID              = 364343   -- SpellID, whitelisted
local REVERSION_ID         = 366155   -- SpellID, whitelisted
local ECHO_REVERSION_ID    = 367364   -- SpellID, whitelisted
local DREAM_BREATH_ID      = 355941   -- SpellID, whitelisted
local ECHO_DREAM_BREATH_ID = 376788   -- SpellID, whitelisted
local LIFEBIND_ID          = 373267   -- SpellID, whitelisted

local REVERSION_IDS = {
    [366155] = true,  -- SpellID
    [367364] = true,  -- SpellID
}

-- =============================================================
-- State
-- =============================================================
local state = {
    revActive       = false,
    revWasActive    = false,
    playerAuraPending   = false,
    rosterUpdatePending = false,
}

-- =============================================================
-- Alert Definitions
-- =============================================================
local ALERTS = {
    {
        key          = "rev",
        label        = "Reversion Low",
        text         = "* REVERSION LOW *",
        defaultColor = { r = 0.200, g = 0.576, b = 0.498 },  -- #33937F
        hasTimer     = false,
        yOffset      = -30,
        defaultSound = "Ping",
    },
}

-- =============================================================
-- Defaults
-- =============================================================
local DEFAULTS = {
    revEnabled           = true,
    revSoundEnabled      = true,
    revSoundName         = "Ping",
    revThresholdDungeon  = 2,
    revThresholdRaid     = 4,
    alertColors          = {
        rev  = { r = 0.200, g = 0.576, b = 0.498 },
    },
}

-- =============================================================
-- Reversion Count Scan
-- =============================================================
local function CountActiveReversions()
    local count = 0
    for _, unit in ipairs(NHA:GetGroupUnits()) do
        if UnitExists(unit) and C_UnitAuras and C_UnitAuras.GetAuraDataBySpellID then
            for spellId in pairs(REVERSION_IDS) do
                local aura = C_UnitAuras.GetAuraDataBySpellID(unit, spellId, "HELPFUL")
                if aura and aura.auraInstanceID ~= 0
                   and aura.sourceUnit == "player" then
                    count = count + 1
                end
            end
        end
    end
    return count
end

local function GetReversionThreshold()
    local db = NHA.db
    if IsInRaid and IsInRaid() then
        return db.revThresholdRaid or 4
    end
    return db.revThresholdDungeon or 2
end

-- =============================================================
-- Module Table
-- =============================================================
local EvokerPres = {
    class     = "EVOKER",
    specIndex = 2,
    specID    = 1468,
    specName  = "Preservation",
    alerts    = ALERTS,
    defaults  = DEFAULTS,
    state     = state,

    extraEvents = {
        "UNIT_AURA",
    },
}

-- =============================================================
-- ClearAllGlows
-- =============================================================
function EvokerPres:ClearAllGlows()
end

-- =============================================================
-- ClearAllState
-- =============================================================
function EvokerPres:ClearAllState()
    state.revActive     = false
    state.revWasActive  = false
    self:ClearAllGlows()
end

-- =============================================================
-- Evaluate
-- =============================================================
function EvokerPres:Evaluate(db)
    local revLow = false
    if db.revEnabled and UnitAffectingCombat("player") then
        local count     = CountActiveReversions()
        local threshold = GetReversionThreshold()
        revLow = count < threshold
    end
    state.revActive = revLow

    if revLow and not state.revWasActive then NHA:PlayAlertSound("rev") end
    state.revWasActive = revLow
end

-- =============================================================
-- OnActivate
-- =============================================================
function EvokerPres:OnActivate(db)
end

-- =============================================================
-- OnEvent
-- =============================================================
function EvokerPres:OnEvent(db, event, ...)
    if event == "UNIT_AURA" then
        local unit = ...
        if unit == "player" then
            if not state.playerAuraPending then
                state.playerAuraPending = true
                C_Timer.After(0.1, function()
                    state.playerAuraPending = false
                    NHA.Evaluate()
                end)
            end
            return true
        end
        return false
    end

    if event == "GROUP_ROSTER_UPDATE" then
        if not state.rosterUpdatePending then
            state.rosterUpdatePending = true
            C_Timer.After(0.5, function()
                state.rosterUpdatePending = false
                NHA.Evaluate()
            end)
        end
        return true
    end

    return false
end

-- =============================================================
-- Register
-- =============================================================
NHA:RegisterSpec("EVOKER_PRES", EvokerPres)
