--[[
============================================================================
Modules/BossLootTooltip/Events.lua  (added in v15.1.0 — Audit Batch 4 Part 1)
============================================================================

Mouseover-event handler and suppression rules for the BossLootTooltip
orchestrator. Owner-pattern: each method takes `owner` as first argument.

Methods owned here (2):
  ShouldSuppress     — combat / PvP / dead-or-ghost suppression rules
  OnMouseoverUnit    — main hook, fires per UPDATE_MOUSEOVER_UNIT event.
                       Drives the lazy-init of the index, GUID-to-NPC resolution,
                       primary (compact-data) and secondary (JLoot) tooltip
                       population.

External dependencies pulled from owner:
  owner.enabled, owner.indexBuilt, owner.lastNPCID
  owner.npcIndex, owner._jlootByEnc
  owner.FLAG_ENCOUNTER (used to flag JLoot-sourced items in fallback path)
  owner:BuildIndex (delegator -> Index:BuildIndex)
  owner:GetNPCIDFromGUID (delegator -> Index:GetNPCIDFromGUID)
  owner:ShouldSuppress (delegator -> Events:ShouldSuppress, recursive but safe)
  owner:AddLootToTooltip (delegator -> Render:AddLootToTooltip)

Closure / shadowing notes: No closures, no async.
============================================================================
]]--
local ADDON_NAME, NS = ...
local ETB = NS.ETB
NS.ETB.BossLootTooltip = NS.ETB.BossLootTooltip or {}
NS.ETB.BossLootTooltip.Events = NS.ETB.BossLootTooltip.Events or {}
local Events = NS.ETB.BossLootTooltip.Events

-- =============================================================================
-- METHODS (extracted from Modules/BossLootTooltip.lua in v15.1.0)
-- =============================================================================

function Events:ShouldSuppress(owner)
    if UnitAffectingCombat("player") then return true end
    -- C_PvP.IsBattleground/IsArena were removed on 5.5.4; fall back to the
    -- instance type, which is reliable on this client.
    if C_PvP and C_PvP.IsBattleground and C_PvP.IsBattleground() then return true end
    if C_PvP and C_PvP.IsArena and C_PvP.IsArena() then return true end
    local _, instanceType = IsInInstance()
    if instanceType == "pvp" or instanceType == "arena" then return true end
    if IsActiveBattlefieldArena and IsActiveBattlefieldArena() then return true end
    if UnitIsDeadOrGhost("player") then return true end
    return false
end

function Events:OnMouseoverUnit(owner)
    if not owner.enabled then return end
    if ETB.IsLowMemoryMode and ETB:IsLowMemoryMode() then return end
    if owner:ShouldSuppress() then return end
    
    -- No synchronous index build on hover. Smooth mode prefers skipping boss
    -- loot lines until the optional background build is ready.
    if not owner.indexBuilt then
        if not owner._lazyBuildQueued then
            owner._lazyBuildQueued = true
            local function buildLater()
                if not owner.indexBuilt and not (InCombatLockdown and InCombatLockdown()) then
                    owner:BuildIndexChunked()
                end
            end
            if ETB.RunWhenStartupIdle then
                ETB:RunWhenStartupIdle("bossLoot.mouseoverDeferredBuild", buildLater, { delay = 2, interval = 3, maxWait = 300 })
            elseif ETB.QueueWork then
                ETB:QueueWork("bossLoot.mouseoverDeferredBuild", buildLater, { pauseInCombat = true, startupIdleOptions = { ignoreQuietWindow = true } })
            end
        end
        return
    end
    
    local guid = UnitGUID("mouseover")
    if not guid then return end
    
    local npcID = owner:GetNPCIDFromGUID(guid)
    if not npcID then return end
    
    -- Skip if same NPC (tooltip already populated)
    if npcID == owner.lastNPCID then return end
    owner.lastNPCID = npcID
    
    -- Primary: compact data index
    if owner.npcIndex[npcID] then
        owner:AddLootToTooltip(GameTooltip, npcID)
    -- Secondary: JLoot database (JNpcToEnc → encounterID → pre-built reverse index)
    elseif ETB.JNpcToEnc and ETB.JEncounters and owner._jlootByEnc then
        local encID = ETB.JNpcToEnc[npcID]
        if encID then
            local itemIDs = owner._jlootByEnc[encID]
            if itemIDs then
                local jlootItems = {}
                for itemID in pairs(itemIDs) do
                    jlootItems[itemID] = owner.FLAG_ENCOUNTER
                end
                if next(jlootItems) then
                    owner.npcIndex[npcID] = jlootItems
                    owner:AddLootToTooltip(GameTooltip, npcID)
                end
            end
        end
    end

    -- ETB: vendor / trainer / quest-giver transmog sources (ATT cross-check)
    if ETB.NPCSourceTooltip then
        ETB.NPCSourceTooltip:Append(GameTooltip, npcID)
    end
end
