-- =============================================================
-- Specs/PaladinHoly.lua
-- Nem: Healer Alerts — Holy Paladin 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 BEACON_OF_LIGHT_ID   = 53563     -- SpellID, whitelisted
local BEACON_OF_FAITH_ID   = 156910    -- SpellID, whitelisted
local BEACON_OF_SAVIOR_ID  = 1244893   -- SpellID, whitelisted
local ETERNAL_FLAME_ID     = 156322    -- SpellID, whitelisted

local BEACON_IDS = {
    [53563]   = true,  -- SpellID
    [156910]  = true,  -- SpellID
    [1244893] = true,  -- SpellID
}

-- =============================================================
-- State
-- =============================================================
local state = {
    beaconActive     = false,
    beaconWasActive  = false,
    efActive         = false,
    efWasActive      = false,
    playerAuraPending   = false,
    rosterUpdatePending = false,
}

-- =============================================================
-- Alert Definitions
-- =============================================================
local BEACON_Y = -30
local EF_Y     = -60

local ALERTS = {
    {
        key          = "beacon",
        label        = "Beacon Missing",
        text         = "* BEACON MISSING *",
        defaultColor = { r = 0.957, g = 0.549, b = 0.729 },  -- #F48CBA
        hasTimer     = false,
        yOffset      = BEACON_Y,
        defaultSound = "Bell",
    },
    {
        key          = "ef",
        label        = "Eternal Flame",
        text         = "* ETERNAL FLAME *",
        defaultColor = { r = 1.000, g = 0.816, b = 0.0 },  -- #FFD000
        hasTimer     = false,
        yOffset      = EF_Y,
        defaultSound = "Ping",
    },
}

-- =============================================================
-- Defaults
-- =============================================================
local DEFAULTS = {
    beaconEnabled        = true,
    beaconSoundEnabled   = true,
    beaconSoundName      = "Bell",
    efEnabled            = false,
    efSoundEnabled       = true,
    efSoundName          = "Ping",
    alertColors          = {
        beacon = { r = 0.957, g = 0.549, b = 0.729 },
        ef     = { r = 1.000, g = 0.816, b = 0.0 },
    },
}

-- =============================================================
-- Beacon Count Scan
-- =============================================================
local function CountActiveBeacons()
    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(BEACON_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 GetExpectedBeaconCount()
    for _, unit in ipairs(NHA:GetGroupUnits()) do
        if UnitExists(unit) and C_UnitAuras and C_UnitAuras.GetAuraDataBySpellID then
            local aura = C_UnitAuras.GetAuraDataBySpellID(unit, BEACON_OF_FAITH_ID, "HELPFUL")
            if aura and aura.auraInstanceID ~= 0
               and aura.sourceUnit == "player" then
                return 2
            end
        end
    end
    return 1
end

local function EternalFlameMissingOnBeaconTarget()
    for _, unit in ipairs(NHA:GetGroupUnits()) do
        if UnitExists(unit) and C_UnitAuras and C_UnitAuras.GetAuraDataBySpellID then
            local hasBeacon = false
            for spellId in pairs(BEACON_IDS) do
                local aura = C_UnitAuras.GetAuraDataBySpellID(unit, spellId, "HELPFUL")
                if aura and aura.auraInstanceID ~= 0
                   and aura.sourceUnit == "player" then
                    hasBeacon = true
                    break
                end
            end
            if hasBeacon then
                local ef = C_UnitAuras.GetAuraDataBySpellID(unit, ETERNAL_FLAME_ID, "HELPFUL")
                if not ef or ef.auraInstanceID == 0
                   or ef.sourceUnit ~= "player" then
                    return true
                end
            end
        end
    end
    return false
end

-- =============================================================
-- Module Table
-- =============================================================
local PaladinHoly = {
    class     = "PALADIN",
    specIndex = 1,
    specID    = 65,
    specName  = "Holy",
    alerts    = ALERTS,
    defaults  = DEFAULTS,
    state     = state,

    extraEvents = {
        "UNIT_AURA",
    },
}

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

-- =============================================================
-- ClearAllState
-- =============================================================
function PaladinHoly:ClearAllState()
    state.beaconActive    = false
    state.beaconWasActive = false
    state.efActive        = false
    state.efWasActive     = false
    self:ClearAllGlows()
end

-- =============================================================
-- Evaluate
-- =============================================================
function PaladinHoly:Evaluate(db)
    local beaconMissing = false
    if db.beaconEnabled and UnitAffectingCombat("player") then
        local count    = CountActiveBeacons()
        local expected = GetExpectedBeaconCount()
        beaconMissing = count < expected
    end
    state.beaconActive = beaconMissing

    local efMissing = false
    if db.efEnabled and UnitAffectingCombat("player") then
        efMissing = EternalFlameMissingOnBeaconTarget()
    end
    state.efActive = efMissing

    if beaconMissing and not state.beaconWasActive then NHA:PlayAlertSound("beacon") end
    if efMissing     and not state.efWasActive      then NHA:PlayAlertSound("ef")     end
    state.beaconWasActive = beaconMissing
    state.efWasActive     = efMissing
end

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

-- =============================================================
-- OnEvent
-- =============================================================
function PaladinHoly: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

-- =============================================================
-- Register
-- =============================================================
NHA:RegisterSpec("PALADIN_HOLY", PaladinHoly)
