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

// Mirrors waiter_pwa/src/store/tableColourStore.js — same localStorage key so both apps share state.

export const DEFAULT_COLOURS = {
  light: {
    free: {
      cardBg:    '#d6d6d6',
      badgeBg:   '#e3e3e3',
      nameText:  '#3b485e',
      badgeText: '#adadad',
    },
    mine: {
      cardBg:    '#e83030',
      badgeBg:   'rgba(255,255,255,0.40)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    open: {
      cardBg:    '#ffbb29',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    partially_paid: {
      cardBg:    '#e89230',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    paid: {
      cardBg:    '#79ad38',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
  },
  dark: {
    free: {
      cardBg:    '#243044',
      badgeBg:   'rgba(26,35,50,0.50)',
      nameText:  '#ffffff',
      badgeText: '#adadad',
    },
    mine: {
      cardBg:    '#e83030',
      badgeBg:   'rgba(255,255,255,0.40)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    open: {
      cardBg:    '#ffbb29',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    partially_paid: {
      cardBg:    '#e89230',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
    paid: {
      cardBg:    '#79ad38',
      badgeBg:   'rgba(255,255,255,0.25)',
      nameText:  '#ffffff',
      badgeText: '#ffffff',
    },
  },
}

const useTableColourStore = create(persist(
  (set) => ({
    colours: DEFAULT_COLOURS,
    setColour: (mode, status, slot, value) =>
      set(s => ({
        colours: {
          ...s.colours,
          [mode]: {
            ...s.colours[mode],
            [status]: {
              ...s.colours[mode][status],
              [slot]: value,
            },
          },
        },
      })),
    resetAll: () => set({ colours: DEFAULT_COLOURS }),
  }),
  { name: 'pos-table-colours' }
))

export default useTableColourStore
