-- [ SPOTLIGHT ENTRY ]--------------------------------------------------------------------------------
local _, Orbit = ...
local L = Orbit.L

-- [ NAMESPACE ]--------------------------------------------------------------------------------------
Orbit.Spotlight = {}
local Spotlight = Orbit.Spotlight
Spotlight.Search = {}
Spotlight.Index = { Sources = {} }
Spotlight.UI = {}
Spotlight._active = false

-- [ KINDS ]------------------------------------------------------------------------------------------
-- Canonical list — adding a source means one row here + matching Index/Sources/<Name>.lua.
Spotlight.Kinds = {
    { kind = "bags",        settingKey = "Bags",        labelKey = "PLU_SPT_SRC_BAGS" },
    { kind = "equipped",    settingKey = "Equipped",    labelKey = "PLU_SPT_SRC_EQUIPPED" },
    { kind = "spellbook",   settingKey = "Spellbook",   labelKey = "PLU_SPT_SRC_SPELLBOOK" },
    { kind = "toys",        settingKey = "Toys",        labelKey = "PLU_SPT_SRC_TOYS" },
    { kind = "mounts",      settingKey = "Mounts",      labelKey = "PLU_SPT_SRC_MOUNTS" },
    { kind = "pets",        settingKey = "Pets",        labelKey = "PLU_SPT_SRC_PETS" },
    { kind = "heirlooms",   settingKey = "Heirlooms",   labelKey = "PLU_SPT_SRC_HEIRLOOMS" },
    { kind = "professions", settingKey = "Professions", labelKey = "PLU_SPT_SRC_PROFESSIONS" },
    { kind = "currencies",  settingKey = "Currencies",  labelKey = "PLU_SPT_SRC_CURRENCIES" },
    { kind = "macros",      settingKey = "Macros",      labelKey = "PLU_SPT_SRC_MACROS" },
    { kind = "questitems",  settingKey = "QuestItems",  labelKey = "PLU_SPT_SRC_QUESTITEMS" },
    -- Help is authored content, not live game data: gated behind its "help"/"orbit" prefix (prefixOnly).
    { kind = "help",        settingKey = "Help",        labelKey = "PLU_SPT_SRC_HELP", aliasTokens = { "orbit" }, prefixOnly = true },
}

-- [ BINDING GLOBALS ]--------------------------------------------------------------------------------
_G.BINDING_HEADER_ORBIT = _G.BINDING_HEADER_ORBIT or "Orbit"
_G.BINDING_NAME_ORBIT_SPOTLIGHT_TOGGLE = L.PLU_SPT_BINDING_NAME

-- [ PUBLIC API ]-------------------------------------------------------------------------------------
function Spotlight:Toggle()
    if InCombatLockdown() then
        Orbit:Print(L.PLU_SPT_MSG_COMBAT)
        return
    end
    if not self._active then return end
    self.UI.SpotlightFrame:Toggle()
end

function Spotlight:Enable()
    if self._active then return end
    self._active = true
    self.Index.IndexManager:RegisterEvents()
end

function Spotlight:Disable()
    if not self._active then return end
    self._active = false
    self.Index.IndexManager:UnregisterEvents()
    if self.UI.SpotlightFrame._frame and self.UI.SpotlightFrame._frame:IsShown() then
        self.UI.SpotlightFrame:Close()
    end
end

-- [ DIAGNOSTIC ]-------------------------------------------------------------------------------------
function Spotlight:Debug()
    local IM = self.Index.IndexManager
    IM:InvalidateAll()
    IM:Rebuild()
    local counts = IM:GetLastCounts()
    Orbit:Print("Spotlight index — master size: " .. #IM:GetMaster())
    for name, count in pairs(counts) do Orbit:Print("  " .. name .. ": " .. count) end
end

function Spotlight:Dump(kind, limit)
    limit = limit or 20
    local IM = self.Index.IndexManager
    IM:InvalidateAll()
    IM:Rebuild()
    Orbit:Print("Spotlight dump (" .. kind .. "):")
    local shown = 0
    for _, entry in ipairs(IM:GetMaster()) do
        if entry.kind == kind then
            shown = shown + 1
            if shown > limit then break end
            Orbit:Print(string.format("  [%s] %s", entry.name, entry.lowerName))
        end
    end
    if shown == 0 then Orbit:Print("  (no entries)") end
end

-- [ AUTO-ENABLE ON LOGIN ]---------------------------------------------------------------------------
local loader = CreateFrame("Frame")
loader:RegisterEvent("PLAYER_LOGIN")
loader:SetScript("OnEvent", function()
    Spotlight:Enable()
    loader:UnregisterAllEvents()
end)

-- [ EVENT BUS ]--------------------------------------------------------------------------------------
-- Lets other modules (e.g. the minimap button) request a toggle without referencing Spotlight directly.
Orbit.EventBus:On("ORBIT_SPOTLIGHT_TOGGLE", function() Spotlight:Toggle() end)
