import { create } from 'zustand'

export const DEFAULT_COLOURS = {
  light: {
    free: {
      cardBg:    '#dde5ef',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#3d5270',
      badgeText: '#3d5270',
    },
    mine: {
      cardBg:    '#e8610a',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#e8610a',
    },
    open: {
      cardBg:    '#FF8F60',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#FF8F60',
    },
    partially_paid: {
      cardBg:    '#FFDC67',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#d4a800',
    },
    paid: {
      cardBg:    '#81D264',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#81D264',
    },
    // 6th status: items are ready to pick up from kitchen/bar
    kds_ready: {
      cardBg:    '#5dc936',
      cardBg2:   '#FFDC67',
      badgeBg:   'rgba(255,255,255,0.57)',
      badgeBg2:  'rgba(255,255,255,0.57)',
      nameText:  '#ffffff',
      nameText2: '#ffffff',
      badgeText: 'rgba(45,71,36,0.63)',
      badgeText2:'rgba(45,71,36,0.63)',
      flash:     true,
    },
  },
  dark: {
    free: {
      cardBg:    '#243044',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#94b8d4',
      badgeText: '#94b8d4',
    },
    mine: {
      cardBg:    '#e8610a',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#e8610a',
    },
    open: {
      cardBg:    '#FF8F60',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#FF8F60',
    },
    partially_paid: {
      cardBg:    '#FFDC67',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#d4a800',
    },
    paid: {
      cardBg:    '#81D264',
      badgeBg:   'rgba(255,255,255,0.92)',
      nameText:  '#ffffff',
      badgeText: '#81D264',
    },
    kds_ready: {
      cardBg:    '#5dc936',
      cardBg2:   '#FFDC67',
      badgeBg:   'rgba(255,255,255,0.57)',
      badgeBg2:  'rgba(255,255,255,0.57)',
      nameText:  '#ffffff',
      nameText2: '#ffffff',
      badgeText: 'rgba(45,71,36,0.63)',
      badgeText2:'rgba(45,71,36,0.63)',
      flash:     true,
    },
  },
}

const useTableColourStore = create((set) => ({
  colours: DEFAULT_COLOURS,
  loadFromBackend: (raw) => {
    try {
      const parsed = JSON.parse(raw)
      if (parsed?.light && parsed?.dark) {
        const merged = { light: {}, dark: {} }
        for (const mode of ['light', 'dark']) {
          for (const status of Object.keys(DEFAULT_COLOURS[mode])) {
            merged[mode][status] = { ...DEFAULT_COLOURS[mode][status], ...(parsed[mode][status] || {}) }
          }
        }
        set({ colours: merged })
      }
    } catch {}
  },
}))

export default useTableColourStore
