--[[
============================================================================
HiddenAppearances.lua  (added in v14.6.0, Tier 2 Feature 1.3)
============================================================================

Per-appearance hide flags. The negative complement to Favorites: lets users
mark appearances they never want to see in the browser, so the list stays
focused on what they actually like.

Storage: ETB_HiddenAppearances saved variable. Keyed by itemID. Mirror of
the FavoritesManager pattern so users can reason about both the same way.

By default, hidden appearances are filtered out. The user can toggle a
"Show hidden" button in the FilterBar to reveal them (e.g. to unhide one).

Why a separate module:
  - A flag-store + persistence is its own concern. Favorites and Hidden
    are sibling concepts but stay separate so neither file gets bloated.
  - Future per-appearance metadata (color tags, notes, custom labels)
    can live alongside if we add them — or can spawn their own module.
============================================================================
]]--
local ADDON_NAME, NS = ...
local ETB = NS.ETB
NS.ETB.HiddenAppearances = NS.ETB.HiddenAppearances or {}
local HA = NS.ETB.HiddenAppearances

HA.hidden = HA.hidden or {}

-- =============================================================================
-- INIT / PERSIST
-- =============================================================================

function HA:Initialize()
    ETB_HiddenAppearances = ETB_HiddenAppearances or { items = {} }
    self.hidden = ETB_HiddenAppearances.items or {}
    if ETB.Debug then
        ETB:Debug("HiddenAppearances", "Loaded " .. self:GetCount() .. " hidden")
    end
end

function HA:Save()
    ETB_HiddenAppearances = ETB_HiddenAppearances or {}
    ETB_HiddenAppearances.items = self.hidden
end

-- =============================================================================
-- CRUD
-- =============================================================================

function HA:Hide(itemID)
    if not itemID then return false end
    self.hidden[itemID] = { itemID = itemID, hiddenAt = time() }
    self:Save()
    if ETB.EventBus then ETB.EventBus:Fire("ETB_APPEARANCE_HIDDEN", itemID) end
    return true
end

function HA:Unhide(itemID)
    if not itemID or not self.hidden[itemID] then return false end
    self.hidden[itemID] = nil
    self:Save()
    if ETB.EventBus then ETB.EventBus:Fire("ETB_APPEARANCE_UNHIDDEN", itemID) end
    return true
end

function HA:Toggle(itemID)
    if self:IsHidden(itemID) then
        self:Unhide(itemID)
        if ETB.Print then ETB:Print("Appearance unhidden") end
        return false
    else
        self:Hide(itemID)
        if ETB.Print then ETB:Print("Appearance hidden") end
        return true
    end
end

function HA:IsHidden(itemID)
    return itemID and self.hidden[itemID] ~= nil or false
end

function HA:GetCount()
    local n = 0
    for _ in pairs(self.hidden) do n = n + 1 end
    return n
end

function HA:ClearAll()
    local count = self:GetCount()
    self.hidden = {}
    self:Save()
    if ETB.Print then ETB:Print("Cleared " .. count .. " hidden appearances") end
    return count
end
