-- =============================================================
-- Specs/PriestHoly.lua
-- Nem: Healer Alerts — Holy Priest spec module
--
-- FULLY SELF-CONTAINED. Zero dependencies on other spec modules.
--
-- Alerts:
--   1. Prayer of Mending missing (41635) — text alert
--
-- Glows:
--   • Prayer of Mending active target glow (41635)
--
-- Whitelisted spell IDs (non-secret in Midnight):
--   139   - Renew
--   41635 - Prayer of Mending
--   77489 - Echo of Light
-- =============================================================

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 PRAYER_OF_MENDING_ID = 41635
local RENEW_ID             = 139
local ECHO_OF_LIGHT_ID     = 77489

-- =============================================================
-- State
-- =============================================================
local state = {
    -- Prayer of Mending missing
    pomActive      = false,
    pomWasActive   = false,
    -- Debounce
    playerAuraPending   = false,
    rosterUpdatePending = false,
}

-- Glow frame tables
local pomGlowFrames = {}  -- buffFrame → true

-- =============================================================
-- Alert Definitions
-- =============================================================
local POM_Y = -30

local ALERTS = {
    {
        key          = "pom",
        label        = "Prayer of Mending",
        text         = "* PRAYER OF MENDING *",
        defaultColor = { r = 1.000, g = 1.000, b = 1.000 },  -- Priest class color
        hasTimer     = false,
        yOffset      = POM_Y,
        defaultSound = "Bell",
    },
}

-- =============================================================
-- Defaults
-- =============================================================
local DEFAULTS = {
    pomEnabled           = true,
    pomSoundEnabled      = true,
    pomSoundName         = "Bell",
    pomGlowEnabled       = true,
    alertColors          = {
        pom = { r = 1.000, g = 1.000, b = 1.000 },
    },
}

-- =============================================================
-- Prayer of Mending Scan
-- PoM bounces between targets. Alert when no player-cast PoM
-- exists on any group member.
-- =============================================================
local function HasActivePrayerOfMending()
    for _, unit in ipairs(NHA:GetGroupUnits()) do
        if UnitExists(unit) and C_UnitAuras and C_UnitAuras.GetAuraDataBySpellID then
            local aura = C_UnitAuras.GetAuraDataBySpellID(unit, PRAYER_OF_MENDING_ID, "HELPFUL")
            if aura and aura.auraInstanceID ~= 0
               and aura.sourceUnit == "player" then
                return true
            end
        end
    end
    return false
end

-- =============================================================
-- Module Table
-- =============================================================
local PriestHoly = {
    class     = "PRIEST",
    specIndex = 2,  -- Holy is spec index 2
    specID    = 257, -- fallback: Holy Priest spec ID for secret-value contexts
    specName  = "Holy",
    alerts    = ALERTS,
    defaults  = DEFAULTS,
    state     = state,

    extraEvents = {
        "UNIT_AURA",
    },
}

-- =============================================================
-- ClearAllGlows
-- =============================================================
function PriestHoly:ClearAllGlows()
    for buffFrame in pairs(pomGlowFrames) do
        NHA:HideProcGlow(buffFrame)
    end
    wipe(pomGlowFrames)
end

-- =============================================================
-- ClearAllState
-- =============================================================
function PriestHoly:ClearAllState()
    state.pomActive    = false
    state.pomWasActive = false
    self:ClearAllGlows()
end

-- =============================================================
-- NeedsTicker
-- =============================================================
function PriestHoly:NeedsTicker(db)
    return db.pomGlowEnabled
end

-- =============================================================
-- RefreshGlows
-- =============================================================
function PriestHoly:RefreshGlows(db)
    -- PoM glow is static while buff exists on a target
end

-- =============================================================
-- EvaluateFrameGlow
-- =============================================================
function PriestHoly:EvaluateFrameGlow(db, buffFrame, aura)
    if not db or not db.glowEnabled or not db.enabled then
        if pomGlowFrames[buffFrame] then
            NHA:HideProcGlow(buffFrame)
            pomGlowFrames[buffFrame] = nil
        end
        return
    end

    local tracked = pomGlowFrames[buffFrame]

    if not aura then
        if tracked then
            NHA:HideProcGlow(buffFrame)
            pomGlowFrames[buffFrame] = nil
        end
        return
    end

    if issecretvalue(aura.spellId) then
        if tracked then
            NHA:HideProcGlow(buffFrame)
            pomGlowFrames[buffFrame] = nil
        end
        return
    end

    if aura.sourceUnit ~= "player" then
        if tracked then
            NHA:HideProcGlow(buffFrame)
            pomGlowFrames[buffFrame] = nil
        end
        return
    end

    -- Prayer of Mending glow on current holder
    if aura.spellId == PRAYER_OF_MENDING_ID and db.pomGlowEnabled then
        pomGlowFrames[buffFrame] = true
        local c = db.alertColors and db.alertColors.pom
                  or { r=1, g=1, b=1 }
        NHA:ShowProcGlow(buffFrame, c.r, c.g, c.b)
        return
    end

    -- Not tracked
    if tracked then
        NHA:HideProcGlow(buffFrame)
        pomGlowFrames[buffFrame] = nil
    end
end

-- =============================================================
-- Evaluate
-- =============================================================
function PriestHoly:Evaluate(db)
    -- Prayer of Mending missing (combat only)
    local pomMissing = false
    if db.pomEnabled and UnitAffectingCombat("player") then
        pomMissing = not HasActivePrayerOfMending()
    end
    state.pomActive = pomMissing

    -- Rising-edge sound
    if pomMissing and not state.pomWasActive then NHA:PlayAlertSound("pom") end
    state.pomWasActive = pomMissing
end

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

-- =============================================================
-- OnEvent
-- =============================================================
function PriestHoly: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 PriestHoly:BuildOptions(parent, db, helpers)
    helpers.MakeCheckbox("PoM Bounce Glow", 230, -90,
        function() return db.pomGlowEnabled end,
        function(v) db.pomGlowEnabled = v and true or false end)
end

-- =============================================================
-- Register
-- =============================================================
NHA:RegisterSpec("PRIEST_HOLY", PriestHoly)
