-- Add token column to tables for self-order QR code identification
ALTER TABLE tables ADD COLUMN IF NOT EXISTS token VARCHAR(100);
CREATE UNIQUE INDEX IF NOT EXISTS idx_tables_token ON tables(token);
CREATE INDEX IF NOT EXISTS idx_tables_token_active ON tables(token) WHERE is_active = true;

-- Backfill existing tables with unique tokens
-- Uses gen_random_uuid() to generate unique tokens for each existing table
UPDATE tables SET token = gen_random_uuid()::text WHERE token IS NULL;

-- Make token NOT NULL after backfill (optional, keep nullable for flexibility)

-- Add source column to orders for tracking order origin
ALTER TABLE orders ADD COLUMN IF NOT EXISTS source VARCHAR(50) DEFAULT 'staff';
