/**
 * card_submissions + scan_attempts for server-side scan pipeline.
 *
 * Retimestamped from 1748365200000 → 1779853647566 so fresh DBs run
 * initial-schema before scan tables (FK to users). Existing envs that
 * already recorded the old name in pgmigrations are unaffected; IF NOT EXISTS
 * guards make re-application safe if the new name is pending.
 *
 * @type {import('node-pg-migrate').ColumnDefinitions | undefined}
 */
export const shorthands = undefined;

/**
 * @param {import('node-pg-migrate').MigrationBuilder} pgm
 */
export const up = (pgm) => {
  pgm.sql(`
    CREATE TABLE IF NOT EXISTS card_submissions (
      id SERIAL PRIMARY KEY,
      user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
      ocr_text TEXT,
      ocr_confidence INTEGER,
      scan_image_url TEXT,
      candidate_card_ids JSONB DEFAULT '[]',
      ocr_payload JSONB,
      status VARCHAR(32) NOT NULL DEFAULT 'pending',
      reviewed_by INTEGER REFERENCES users(id) ON DELETE SET NULL,
      promoted_card_id INTEGER REFERENCES cards(id) ON DELETE SET NULL,
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    CREATE TABLE IF NOT EXISTS scan_attempts (
      id SERIAL PRIMARY KEY,
      user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
      ocr_text TEXT,
      ocr_confidence INTEGER,
      layer INTEGER NOT NULL DEFAULT 2,
      matched_card_id INTEGER REFERENCES cards(id) ON DELETE SET NULL,
      result_kind VARCHAR(32) NOT NULL,
      latency_ms INTEGER,
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    CREATE INDEX IF NOT EXISTS idx_card_submissions_status
      ON card_submissions (status, created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_scan_attempts_user_created
      ON scan_attempts (user_id, created_at DESC);
  `);
};

/**
 * @param {import('node-pg-migrate').MigrationBuilder} pgm
 */
export const down = (pgm) => {
  throw new Error('Down migration not supported for add-scan-tables');
};
