RockyCoreWoWDeckHelper = {}
RockyCoreWoWDeckHelper_State = {}  -- This is the SavedVariables table

-- Number of action bar slots to track (12 slots x 10 bars = 120 max)
-- Retail has bars 1-8 active; we track bars 1-6 (slots 1-72 + overrides)
RockyCoreWoWDeckHelper.NUM_BARS = 8
RockyCoreWoWDeckHelper.SLOTS_PER_BAR = 12

function RockyCoreWoWDeckHelper:Initialize()
    RockyCoreWoWDeckHelper_State = RockyCoreWoWDeckHelper_State or {}
    RockyCoreWoWDeckHelper_State.version = 2
    RockyCoreWoWDeckHelper_State.timestamp = 0
    RockyCoreWoWDeckHelper_State.slots = {}
    RockyCoreWoWDeckHelper_State.meta = {}
    self:RefreshAll()
end

function RockyCoreWoWDeckHelper:RefreshAll()
    RockyCoreWoWDeckHelper_State.timestamp = time()
    RockyCoreWoWDeckHelper_State.meta = self:GetMeta()
    RockyCoreWoWDeckHelper_State.slots = {}

    local found = 0
    for _, barDef in ipairs(self.BARS) do
        for slot = 1, self.SLOTS_PER_BAR do
            local actionSlot = barDef.first + slot - 1
            local ok, result = pcall(function()
                return RockyCoreWoWDeckHelper.ActionBar:GetSlotData(actionSlot)
            end)
            if not ok then
                print("RockyCoreWoWDeckHelper ERROR slot " .. actionSlot .. ": " .. tostring(result))
            elseif result and not result.empty then
                RockyCoreWoWDeckHelper_State.slots[actionSlot] = result
                found = found + 1
            end
        end
    end
    if self.debug then
        print("RockyCore WoWDeck: captured " .. found .. " slots")
    end
end

function RockyCoreWoWDeckHelper:GetMeta()
    local specIndex = GetSpecialization()
    local specID, specName = nil, nil
    if specIndex then
        specID, specName = GetSpecializationInfo(specIndex)
    end

    return {
        playerClass = UnitClassBase("player"),
        specID = specID,
        specName = specName,
        inVehicle = UnitInVehicle("player"),
        hasPossessBar = IsPossessBarVisible(),
        shapeshift = GetShapeshiftForm(),
        overrideBarActive = HasOverrideActionBar() and 1 or 0,
        vehicleBarActive = HasVehicleActionBar() and 1 or 0,
    }
end
