import type { Migration } from '@/shared/db';

export const sendQueueMigrations: readonly Migration[] = [
  {
    id: '2026-04-22_send_queue_initial',
    sql: `
      CREATE TABLE IF NOT EXISTS macsync.send_queue (
        id                UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        device_id         UUID NOT NULL REFERENCES macsync.devices(id) ON DELETE CASCADE,
        to_handle         TEXT NOT NULL,
        body              TEXT NOT NULL,
        status            TEXT NOT NULL DEFAULT 'queued',
        send_queue_id     TEXT,
        sent_at           TIMESTAMPTZ,
        failure_reason    TEXT,
        created_at        TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at        TIMESTAMPTZ NOT NULL DEFAULT now()
      );
      CREATE INDEX IF NOT EXISTS idx_sendq_device_status ON macsync.send_queue(device_id, status);
      CREATE INDEX IF NOT EXISTS idx_sendq_created ON macsync.send_queue(created_at DESC);
      CREATE INDEX IF NOT EXISTS idx_sendq_status ON macsync.send_queue(status);
    `,
  },
];
