import { create } from 'zustand'
import { persist } from 'zustand/middleware'

// density: '1x1' | '2x1' | '2x2' | '4x1' | '4x2' | '4x3'
// ownerFilter: 'all' | 'mine'
// statusFilter: 'all' | 'free' | 'open' | 'paid'
// zoneFilter: Set of zone IDs (serialized as array in localStorage)
// activeZoneTab: zone id string or 'all'

const useTableViewStore = create(
  persist(
    (set, get) => ({
      density: '2x2',
      ownerFilter: 'all',
      statusFilter: 'all',
      zoneFilter: [],        // array of zone ids (serialized fine in JSON)
      activeZoneTab: 'all',
      chipStyle: 'icon_short', // 'full' | 'short' | 'icon_full' | 'icon_short' | 'icon'
      tabSwitcherStyle: 'text', // 'text' | 'icon'

      // Footer nav settings
      // navStyle: 'triple' | 'icon_navbar'
      navStyle: 'triple',
      // navItems: array of item keys for icon_navbar mode (1-5 items)
      // Available keys: 'tables' | 'orders' | 'messages'
      navItems: ['tables', 'orders', 'messages'],
      // tripleItems: ordered keys for triple pill mode (exactly 3)
      tripleItems: ['tables', 'orders', 'messages'],

      setDensity: (density) => set({ density }),
      setOwnerFilter: (ownerFilter) => set({ ownerFilter }),
      setStatusFilter: (statusFilter) => set({ statusFilter }),
      setZoneFilter: (zoneFilter) => set({ zoneFilter }),
      setActiveZoneTab: (activeZoneTab) => set({ activeZoneTab }),
      setChipStyle: (chipStyle) => set({ chipStyle }),
      setTabSwitcherStyle: (tabSwitcherStyle) => set({ tabSwitcherStyle }),
      setNavStyle: (navStyle) => set({ navStyle }),
      setNavItems: (navItems) => set({ navItems }),
      setTripleItems: (tripleItems) => set({ tripleItems }),
      // KDS status display: 'badge' (default) | 'icon'
      kdsDisplayStyle: 'badge',
      setKdsDisplayStyle: (kdsDisplayStyle) => set({ kdsDisplayStyle }),

      // Order item layout: 'compact' | 'expanded' | 'fully_detailed'
      orderItemLayout: 'compact',
      setOrderItemLayout: (orderItemLayout) => set({ orderItemLayout }),

      // Merge view persistence: 'off' | 'per_order' | 'global'
      mergePersist: 'off',
      setMergePersist: (mergePersist) => set({ mergePersist }),
      // Stored merge mode for 'global' persistence
      globalMergeMode: 'off',
      setGlobalMergeMode: (globalMergeMode) => set({ globalMergeMode }),

      clearFilters: () => set({
        ownerFilter: 'all',
        statusFilter: 'all',
        zoneFilter: [],
        activeZoneTab: 'all',
      }),
    }),
    {
      name: 'table-view-prefs',
      // future: could sync to backend here
    }
  )
)

export default useTableViewStore
