-- ============================================================================
-- Housing Decor Guide - Store (Simplified)
-- Direct state management with persistence
-- ============================================================================

local addonName, privateDB = ...
local HDG = _G.HousingDecorGuide

-- Deep copy helper (only used for array operations)
local function DeepCopy(orig)
    local copy
    if type(orig) == "table" then
        copy = {}
        for k, v in pairs(orig) do
            copy[k] = DeepCopy(v)
        end
    else
        copy = orig
    end
    return copy
end

-- Default state template
local DEFAULT_STATE = {
    config = {
        debug = false,
        showMinimapButton = true,
        theme = "HDGDefault",
        scale = 1.0,
        fontScale = 0,
        fontFamily = "FRIZQT__",
        bgOpacity = 0.95,
        quotesEnabled = true,
        vamooseChattiness = 30,
        vamooseChatOutput = false,
        lumberQuotesEnabled = true,
        craftQuotesEnabled = true,
        quotesOnlyChat = false,
        closeProfWindowAfterCraft = true,
        showProfessionButtons = true,
        preferredPriceAddon = nil,
        showRawMaterials = false,
        materialSortMode = "totals",
        craftableFilter = false,
        searchReagents = false,
        uncollectedFilter = false,
        lumberFocusMode = true,
        lumberAutoShow = true,
        showLumberMaterials = false,
        lumberHUDEnabled = true,
        lumberHUDPosition = nil,
        lumberHUDVisible = false,
        lumberRadarPinToTracker = false,
        zoneScannerEnabled = false,
        zoneScannerPopup = false,
        zoneScannerSquirrels = false,
        zoneScannerChat = true,
        zoneScannerSound = false,
    },
    ui = {
        framePosition = nil,
        activeTab = "recipes",
        professionFilter = "All",
        characterProfessionFilter = {},
        collapsedMaterials = {},
        trainersCollapsed = {},
        lumberPinned = false,
        pinnedLumberPosition = nil,
        vendorListPosition = nil,
    },
    shoppingList = {},
    characters = {},
    craftingHistory = {},
    trackedReagents = {},
    minimap = {
        hide = false,
        minimapPos = 220,
        lock = false,
        showMinimapButton = true,
        showInCompartment = true,
        icon = nil,
    },
    lumberCoordinates = {},
    farmingHistory = {},
    vendorShoppingList = {},
    styleShoppingList = {},
    customStyles = {},
    customStyleOrder = {},
    layout = {},
    userNotes = {},
    vendorNotes = {},
    dbVersion = 4,
    _runtime = { pendingCraftInfo = { itemID = nil, remaining = 0 } },
}

HDG.Store = {
    state = DeepCopy(DEFAULT_STATE),
    saveTimer = nil,
    onThemeChange = nil,  -- Theme change callback
}

function HDG.Store:GetState()
    return self.state
end

-- ============================================================================
-- PERSISTENCE (Simplified auto-serialize)
-- ============================================================================

-- Valid tab keys for activeTab validation
local VALID_TABS = { recipes=1, acquisition=1, preview=1, goblin=1, trainers=1, characters=1, styles=1, data=1, config=1 }

-- Type-safe merge: only overwrite when saved value matches default's type
local function MergeTable(target, source)
    if not source or type(source) ~= "table" then return end
    for k, v in pairs(source) do
        local targetVal = target[k]
        if type(v) == "table" and type(targetVal) == "table" then
            MergeTable(targetVal, v)
        elseif targetVal == nil or type(v) == type(targetVal) then
            target[k] = v
        end -- type mismatch on non-nil default: silently keep default
    end
end

-- Clamp numeric config values to safe ranges
local function ValidateConfig(config)
    if type(config.scale) == "number" then
        config.scale = math.max(0.5, math.min(2.0, config.scale))
    end
    if type(config.fontScale) == "number" then
        config.fontScale = math.max(-5, math.min(5, config.fontScale))
    end
    if type(config.bgOpacity) == "number" then
        config.bgOpacity = math.max(0.3, math.min(1.0, config.bgOpacity))
    end
    if type(config.vamooseChattiness) == "number" then
        config.vamooseChattiness = math.max(0, math.min(99, config.vamooseChattiness))
    end
end

-- Validate UI state values
local function ValidateUI(ui)
    if ui.activeTab and not VALID_TABS[ui.activeTab] then
        ui.activeTab = "recipes"
    end
end

-- Validate minimap values
local function ValidateMinimap(minimap)
    if type(minimap.minimapPos) == "number" then
        minimap.minimapPos = minimap.minimapPos % 360
    end
end

-- Sanitize an array table: keep only entries passing filterFn
local function SanitizeArray(saved, filterFn)
    if type(saved) ~= "table" then return nil end
    local clean = {}
    for _, entry in ipairs(saved) do
        if filterFn(entry) then
            table.insert(clean, entry)
        end
    end
    return clean
end

function HDG.Store:LoadFromSavedVariables()
    HDG_DB = HDG_DB or {}

    -- Merge config/ui/minimap with type safety
    MergeTable(self.state.config, HDG_DB.config)
    MergeTable(self.state.ui, HDG_DB.ui)
    MergeTable(self.state.minimap, HDG_DB.minimap)

    -- Validate merged values
    ValidateConfig(self.state.config)
    ValidateUI(self.state.ui)
    ValidateMinimap(self.state.minimap)

    -- Array tables: sanitize entries
    local clean
    clean = SanitizeArray(HDG_DB.shoppingList, function(e) return type(e) == "table" and e.id end)
    if clean then self.state.shoppingList = clean end

    clean = SanitizeArray(HDG_DB.vendorShoppingList, function(e) return type(e) == "table" and e.itemID end)
    if clean then self.state.vendorShoppingList = clean end

    clean = SanitizeArray(HDG_DB.styleShoppingList, function(e) return type(e) == "table" and e.itemID and e.styleId end)
    if clean then self.state.styleShoppingList = clean end

    clean = SanitizeArray(HDG_DB.craftingHistory, function(e) return type(e) == "table" and e.itemID end)
    if clean then self.state.craftingHistory = clean end

    clean = SanitizeArray(HDG_DB.farmingHistory, function(e) return type(e) == "table" and (e.sessionId or e.timestamp) end)
    if clean then self.state.farmingHistory = clean end

    clean = SanitizeArray(HDG_DB.customStyleOrder, function(e) return type(e) == "string" end)
    if clean then self.state.customStyleOrder = clean end

    -- Dict tables: type-checked assignment
    if type(HDG_DB.characters) == "table" then self.state.characters = HDG_DB.characters end
    if type(HDG_DB.trackedReagents) == "table" then self.state.trackedReagents = HDG_DB.trackedReagents end
    if type(HDG_DB.lumberCoordinates) == "table" then self.state.lumberCoordinates = HDG_DB.lumberCoordinates end
    if type(HDG_DB.customStyles) == "table" then self.state.customStyles = HDG_DB.customStyles end
    if type(HDG_DB.layout) == "table" then self.state.layout = HDG_DB.layout end
    if type(HDG_DB.userNotes) == "table" then self.state.userNotes = HDG_DB.userNotes end
    if type(HDG_DB.vendorNotes) == "table" then self.state.vendorNotes = HDG_DB.vendorNotes end
    if type(HDG_DB.dbVersion) == "number" then self.state.dbVersion = HDG_DB.dbVersion end

    -- Prune bloated SavedVariables (prevent unbounded growth)
    self:PruneSavedVariables()

    if self.state.config.debug then
        print("|cFF14b8a6[HDG Store]|r Loaded state from SavedVariables")
    end
end

function HDG.Store:QueueSave()
    if self.saveTimer then self.saveTimer:Cancel() end
    self.saveTimer = C_Timer.NewTimer(1, function() self:SaveToSavedVariables() end)
end

function HDG.Store:SaveToSavedVariables()
    local prevExport = HDG_DB and HDG_DB.catalogExport -- preserve debug export
    HDG_DB = {
        config = self.state.config,
        ui = self.state.ui,
        minimap = self.state.minimap,
        shoppingList = self.state.shoppingList,
        characters = self.state.characters,
        craftingHistory = self.state.craftingHistory,
        trackedReagents = self.state.trackedReagents,
        lumberCoordinates = self.state.lumberCoordinates,
        farmingHistory = self.state.farmingHistory,
        vendorShoppingList = self.state.vendorShoppingList,
        styleShoppingList = self.state.styleShoppingList,
        customStyles = self.state.customStyles,
        customStyleOrder = self.state.customStyleOrder,
        layout = self.state.layout,
        userNotes = self.state.userNotes,
        vendorNotes = self.state.vendorNotes,
        dbVersion = self.state.dbVersion,
        catalogExport = prevExport,
    }
    if self.state.config.debug then
        print("|cFF14b8a6[HDG Store]|r Saved state to SavedVariables")
    end
end

function HDG.Store:Flush()
    if self.saveTimer then
        self.saveTimer:Cancel()
        self.saveTimer = nil
    end
    self:SaveToSavedVariables()
end

-- ============================================================================
-- SAVEDVARIABLES PRUNING (prevent unbounded growth)
-- ============================================================================

function HDG.Store:PruneSavedVariables()
    local CONFIG = privateDB.CONFIG
    if not CONFIG then return end

    -- Prune userNotes (oldest by timestamp)
    if self.state.userNotes then
        local count = 0
        for _ in pairs(self.state.userNotes) do count = count + 1 end
        if count > CONFIG.MAX_USER_NOTES then
            local sorted = {}
            for itemID, note in pairs(self.state.userNotes) do
                table.insert(sorted, { itemID = itemID, ts = note.ts or 0 })
            end
            table.sort(sorted, function(a, b) return a.ts < b.ts end)
            local toRemove = count - CONFIG.MAX_USER_NOTES
            for i = 1, toRemove do
                self.state.userNotes[sorted[i].itemID] = nil
            end
        end
    end

    -- Prune vendorNotes (oldest by timestamp)
    if self.state.vendorNotes then
        local count = 0
        for _ in pairs(self.state.vendorNotes) do count = count + 1 end
        if count > CONFIG.MAX_VENDOR_NOTES then
            local sorted = {}
            for npcID, note in pairs(self.state.vendorNotes) do
                table.insert(sorted, { npcID = npcID, ts = note.ts or 0 })
            end
            table.sort(sorted, function(a, b) return a.ts < b.ts end)
            local toRemove = count - CONFIG.MAX_VENDOR_NOTES
            for i = 1, toRemove do
                self.state.vendorNotes[sorted[i].npcID] = nil
            end
        end
    end

    -- Prune farmingHistory (oldest sessions)
    if self.state.farmingHistory and #self.state.farmingHistory > CONFIG.MAX_FARMING_HISTORY then
        local newHistory = {}
        for i = 1, CONFIG.MAX_FARMING_HISTORY do
            newHistory[i] = self.state.farmingHistory[i]
        end
        self.state.farmingHistory = newHistory
    end

    -- Prune characters (least recently seen)
    if self.state.characters then
        local count = 0
        for _ in pairs(self.state.characters) do count = count + 1 end
        if count > CONFIG.MAX_CHARACTERS then
            local sorted = {}
            for charKey, data in pairs(self.state.characters) do
                table.insert(sorted, { charKey = charKey, lastUpdated = data.lastUpdated or 0 })
            end
            table.sort(sorted, function(a, b) return a.lastUpdated < b.lastUpdated end)
            local toRemove = count - CONFIG.MAX_CHARACTERS
            for i = 1, toRemove do
                self.state.characters[sorted[i].charKey] = nil
            end
        end
    end
end

-- ============================================================================
-- DIRECT STATE METHODS (replaces reducers for simple operations)
-- ============================================================================

function HDG.Store:SetConfig(key, value)
    self.state.config[key] = value
    self:QueueSave()
end

function HDG.Store:SetTheme(themeName)
    self.state.config.theme = themeName
    if self.onThemeChange then self.onThemeChange(themeName) end
    self:QueueSave()
end

function HDG.Store:SetUI(key, value)
    self.state.ui[key] = value
    self:QueueSave()
end

function HDG.Store:SetMinimap(key, value)
    self.state.minimap[key] = value
    self:QueueSave()
end

function HDG.Store:UpdateLayout(frameName, layoutData)
    self.state.layout[frameName] = layoutData
    self:QueueSave()
end

-- ============================================================================
-- SHOPPING LIST METHODS
-- ============================================================================

function HDG.Store:AddToShoppingList(item)
    if not item or not item.id then
        if HDG.Config and HDG.Config.Get("debug") then
            print("|cFFdc322f[HDG Store]|r Rejected non-craftable item from queue:", item and item.name or "nil")
        end
        return
    end
    for i, existing in ipairs(self.state.shoppingList) do
        if existing.id == item.id then
            self.state.shoppingList[i].quantity = (existing.quantity or 1) + (item.quantity or 1)
            self:QueueSave()
            return
        end
    end
    table.insert(self.state.shoppingList, {
        id = item.id,
        name = item.name,
        spell = item.spell,
        reagents = item.reagents,
        expansion = item.expansion,
        quantity = item.quantity or 1,
    })
    self:QueueSave()
end

function HDG.Store:RemoveFromShoppingList(itemID)
    for i, item in ipairs(self.state.shoppingList) do
        if item.id == itemID then
            table.remove(self.state.shoppingList, i)
            self:QueueSave()
            return
        end
    end
end

function HDG.Store:UpdateShoppingItemQty(itemID, quantity)
    for i, item in ipairs(self.state.shoppingList) do
        if item.id == itemID then
            if quantity <= 0 then
                table.remove(self.state.shoppingList, i)
            else
                self.state.shoppingList[i].quantity = quantity
            end
            self:QueueSave()
            return
        end
    end
end

function HDG.Store:ClearShoppingList()
    self.state.shoppingList = {}
    self:QueueSave()
end

-- ============================================================================
-- CHARACTER DATA METHODS
-- ============================================================================

function HDG.Store:UpdateCharacterData(charKey, data)
    if not self.state.characters[charKey] then
        self.state.characters[charKey] = { professions = {}, lastUpdated = 0 }
    end
    local char = self.state.characters[charKey]

    if data.professions then
        -- Replace entire professions table so dropped professions are removed
        char.professions = {}
        for profName, profData in pairs(data.professions) do
            char.professions[profName] = profData
        end
    end

    if data.knowsFindLumber ~= nil then char.knowsFindLumber = data.knowsFindLumber end
    if data.lumberInventory then char.lumberInventory = data.lumberInventory end
    if data.lumberUpdated then char.lumberUpdated = data.lumberUpdated end
    char.lastUpdated = data.lastUpdated or time()

    self:QueueSave()
end

function HDG.Store:RemoveCharacter(charKey)
    self.state.characters[charKey] = nil
    self:QueueSave()
end

-- ============================================================================
-- CRAFTING HISTORY METHODS
-- ============================================================================

function HDG.Store:AddCraftingHistory(entry)
    table.insert(self.state.craftingHistory, 1, {
        itemID = entry.itemID,
        itemName = entry.itemName,
        quantity = entry.quantity or 1,
        timestamp = entry.timestamp or time(),
        charName = entry.charName,
    })
    while #self.state.craftingHistory > 100 do
        table.remove(self.state.craftingHistory)
    end
    self:QueueSave()
end

function HDG.Store:ClearCraftingHistory()
    self.state.craftingHistory = {}
    self:QueueSave()
end

-- ============================================================================
-- TRACKED REAGENTS METHODS
-- ============================================================================

function HDG.Store:AddTrackedReagent(itemID, name, targetQty)
    local CONFIG = privateDB.CONFIG
    if CONFIG then
        local count = 0
        for _ in pairs(self.state.trackedReagents) do count = count + 1 end
        if count >= CONFIG.MAX_TRACKED_REAGENTS then
            print("|cFFFF0000[HDG]|r Reagent tracking limit reached (max " .. CONFIG.MAX_TRACKED_REAGENTS .. "). Remove some items first.")
            return false
        end
    end
    self.state.trackedReagents[itemID] = {
        name = name,
        targetQty = targetQty or 0,
        addedTime = time(),
    }
    self:QueueSave()
    return true
end

function HDG.Store:RemoveTrackedReagent(itemID)
    self.state.trackedReagents[itemID] = nil
    self:QueueSave()
end

function HDG.Store:UpdateTrackedReagent(itemID, targetQty)
    if self.state.trackedReagents[itemID] then
        self.state.trackedReagents[itemID].targetQty = targetQty
        self:QueueSave()
    end
end

function HDG.Store:ClearTrackedReagents()
    self.state.trackedReagents = {}
    self:QueueSave()
end

-- ============================================================================
-- USER NOTES METHODS
-- ============================================================================

function HDG.Store:SetUserNote(itemID, text)
    self.state.userNotes[itemID] = { text = text, ts = time() }
    self:QueueSave()
end

function HDG.Store:GetUserNote(itemID)
    return self.state.userNotes[itemID]
end

function HDG.Store:RemoveUserNote(itemID)
    self.state.userNotes[itemID] = nil
    self:QueueSave()
end

function HDG.Store:GetAllUserNotes()
    return self.state.userNotes
end

-- ============================================================================
-- VENDOR NOTES METHODS
-- ============================================================================

function HDG.Store:SetVendorNote(npcID, text)
    self.state.vendorNotes[npcID] = { text = text, ts = time() }
    self:QueueSave()
end

function HDG.Store:GetVendorNote(npcID)
    return self.state.vendorNotes[npcID]
end

function HDG.Store:RemoveVendorNote(npcID)
    self.state.vendorNotes[npcID] = nil
    self:QueueSave()
end

function HDG.Store:GetAllVendorNotes()
    return self.state.vendorNotes
end

-- ============================================================================
-- EXPORT ALL NOTES
-- ============================================================================

function HDG.Store:ExportUserNotes()
    local itemNotes = self.state.userNotes
    local vendorNotes = self.state.vendorNotes
    local itemCount, vendorCount = 0, 0
    for _ in pairs(itemNotes) do itemCount = itemCount + 1 end
    for _ in pairs(vendorNotes) do vendorCount = vendorCount + 1 end
    if itemCount == 0 and vendorCount == 0 then return "No notes to export." end

    local lines = {}
    table.insert(lines, "=== HDG Notes Export ===")
    table.insert(lines, "Date: " .. date("%Y-%m-%d"))
    table.insert(lines, string.format("Total: %d item notes, %d vendor notes", itemCount, vendorCount))
    table.insert(lines, "")

    if vendorCount > 0 then
        table.insert(lines, "--- Vendor Notes ---")
        table.insert(lines, "")
        local sorted = {}
        for npcID, note in pairs(vendorNotes) do
            table.insert(sorted, { npcID = npcID, text = note.text, ts = note.ts })
        end
        table.sort(sorted, function(a, b) return a.ts > b.ts end)
        for _, entry in ipairs(sorted) do
            local ve = HDG_VendorDB and HDG_VendorDB[entry.npcID]
            local name = ve and ve[1] or ("NPC #" .. entry.npcID)
            local zone = ve and ve[2] or "Unknown Zone"
            table.insert(lines, string.format("[NPC %d] %s — %s", entry.npcID, name, zone))
            table.insert(lines, "  Note: " .. entry.text)
            if entry.ts and entry.ts > 0 then
                table.insert(lines, "  Added: " .. date("%Y-%m-%d %H:%M", entry.ts))
            end
            table.insert(lines, "")
        end
    end

    if itemCount > 0 then
        table.insert(lines, "--- Item Notes ---")
        table.insert(lines, "")
        local sorted = {}
        for itemID, note in pairs(itemNotes) do
            table.insert(sorted, { itemID = itemID, text = note.text, ts = note.ts })
        end
        table.sort(sorted, function(a, b) return a.ts > b.ts end)
        for _, entry in ipairs(sorted) do
            local name = HDG_Data and HDG_Data.GetCachedItemName(entry.itemID) or ("Unknown Item")
            table.insert(lines, string.format("[%d] %s", entry.itemID, name))
            table.insert(lines, "  Note: " .. entry.text)
            if entry.ts and entry.ts > 0 then
                table.insert(lines, "  Added: " .. date("%Y-%m-%d %H:%M", entry.ts))
            end
            table.insert(lines, "")
        end
    end

    return table.concat(lines, "\n")
end

-- ============================================================================
-- LUMBER COORDINATES METHODS
-- ============================================================================

function HDG.Store:AddLumberCoordinate(node)
    table.insert(self.state.lumberCoordinates, node)
    self:QueueSave()
end

function HDG.Store:SetLumberCoordinates(coordinates)
    self.state.lumberCoordinates = coordinates or {}
    self:QueueSave()
end

function HDG.Store:ClearLumberCoordinates()
    self.state.lumberCoordinates = {}
    self:QueueSave()
end

-- ============================================================================
-- FARMING HISTORY METHODS
-- ============================================================================

function HDG.Store:UpsertFarmingSession(session)
    local sessionId = session.sessionId
    if sessionId then
        for _, entry in ipairs(self.state.farmingHistory) do
            if entry.sessionId == sessionId then
                entry.collected = session.collected or entry.collected
                entry.duration = session.duration or entry.duration
                entry.perHour = session.perHour or entry.perHour
                entry.timestamp = session.timestamp or entry.timestamp
                entry.zone = session.zone or entry.zone
                self:QueueSave()
                return
            end
        end
    end
    table.insert(self.state.farmingHistory, 1, session)
    while #self.state.farmingHistory > 100 do
        table.remove(self.state.farmingHistory)
    end
    self:QueueSave()
end

function HDG.Store:ClearFarmingHistory()
    self.state.farmingHistory = {}
    self:QueueSave()
end

function HDG.Store:DeleteMostRecentFarmingSession()
    if #self.state.farmingHistory > 0 then
        table.remove(self.state.farmingHistory, 1)  -- Most recent is at index 1
        self:QueueSave()
        return true
    end
    return false
end

-- ============================================================================
-- VENDOR SHOPPING LIST METHODS
-- ============================================================================

function HDG.Store:AddToVendorShoppingList(itemID, npcID)
    if not itemID then return false end
    -- Find existing entry and bump qty
    for _, entry in ipairs(self.state.vendorShoppingList) do
        local match = npcID and (entry.itemID == itemID and entry.npcID == npcID)
                           or (entry.itemID == itemID and not entry.npcID)
        if match then
            entry.qty = (entry.qty or 1) + 1
            self:QueueSave()
            return true
        end
    end
    table.insert(self.state.vendorShoppingList, { itemID = itemID, npcID = npcID, qty = 1, addedAt = time() })
    self:QueueSave()
    return true
end

function HDG.Store:RemoveFromVendorShoppingList(itemID, npcID)
    for i = #self.state.vendorShoppingList, 1, -1 do
        local entry = self.state.vendorShoppingList[i]
        local match = npcID and (entry.itemID == itemID and entry.npcID == npcID)
                           or (entry.itemID == itemID and not entry.npcID)
        if match then
            local newQty = (entry.qty or 1) - 1
            if newQty <= 0 then
                table.remove(self.state.vendorShoppingList, i)
            else
                entry.qty = newQty
            end
            self:QueueSave()
            return true
        end
    end
    return false
end

function HDG.Store:ClearVendorShoppingList()
    wipe(self.state.vendorShoppingList)
    self:QueueSave()
end

function HDG.Store:IsOnVendorShoppingList(itemID, npcID)
    for _, entry in ipairs(self.state.vendorShoppingList) do
        local match = npcID and (entry.itemID == itemID and entry.npcID == npcID)
                           or (entry.itemID == itemID and not entry.npcID)
        if match then return true end
    end
    return false
end

function HDG.Store:PruneCollectedFromVendorShoppingList()
    if not HDG_Data or not HDG_Data.IsItemCollected then return end
    local changed = false
    for i = #self.state.vendorShoppingList, 1, -1 do
        if HDG_Data.IsItemCollected(self.state.vendorShoppingList[i].itemID) then
            table.remove(self.state.vendorShoppingList, i)
            changed = true
        end
    end
    if changed then self:QueueSave() end
end

function HDG.Store:GetVendorShoppingListCount()
    local total = 0
    for _, entry in ipairs(self.state.vendorShoppingList) do
        total = total + (entry.qty or 1)
    end
    return total
end

-- ============================================================================
-- STYLE SHOPPING LIST METHODS
-- ============================================================================

function HDG.Store:AddToStyleShoppingList(itemID, styleId)
    if not itemID or not styleId then return false end
    -- Find existing entry for this item+style and bump quantity
    for _, entry in ipairs(self.state.styleShoppingList) do
        if entry.itemID == itemID and entry.styleId == styleId then
            entry.qty = (entry.qty or 1) + 1
            self:QueueSave()
            return true
        end
    end
    local sourceType = 0
    if HDG_AllDecorDB and HDG_AllDecorDB[itemID] then
        sourceType = HDG_AllDecorDB[itemID][2] or 0
    end
    table.insert(self.state.styleShoppingList, {
        itemID = itemID, styleId = styleId, sourceType = sourceType, addedAt = time(), qty = 1,
    })
    self:QueueSave()
    return true
end

function HDG.Store:RemoveFromStyleShoppingList(itemID, styleId)
    for i = #self.state.styleShoppingList, 1, -1 do
        local entry = self.state.styleShoppingList[i]
        if entry.itemID == itemID and entry.styleId == styleId then
            local qty = (entry.qty or 1) - 1
            if qty <= 0 then
                table.remove(self.state.styleShoppingList, i)
            else
                entry.qty = qty
            end
            self:QueueSave()
            return true
        end
    end
    return false
end

function HDG.Store:ClearStyleShoppingListForStyle(styleId)
    local changed = false
    for i = #self.state.styleShoppingList, 1, -1 do
        if self.state.styleShoppingList[i].styleId == styleId then
            table.remove(self.state.styleShoppingList, i)
            changed = true
        end
    end
    if changed then self:QueueSave() end
end

function HDG.Store:ClearStyleShoppingList()
    wipe(self.state.styleShoppingList)
    self:QueueSave()
end

function HDG.Store:IsOnStyleShoppingList(itemID, styleId)
    for _, entry in ipairs(self.state.styleShoppingList) do
        if entry.itemID == itemID and entry.styleId == styleId then return true end
    end
    return false
end

function HDG.Store:PruneCollectedFromStyleShoppingList()
    if not HDG_Data or not HDG_Data.IsItemCollected then return end
    local changed = false
    for i = #self.state.styleShoppingList, 1, -1 do
        if HDG_Data.IsItemCollected(self.state.styleShoppingList[i].itemID) then
            table.remove(self.state.styleShoppingList, i)
            changed = true
        end
    end
    if changed then self:QueueSave() end
end

function HDG.Store:GetStyleShoppingListCount()
    return #self.state.styleShoppingList
end

-- ============================================================================
-- CUSTOM STYLES METHODS
-- ============================================================================

function HDG.Store:SaveCustomStyle(key, def)
    if not key then
        key = "custom-" .. time() .. "-" .. math.random(1000, 9999)
    end
    self.state.customStyles[key] = def
    -- Add to order if not already present
    local found = false
    for _, k in ipairs(self.state.customStyleOrder) do
        if k == key then found = true; break end
    end
    if not found then
        table.insert(self.state.customStyleOrder, key)
    end
    self:QueueSave()
    return key
end

function HDG.Store:DeleteCustomStyle(key)
    self.state.customStyles[key] = nil
    for i = #self.state.customStyleOrder, 1, -1 do
        if self.state.customStyleOrder[i] == key then
            table.remove(self.state.customStyleOrder, i)
        end
    end
    self:QueueSave()
end

function HDG.Store:GetCustomStyles()
    return self.state.customStyles, self.state.customStyleOrder
end

-- ============================================================================
-- LEGACY DISPATCH SUPPORT (for backward compatibility during transition)
-- ============================================================================

HDG.Store.reducers = {}

function HDG.Store:RegisterReducer(action, reducerFn)
    self.reducers[action] = reducerFn
end

function HDG.Store:Dispatch(action, payload)
    if self.state.config.debug then
        print("|cFF14b8a6[HDG Store]|r Dispatching:", action)
    end

    -- Map dispatch calls to new methods
    if action == "SET_CONFIG" and payload.key then
        self:SetConfig(payload.key, payload.value)
    elseif action == "SET_THEME" then
        self:SetTheme(payload.theme)
    elseif action == "SET_FRAME_POSITION" then
        self:SetUI("framePosition", payload.position)
    elseif action == "SET_ACTIVE_TAB" then
        self:SetUI("activeTab", payload.tab)
    elseif action == "SET_PROFESSION_FILTER" then
        self:SetUI("professionFilter", payload.filter)
    elseif action == "SET_CHARACTER_PROFESSION_FILTER" then
        if payload.charKey then
            self.state.ui.characterProfessionFilter = self.state.ui.characterProfessionFilter or {}
            self.state.ui.characterProfessionFilter[payload.charKey] = payload.filter
        end
        self:SetUI("professionFilter", payload.filter)
    elseif action == "SET_COLLAPSED_MATERIAL" then
        self.state.ui.collapsedMaterials[payload.category] = payload.collapsed
        self:QueueSave()
    elseif action == "SET_TRAINER_COLLAPSED" then
        self.state.ui.trainersCollapsed[payload.profession] = payload.collapsed
        self:QueueSave()
    elseif action == "SET_LUMBER_PINNED" then
        self:SetUI("lumberPinned", payload.pinned)
    elseif action == "SET_LUMBER_POSITION" then
        self:SetUI("pinnedLumberPosition", payload.position)
    elseif action == "SET_MINIMAP" and payload.key then
        self:SetMinimap(payload.key, payload.value)
    elseif action == "UPDATE_LAYOUT" then
        self:UpdateLayout(payload.frame, {
            width = payload.width, height = payload.height,
            point = payload.point, relPoint = payload.relPoint,
            x = payload.x, y = payload.y,
        })
    elseif action == "ADD_TO_SHOPPING_LIST" then
        self:AddToShoppingList(payload.item)
        if HDG_LumberTracker and HDG_LumberTracker.InvalidateLumberNeeds then HDG_LumberTracker.InvalidateLumberNeeds() end
    elseif action == "REMOVE_FROM_SHOPPING_LIST" then
        self:RemoveFromShoppingList(payload.itemID)
        if HDG_LumberTracker and HDG_LumberTracker.InvalidateLumberNeeds then HDG_LumberTracker.InvalidateLumberNeeds() end
    elseif action == "UPDATE_SHOPPING_ITEM_QTY" then
        self:UpdateShoppingItemQty(payload.itemID, payload.quantity)
        if HDG_LumberTracker and HDG_LumberTracker.InvalidateLumberNeeds then HDG_LumberTracker.InvalidateLumberNeeds() end
    elseif action == "CLEAR_SHOPPING_LIST" then
        self:ClearShoppingList()
        if HDG_LumberTracker and HDG_LumberTracker.InvalidateLumberNeeds then HDG_LumberTracker.InvalidateLumberNeeds() end
    elseif action == "UPDATE_CHARACTER_DATA" then
        self:UpdateCharacterData(payload.charKey, payload)
    elseif action == "REMOVE_CHARACTER" then
        self:RemoveCharacter(payload.charKey)
    elseif action == "ADD_CRAFTING_HISTORY" then
        self:AddCraftingHistory(payload)
    elseif action == "CLEAR_CRAFTING_HISTORY" then
        self:ClearCraftingHistory()
    elseif action == "ADD_TRACKED_REAGENT" then
        self:AddTrackedReagent(payload.itemID, payload.name, payload.targetQty)
    elseif action == "REMOVE_TRACKED_REAGENT" then
        self:RemoveTrackedReagent(payload.itemID)
    elseif action == "UPDATE_TRACKED_REAGENT" then
        self:UpdateTrackedReagent(payload.itemID, payload.targetQty)
    elseif action == "CLEAR_TRACKED_REAGENTS" then
        self:ClearTrackedReagents()
    elseif action == "ADD_LUMBER_COORDINATE" then
        self:AddLumberCoordinate(payload.node)
    elseif action == "SET_LUMBER_COORDINATES" then
        self:SetLumberCoordinates(payload.coordinates)
    elseif action == "CLEAR_LUMBER_COORDINATES" then
        self:ClearLumberCoordinates()
    elseif action == "UPSERT_FARMING_SESSION" then
        self:UpsertFarmingSession(payload.session)
    elseif action == "CLEAR_FARMING_HISTORY" then
        self:ClearFarmingHistory()
    elseif action == "DELETE_MOST_RECENT_FARMING_SESSION" then
        self:DeleteMostRecentFarmingSession()
    elseif action == "ADD_TO_VENDOR_SHOPPING_LIST" then
        self:AddToVendorShoppingList(payload.itemID, payload.npcID)
    elseif action == "REMOVE_FROM_VENDOR_SHOPPING_LIST" then
        self:RemoveFromVendorShoppingList(payload.itemID, payload.npcID)
    elseif action == "CLEAR_VENDOR_SHOPPING_LIST" then
        self:ClearVendorShoppingList()
    else
        -- Fallback to legacy reducer if exists
        local reducer = self.reducers[action]
        if reducer then
            local newState = reducer(self.state, payload)
            if newState then self.state = newState end
            self:QueueSave()
        elseif self.state.config.debug then
            print("|cFFdc322f[HDG Store]|r Unknown action:", action)
        end
    end

    -- Trigger EventBus for listeners (backward compat)
    if HDG.EventBus then
        HDG.EventBus:Trigger("HDG_STATE_CHANGED", { action = action, state = self.state })
    end
end

-- Expose DeepCopy for other modules
HDG.Store.DeepCopy = DeepCopy
