import Dexie from 'dexie'

// NOTE: do NOT use 'tables' as a store name — Dexie 4.x has a built-in
// `.tables` getter on the prototype that returns all registered stores as an
// array, so an own-property store named 'tables' gets shadowed by it.
// We use pos_ prefix on store names to avoid all such collisions.

const db = new Dexie('pos_snapshot')

db.version(1).stores({
  tables:           'id, group_id, is_active',
  orders:           'id, table_id, status',
  offline_payments: '++localId, uuid, synced',
})

db.version(2).stores({
  tables:           'id, group_id, is_active',
  orders:           'id, table_id, status',
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
})

db.version(3).stores({
  tables:           'id, group_id, is_active',
  orders:           'id, table_id, status',
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
})

db.version(4).stores({
  tables:           'id, group_id, is_active',
  orders:           'id, table_id, status',
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
  table_groups:     'id',
  flag_defs:        'id',
  flag_assignments: '++localId, table_id, flag_id',
})

// Version 5: rename 'tables' → 'pos_tables' and 'orders' → 'pos_orders'
db.version(5).stores({
  tables:           null,
  orders:           null,
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
  table_groups:     'id',
  flag_defs:        'id',
  flag_assignments: '++localId, table_id, flag_id',
  pos_tables:       'id, group_id, is_active',
  pos_orders:       'id, table_id, status',
}).upgrade(tx => {
  return tx.table('tables').toArray().then(rows => {
    if (rows.length > 0) return tx.table('pos_tables').bulkPut(rows)
  }).then(() =>
    tx.table('orders').toArray().then(rows => {
      if (rows.length > 0) return tx.table('pos_orders').bulkPut(rows)
    })
  ).catch(() => {})
})

// Version 6: add offline_ops queue for splits and payments taken offline
db.version(6).stores({
  tables:           null,
  orders:           null,
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
  table_groups:     'id',
  flag_defs:        'id',
  flag_assignments: '++localId, table_id, flag_id',
  pos_tables:       'id, group_id, is_active',
  pos_orders:       'id, table_id, status',
  offline_ops:      '++localId, orderId, synced, seq',
})

// Version 7: add kv store for persisting WebSocket cursor (last seen seq_id)
db.version(7).stores({
  tables:           null,
  orders:           null,
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
  table_groups:     'id',
  flag_defs:        'id',
  flag_assignments: '++localId, table_id, flag_id',
  pos_tables:       'id, group_id, is_active',
  pos_orders:       'id, table_id, status',
  offline_ops:      '++localId, orderId, synced, seq',
  kv:               'key',
})

// Version 8: add uuid index to offline_ops (needed for legacy-drain dedup check)
db.version(8).stores({
  tables:           null,
  orders:           null,
  offline_payments: '++localId, uuid, synced',
  products:         'id, category_id, is_available',
  categories:       'id, parent_id',
  offline_orders:   '++localId, uuid, synced, table_id',
  table_groups:     'id',
  flag_defs:        'id',
  flag_assignments: '++localId, table_id, flag_id',
  pos_tables:       'id, group_id, is_active',
  pos_orders:       'id, table_id, status',
  offline_ops:      '++localId, uuid, orderId, synced, seq',
  kv:               'key',
})

export default db
