--[[
============================================================================
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
    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
    if UnitIsDeadOrGhost("player") then return true end
    return false
end

function Events:OnMouseoverUnit(owner)
    if not owner.enabled then return end
    if owner:ShouldSuppress() then return end
    
    -- Lazy init: build index on first NPC hover
    if not owner.indexBuilt then
        owner:BuildIndex()
        if not owner.indexBuilt then return end
    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)
        return
    end
    
    -- Secondary: JLoot database (JNpcToEnc → encounterID → pre-built reverse index)
    if 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
                -- Add all JLoot items — don't filter with GetItemInfoInstant
                -- since heroic items may not be cached. Class filtering
                -- happens in addItem via CanPlayerUseItem (which returns true for unknowns).
                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)
                    return
                end
            end
        end
    end
end

