-- =========================================================
-- pom_raidmarkers.lua
-- Owns RaidTargetIcon positioning
-- Works even when CompactUnitFrame_UpdateRaidTargetIcon doesn't exist (12.0)
-- =========================================================

local ADDON_NAME, ns = ...
ns = _G[ADDON_NAME] or ns
_G[ADDON_NAME] = ns

ns.raidmarkers = ns.raidmarkers or {}
local RM = ns.raidmarkers

local U = ns.util

local function AnchorIcon(frame)
    if not frame or frame:IsForbidden() then return end
    if not U.IsNameplateUnit(frame.unit) then return end

    local container = frame.HealthBarsContainer
    local rtf = frame.RaidTargetFrame
    if not (container and rtf) then return end

    local icon = rtf.RaidTargetIcon
    if not icon then return end

    icon:ClearAllPoints()
    icon:SetPoint("BOTTOM", container, "TOP", 0, 6) -- <-- JUSTER HER
    icon:SetSize(20, 20)
    icon:SetDrawLayer("OVERLAY", 7)
    icon:Show()
end

function RM:Apply(frame)
    if not frame or frame:IsForbidden() then return end
    if not U.IsNameplateUnit(frame.unit) then return end

    -- Apply now (best effort)
    AnchorIcon(frame)

    -- Hook the RaidTargetFrame once per unitframe (guaranteed to exist per FrameStack)
    local rtf = frame.RaidTargetFrame
    if rtf and not rtf.__pomHooked then
        rtf.__pomHooked = true

        rtf:HookScript("OnShow", function()
            -- Win AFTER Blizzard: next frame + tiny delay
            if C_Timer and C_Timer.After then
                C_Timer.After(0, function() AnchorIcon(frame) end)
                C_Timer.After(0.05, function() AnchorIcon(frame) end)
            else
                AnchorIcon(frame)
            end
        end)
    end
end
