-- HousingDecorGuide/UI/Tabs/Styles/HDG_ST_State.lua
-- Module-level state for the Styles tab. Single source of truth for the
-- selectors that drive every Styles screen (Landing, Detail, ItemPanel,
-- ShoppingList). Owns named mutators so screen modules never poke fields
-- directly -- mutate via State:Foo() / read via State.bar.
--
-- Loaded BEFORE every Styles screen module via TOC ordering.
--
-- Future-screen consumers attach via privateDB.HDG_ST.State.

local _, privateDB = ...

privateDB.HDG_ST = privateDB.HDG_ST or {}

local State = {}
privateDB.HDG_ST.State = State

-- =============================================================================
-- FIELDS (defaults; mutated via the named mutators below)
-- =============================================================================

-- Currently inspected style (nil = on landing page).
State.selectedStyle = nil

-- Tier or subcategory key inside the current style.
-- "first" / "second" / "neutral" / "clash" for tier styles; subcat key for
-- themed; nil for snapshot/collection/curated (rendering ignores this).
State.selectedTier = "first"

-- Currently selected item in the detail-view list (drives the side panel).
State.selectedItemID = nil

-- Detail-frame search box text (lowercased).
State.searchText = ""
State.searchDebounceTimer = nil

-- Collection filter -- replaces the legacy uncollectedOnly + collectedOnly
-- boolean pair. Values: "all" / "have" / "need".
State.collectionFilter = "all"

-- Detail-frame quick filters.
State.craftableOnly  = false
State.hideUnreleased = true

-- Tier-style sub-filters.
State.selectedRoom   = nil  -- nil = "All", or room key like "kitchen"
State.selectedSource = nil  -- nil = "All", or source-type int (5 = Vendor)

-- Landing-page state.
State.selectedLandingCategory = nil  -- nil = All; or tier key like "custom"
State.landingSearchText  = ""
State.landingSearchTimer = nil
State.collapsedTiers     = {}

-- Side-trip selectors that swap the detail frame to non-style data.
State.selectedShoppingListId = nil       -- ShoppingList detail
State.selectedSessionData    = nil       -- Recent-session detail
                                          -- shape: { houseGUID, sessionID, displayName }

-- Grid vs flat-list view in the detail frame.
State.viewMode = "gallery"  -- "gallery" or "list"

-- =============================================================================
-- DERIVED / COMPATIBILITY READERS
-- =============================================================================
-- The legacy filter loops use uncollectedOnly / collectedOnly boolean pairs.
-- Surface them as derived getters so existing call sites keep working
-- without change while the source of truth migrates to collectionFilter.

function State:IsCollectedOnly()   return self.collectionFilter == "have" end
function State:IsUncollectedOnly() return self.collectionFilter == "need" end

-- =============================================================================
-- MUTATORS (called by widget event glue / Show* dispatchers)
-- =============================================================================

function State:SelectStyle(id)              self.selectedStyle = id end
function State:SelectTier(key)              self.selectedTier = key end
function State:SelectItem(itemID)           self.selectedItemID = itemID end
function State:SelectRoom(key)              self.selectedRoom = key end
function State:SelectSource(key)            self.selectedSource = key end
function State:SelectLandingCategory(key)   self.selectedLandingCategory = key end

function State:SetSearchText(s)             self.searchText = s or "" end
function State:SetLandingSearchText(s)      self.landingSearchText = s or "" end
function State:SetCollectionFilter(filter)
    if filter == "all" or filter == "have" or filter == "need" then
        self.collectionFilter = filter
    end
end
function State:SetCraftableOnly(b)          self.craftableOnly  = not not b end
function State:SetHideUnreleased(b)         self.hideUnreleased = not not b end
function State:SetViewMode(m)               self.viewMode = m end

function State:ToggleCollapsedTier(key)
    self.collapsedTiers[key] = not self.collapsedTiers[key]
end

function State:SelectShoppingList(listId)   self.selectedShoppingListId = listId end
function State:SelectSession(data)          self.selectedSessionData = data end

-- Reset transient detail state when entering a new style. Preserves filter
-- choices (the user just picked them) but clears style-scoped selectors.
function State:ResetForStyle(styleId, def)
    self.selectedStyle = styleId
    self.selectedSessionData = nil
    self.selectedItemID = nil
    if def and def.tier == "snapshot" then
        self.selectedTier = nil
        self.selectedRoom = nil
        self.selectedSource = nil
    elseif def and def.tier == "collection" then
        self.selectedTier = nil
        self.selectedRoom = nil
        self.selectedSource = nil
    elseif def and def.subcategories then
        self.selectedTier = def.subcategories[1].key
        self.selectedSource = nil
    else
        self.selectedTier = "first"
        self.selectedRoom = nil
    end
end

return State
