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

export const syncHistoryMigrations: readonly Migration[] = [
  {
    id: '2026-04-23_sync_history_initial',
    sql: `
      CREATE TABLE IF NOT EXISTS macsync.sync_history (
        id                UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        device_id         UUID NOT NULL REFERENCES macsync.devices(id) ON DELETE CASCADE,
        module            TEXT NOT NULL,
        started_at        TIMESTAMPTZ NOT NULL,
        completed_at      TIMESTAMPTZ,
        status            TEXT NOT NULL CHECK (status IN ('pending', 'success', 'error')),
        messages_synced   INTEGER NOT NULL DEFAULT 0,
        contacts_synced   INTEGER NOT NULL DEFAULT 0,
        conversations_synced INTEGER NOT NULL DEFAULT 0,
        error_message     TEXT,
        created_at        TIMESTAMPTZ NOT NULL DEFAULT now(),
        UNIQUE(device_id, module, started_at)
      );
      CREATE INDEX IF NOT EXISTS idx_sync_history_device ON macsync.sync_history(device_id);
      CREATE INDEX IF NOT EXISTS idx_sync_history_started ON macsync.sync_history(started_at DESC);
      CREATE INDEX IF NOT EXISTS idx_sync_history_status ON macsync.sync_history(status);
    `,
  },
];
