ALTER TABLE bookings ADD COLUMN public_booking_id TEXT;

-- Give existing bookings a stable public reference without exposing the full
-- internal UUID. Existing UUIDs are random, so the first 20 hexadecimal
-- characters still provide a high-entropy reference for migrated rows.
UPDATE bookings
SET public_booking_id =
  'SLT-' || upper(substr(replace(id, '-', ''), 1, 4)) || '-' ||
  upper(substr(replace(id, '-', ''), 5, 4)) || '-' ||
  upper(substr(replace(id, '-', ''), 9, 4)) || '-' ||
  upper(substr(replace(id, '-', ''), 13, 4)) || '-' ||
  upper(substr(replace(id, '-', ''), 17, 4))
WHERE public_booking_id IS NULL;

CREATE UNIQUE INDEX IF NOT EXISTS idx_bookings_public_booking_id
  ON bookings(public_booking_id)
  WHERE public_booking_id IS NOT NULL;
