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

export const conversationMigrations: readonly Migration[] = [
  {
    id: '2026-04-18_conversation_initial',
    sql: `
      CREATE TABLE IF NOT EXISTS macsync.conversations (
        id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        device_id       UUID NOT NULL REFERENCES macsync.devices(id) ON DELETE CASCADE,
        external_id     TEXT NOT NULL,
        display_name    TEXT NOT NULL DEFAULT '',
        is_group        BOOLEAN NOT NULL DEFAULT false,
        participants    JSONB NOT NULL DEFAULT '[]'::jsonb,
        last_message_at TIMESTAMPTZ,
        message_count   INTEGER NOT NULL DEFAULT 0,
        synced_at       TIMESTAMPTZ NOT NULL DEFAULT now(),
        created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
        UNIQUE(device_id, external_id)
      );
      CREATE INDEX IF NOT EXISTS idx_conv_device ON macsync.conversations(device_id);
      CREATE INDEX IF NOT EXISTS idx_conv_last_msg ON macsync.conversations(last_message_at DESC);
    `,
  },
];
