--[[
ETB Wishlist — Star items/sets, track farming progress
SavedVariable: ETB_Wishlist = { items = {[itemID]=true}, sets = {[setID]=true} }
]]--
local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.Wishlist = {}
local WL = ETB.Wishlist

function WL:Initialize()
    ETB_Wishlist = ETB_Wishlist or { items = {}, sets = {} }
    self.data = ETB_Wishlist
end

function WL:AddItem(itemID)
    if not self.data then self:Initialize() end
    self.data.items[itemID] = true
    ETB.EventBus:Fire("ETB_WISHLIST_CHANGED", "add", itemID)
end

function WL:RemoveItem(itemID)
    if not self.data then return end
    self.data.items[itemID] = nil
    ETB.EventBus:Fire("ETB_WISHLIST_CHANGED", "remove", itemID)
end

function WL:ToggleItem(itemID)
    if self:HasItem(itemID) then self:RemoveItem(itemID); return false
    else self:AddItem(itemID); return true end
end

function WL:HasItem(itemID)
    return self.data and self.data.items and self.data.items[itemID] == true
end

function WL:AddSet(setID)
    if not self.data then self:Initialize() end
    self.data.sets[setID] = true
    ETB.EventBus:Fire("ETB_WISHLIST_CHANGED", "addset", setID)
end

function WL:RemoveSet(setID)
    if not self.data then return end
    self.data.sets[setID] = nil
end

function WL:HasSet(setID)
    return self.data and self.data.sets and self.data.sets[setID] == true
end

function WL:GetItemCount()
    local count = 0
    if self.data and self.data.items then
        for _ in pairs(self.data.items) do count = count + 1 end
    end
    return count
end

function WL:GetAllItems()
    if not self.data or not self.data.items then return {} end
    local list = {}
    for itemID in pairs(self.data.items) do list[#list + 1] = itemID end
    return list
end

-- Group uncollected wishlist items by instance for farming
function WL:GetFarmingGuide()
    if not self.data or not self.data.items then return {}, {} end
    
    local byInstance = {}  -- instanceName → { isRaid, bosses = { bossName → {itemIDs} } }
    local noSource = {}
    
    for itemID in pairs(self.data.items) do
        -- Skip already collected
        local ok, collected = pcall(C_TransmogCollection.PlayerHasTransmog, itemID)
        if ok and collected then
            -- skip
        else
            local sources = ETB.SourceLookup and ETB.SourceLookup:GetItemSources(itemID)
            if sources and #sources > 0 then
                local s = sources[1]
                local instName = s.instanceName or "Unknown"
                local bossName = s.bossName or "Unknown"
                if not byInstance[instName] then
                    byInstance[instName] = { isRaid = s.isRaid, expansion = s.expansion, bosses = {} }
                end
                if not byInstance[instName].bosses[bossName] then
                    byInstance[instName].bosses[bossName] = {}
                end
                local bossItems = byInstance[instName].bosses[bossName]
                bossItems[#bossItems + 1] = itemID
            else
                noSource[#noSource + 1] = itemID
            end
        end
    end
    
    return byInstance, noSource
end
