/** Notification messages with idempotent dispatch. */

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
  pgm.sql(`
    CREATE TABLE notifications_messages (
      id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
      event_id text NOT NULL UNIQUE,
      channel text NOT NULL DEFAULT 'email',
      template text NOT NULL,
      recipient text NOT NULL,
      subject text NOT NULL,
      body text NOT NULL,
      status text NOT NULL DEFAULT 'sent',
      created_at timestamptz NOT NULL DEFAULT now(),
      CONSTRAINT notifications_messages_channel_check CHECK (channel = 'email'),
      CONSTRAINT notifications_messages_status_check CHECK (status IN ('sent', 'failed', 'queued'))
    )
  `);
  pgm.sql('CREATE INDEX notifications_messages_template_idx ON notifications_messages (template)');
};

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
  pgm.sql('DROP TABLE IF EXISTS notifications_messages');
};
