-- =============================================================
-- Specs/ShamanRestoration.lua
-- Nem: Healer Alerts — Restoration Shaman spec module
--
-- FULLY SELF-CONTAINED. Zero dependencies on other spec modules.
--
-- Alerts:
--   1. Earth Shield missing  (974/383648) — text alert
--   2. Riptide low coverage  (61295)      — count-based text alert
--
-- Whitelisted spell IDs (non-secret in Midnight):
--   974     - Earth Shield
--   383648  - Earth Shield (Elemental Orbit / alternate ID)
--   61295   - Riptide
-- =============================================================

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 EARTH_SHIELD_IDS = {
    [974]    = true,   -- Earth Shield
    [383648] = true,   -- Earth Shield (alternate)
}
local RIPTIDE_ID = 61295

-- =============================================================
-- State
-- =============================================================
local state = {
    -- Earth Shield missing
    esActive       = false,
    esWasActive    = false,
    -- Riptide low
    rtActive       = false,
    rtWasActive    = false,
    -- Debounce
    playerAuraPending   = false,
    rosterUpdatePending = false,
}

-- =============================================================
-- Alert Definitions
-- =============================================================
local ES_Y = -30
local RT_Y = -60

local ALERTS = {
    {
        key          = "es",
        label        = "Earth Shield Missing",
        text         = "* EARTH SHIELD MISSING *",
        defaultColor = { r = 0.000, g = 0.439, b = 0.867 },  -- Shaman class color
        hasTimer     = false,
        yOffset      = ES_Y,
        defaultSound = "Bell",
    },
    {
        key          = "rt",
        label        = "Riptide Low",
        text         = "* RIPTIDE LOW *",
        defaultColor = { r = 0.200, g = 0.600, b = 1.000 },  -- Lighter blue
        hasTimer     = false,
        yOffset      = RT_Y,
        defaultSound = "Ping",
    },
}

-- =============================================================
-- Defaults
-- =============================================================
local DEFAULTS = {
    esEnabled            = true,
    esSoundEnabled       = true,
    esSoundName          = "Bell",
    rtEnabled            = true,
    rtSoundEnabled       = true,
    rtSoundName          = "Ping",
    rtThresholdDungeon   = 2,
    rtThresholdRaid      = 4,
    alertColors          = {
        es = { r = 0.000, g = 0.439, b = 0.867 },
        rt = { r = 0.200, g = 0.600, b = 1.000 },
    },
}

-- =============================================================
-- Earth Shield Scan
-- Priority: focus > target (friendly) > any group member
-- =============================================================
local function HasPlayerEarthShield()
    -- Check all group units for any player-cast Earth Shield
    for _, unit in ipairs(NHA:GetGroupUnits()) do
        if UnitExists(unit) and C_UnitAuras and C_UnitAuras.GetAuraDataBySpellID then
            for spellId in pairs(EARTH_SHIELD_IDS) do
                local aura = C_UnitAuras.GetAuraDataBySpellID(unit, spellId, "HELPFUL")
                if aura and aura.auraInstanceID ~= 0
                   and aura.sourceUnit == "player" then
                    return true
                end
            end
        end
    end
    return false
end

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

local function GetRTThreshold()
    local db = NHA.db
    if IsInRaid and IsInRaid() then
        return db.rtThresholdRaid or 4
    end
    return db.rtThresholdDungeon or 2
end

-- =============================================================
-- Module Table
-- =============================================================
local ShamanResto = {
    class     = "SHAMAN",
    specIndex = 3,  -- Restoration is spec index 3
    specName  = "Restoration",
    alerts    = ALERTS,
    defaults  = DEFAULTS,
    state     = state,

    extraEvents = {
        "UNIT_AURA",
    },
}

-- =============================================================
-- ClearAllGlows / ClearAllState
-- =============================================================
function ShamanResto:ClearAllGlows()
    -- No glow frames in this module
end

function ShamanResto:ClearAllState()
    state.esActive    = false
    state.esWasActive = false
    state.rtActive    = false
    state.rtWasActive = false
end

-- =============================================================
-- Evaluate
-- =============================================================
function ShamanResto:Evaluate(db)
    -- Earth Shield missing (combat only)
    local esMissing = false
    if db.esEnabled and UnitAffectingCombat("player") then
        esMissing = not HasPlayerEarthShield()
    end
    state.esActive = esMissing

    -- Riptide low coverage (combat only)
    local rtLow = false
    if db.rtEnabled and UnitAffectingCombat("player") then
        local count     = CountActiveRiptides()
        local threshold = GetRTThreshold()
        rtLow = count < threshold
    end
    state.rtActive = rtLow

    -- Rising-edge sounds
    if esMissing and not state.esWasActive then NHA:PlayAlertSound("es") end
    if rtLow     and not state.rtWasActive then NHA:PlayAlertSound("rt") end
    state.esWasActive = esMissing
    state.rtWasActive = rtLow
end

-- =============================================================
-- OnActivate
-- =============================================================
function ShamanResto:OnActivate(db)
    -- Nothing spec-specific
end

-- =============================================================
-- OnEvent
-- =============================================================
function ShamanResto:OnEvent(db, event, ...)
    if event == "UNIT_AURA" 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

    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

-- =============================================================
-- BuildOptions
-- =============================================================
function ShamanResto:BuildOptions(parent, db, helpers)
    -- No spec-specific options beyond the standard alert/sound toggles
end

-- =============================================================
-- Register
-- =============================================================
NHA:RegisterSpec("SHAMAN_RESTO", ShamanResto)
