--[[
============================================================================
ETB v10.0 - FavoritesManager.lua
============================================================================
Favorites and wishlist management.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.FavoritesManager = {
    favorites = {},
    outfits = {},
}

local FM = ETB.FavoritesManager

-- =============================================================================
-- INITIALIZATION
-- =============================================================================

function FM:Initialize()
    ETB_Favorites = ETB_Favorites or { items = {}, outfits = {} }
    self.favorites = ETB_Favorites.items or {}
    self.outfits = ETB_Favorites.outfits or {}
    
    ETB:Debug("Favorites", "Loaded " .. self:GetCount() .. " favorites")
end

function FM:Save()
    ETB_Favorites = ETB_Favorites or {}
    ETB_Favorites.items = self.favorites
    ETB_Favorites.outfits = self.outfits
end

-- =============================================================================
-- FAVORITES
-- =============================================================================

function FM:AddFavorite(itemID)
    if not itemID then return false end
    
    self.favorites[itemID] = {
        itemID = itemID,
        addedAt = time(),
    }
    
    self:Save()
    ETB.EventBus:Fire("ETB_FAVORITE_ADDED", itemID)
    ETB:Debug("Favorites", "Added favorite: " .. itemID)
    return true
end

function FM:RemoveFavorite(itemID)
    if not itemID or not self.favorites[itemID] then return false end
    
    self.favorites[itemID] = nil
    self:Save()
    ETB.EventBus:Fire("ETB_FAVORITE_REMOVED", itemID)
    ETB:Debug("Favorites", "Removed favorite: " .. itemID)
    return true
end

function FM:ToggleFavorite(itemID)
    if self:IsFavorite(itemID) then
        self:RemoveFavorite(itemID)
        ETB:Print("Removed from favorites")
        return false
    else
        self:AddFavorite(itemID)
        ETB:Print("Added to favorites")
        return true
    end
end

function FM:IsFavorite(itemID)
    return self.favorites[itemID] ~= nil
end

function FM:GetFavorites()
    local list = {}
    for itemID, data in pairs(self.favorites) do
        list[#list + 1] = {
            itemID = itemID,
            addedAt = data.addedAt,
        }
    end
    table.sort(list, function(a, b) return (a.addedAt or 0) > (b.addedAt or 0) end)
    return list
end

function FM:GetCount()
    local count = 0
    for _ in pairs(self.favorites) do count = count + 1 end
    return count
end

function FM:ClearAll()
    wipe(self.favorites)
    self:Save()
    ETB.EventBus:Fire("ETB_FAVORITES_CLEARED")
    ETB:Print("All favorites cleared")
end

-- =============================================================================
-- OUTFITS
-- =============================================================================

function FM:SaveOutfit(name, items)
    if not name or name == "" then return false end
    
    self.outfits[name] = {
        name = name,
        items = items or {},
        createdAt = time(),
    }
    
    self:Save()
    ETB.EventBus:Fire("ETB_OUTFIT_SAVED", name)
    ETB:Print("Outfit saved: " .. name)
    return true
end

function FM:DeleteOutfit(name)
    if not self.outfits[name] then return false end
    
    self.outfits[name] = nil
    self:Save()
    ETB.EventBus:Fire("ETB_OUTFIT_DELETED", name)
    ETB:Print("Outfit deleted: " .. name)
    return true
end

function FM:GetOutfit(name)
    return self.outfits[name]
end

function FM:GetOutfits()
    local list = {}
    for name, data in pairs(self.outfits) do
        list[#list + 1] = data
    end
    table.sort(list, function(a, b) return (a.createdAt or 0) > (b.createdAt or 0) end)
    return list
end

function FM:GetOutfitCount()
    local count = 0
    for _ in pairs(self.outfits) do count = count + 1 end
    return count
end

-- =============================================================================
-- STATS
-- =============================================================================

function FM:PrintStats()
    ETB:Print("=== FAVORITES STATS ===")
    ETB:Print("Favorite items: " .. self:GetCount())
    ETB:Print("Saved outfits: " .. self:GetOutfitCount())
end

-- Initialize on load
ETB.EventBus:Register("ETB_INITIALIZED", function()
    FM:Initialize()
end, ETB.EventBus.Priority.EARLY)

ETB:Debug("FavoritesManager", "FavoritesManager.lua loaded")
