-- =============================================================
-- Specs/EvokerPreservation.lua
-- Nem: Healer Alerts — Preservation Evoker spec module
--
-- FULLY SELF-CONTAINED. Zero dependencies on other spec modules.
--
-- Alerts:
--   1. Reversion low coverage   (366155) — count-based text alert
--
-- Whitelisted spell IDs (non-secret in Midnight):
--   355941 - Dream Breath
--   363502 - Dream Flight
--   364343 - Echo
--   366155 - Reversion
--   367364 - Echo Reversion
--   373267 - Lifebind
--   376788 - Echo Dream Breath
-- =============================================================

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

-- =============================================================
-- TEMPORARILY DISABLED — spec not yet customized or tested.
-- Remove this block when implementation is ready.
-- =============================================================
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
local REVERSION_ID         = 366155
local ECHO_REVERSION_ID    = 367364
local DREAM_BREATH_ID      = 355941
local ECHO_DREAM_BREATH_ID = 376788
local LIFEBIND_ID          = 373267

-- All Reversion-family IDs for counting
local REVERSION_IDS = {
    [366155] = true,  -- Reversion
    [367364] = true,  -- Echo Reversion
}

-- =============================================================
-- State
-- =============================================================
local state = {
    -- Reversion low coverage
    revActive       = false,
    revWasActive    = false,
    -- Debounce
    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 },  -- Evoker class color
        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,  -- Preservation is spec index 2
    specID    = 1468, -- fallback: Preservation spec ID for secret-value contexts
    specName  = "Preservation",
    alerts    = ALERTS,
    defaults  = DEFAULTS,
    state     = state,

    extraEvents = {
        "UNIT_AURA",
    },
}

-- =============================================================
-- ClearAllGlows
-- =============================================================
-- No CUF whole-frame glow on Evoker yet. Kept as a no-op for
-- core lifecycle compat.
function EvokerPres:ClearAllGlows()
end

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

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

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

-- =============================================================
-- OnActivate
-- =============================================================
function EvokerPres:OnActivate(db)
    -- Nothing spec-specific needed at login
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)
