-- ============================================================================
-- pom/pvp/pvp_healer_mark.lua
-- Arena healer marker (12.0 secret-safe)
--
-- GREEN square over the RED arena numbers, on ENEMY PLAYER nameplates
-- that are HEALER spec. Arena only.
--
-- DB keys:
--   healerMarkEnabled (bool)
--   healerMarkSize (number)
--   healerMarkX (number)
--   healerMarkY (number)    -- additional Y above the arena numbers
--   arenaNumbersX (number)
--   arenaNumbersY (number)
--
-- Secret-safe:
--  - NO "if UnitIsUnit(...) then"
--  - Uses SetAlphaFromBoolean(UnitIsUnit(...), 1, 0)
-- ============================================================================

local ADDON_NAME, ns = ...

ns = _G[ADDON_NAME] or ns
_G[ADDON_NAME] = ns

ns.pvp_healer_mark = ns.pvp_healer_mark or {}
local M = ns.pvp_healer_mark

local pcall = pcall

local function GetDB()
if ns.pvp_db and ns.pvp_db.Get then
    return ns.pvp_db:Get()
    end
    return nil
    end

    local function Ensure(hb)
    if not hb or hb:IsForbidden() then return end

        if not hb.pomPvpHealerSquares then
            hb.pomPvpHealerSquares = {}

            for i = 1, 5 do
                local holder = CreateFrame("Frame", nil, hb)
                holder:SetAllPoints(hb)
                holder:SetAlpha(0)

                local sq = holder:CreateTexture(nil, "OVERLAY", nil, 7) -- max 7
                sq:SetTexture("Interface\\Buttons\\WHITE8x8")
                sq:SetVertexColor(0.0, 1.0, 0.0, 1.0)
                sq:SetSize(12, 12)

                holder._sq = sq
                hb.pomPvpHealerSquares[i] = holder
                end
                end
                end

                local function HideAll(hb)
                if not hb or not hb.pomPvpHealerSquares then return end
                    for i=1,5 do
                        local h = hb.pomPvpHealerSquares[i]
                        if h then h:SetAlpha(0) end
                            end
                            end

                            local function ApplyLayout(hb, db)
                            if not hb or not hb.pomPvpHealerSquares or not db then return end

                                local size = tonumber(db.healerMarkSize or 12) or 12
                                local hx   = tonumber(db.healerMarkX or 0) or 0
                                local hy   = tonumber(db.healerMarkY or 16) or 16

                                local ax = tonumber(db.arenaNumbersX or 0) or 0
                                local ay = tonumber(db.arenaNumbersY or 8) or 8

                                -- Over the red numbers: same base anchor + extra healerMarkY
                                for i=1,5 do
                                    local h = hb.pomPvpHealerSquares[i]
                                    if h and h._sq then
                                        h._sq:ClearAllPoints()
                                        h._sq:SetPoint("BOTTOM", hb, "TOP", ax + hx, ay + hy)
                                        h._sq:SetSize(size, size)
                                        end
                                        end
                                        end

                                        function M:Apply(frame, hb, inst, isEnemyPlayer)
                                        if not frame or not hb or hb:IsForbidden() then return end

                                            local db = GetDB()
                                            if not db or not db.healerMarkEnabled then
                                                HideAll(hb)
                                                return
                                                end

                                                if inst ~= "arena" or not isEnemyPlayer then
                                                    HideAll(hb)
                                                    return
                                                    end

                                                    Ensure(hb)
                                                    ApplyLayout(hb, db)

                                                    for i=1,5 do
                                                        local h = hb.pomPvpHealerSquares[i]
                                                        if h then
                                                            local specID = GetArenaOpponentSpec(i)
                                                            if specID and specID > 0 then
                                                                local _, _, _, _, role = GetSpecializationInfoByID(specID)
                                                                if role == "HEALER" then
                                                                    if h.SetAlphaFromBoolean then
                                                                        pcall(h.SetAlphaFromBoolean, h, UnitIsUnit(frame.unit, "arena"..i), 1, 0)
                                                                        else
                                                                            h:SetAlpha(0)
                                                                            end
                                                                            else
                                                                                h:SetAlpha(0)
                                                                                end
                                                                                else
                                                                                    h:SetAlpha(0)
                                                                                    end
                                                                                    end
                                                                                    end
                                                                                    end
