import type { Database } from 'bun:sqlite';

import { tableHasColumn } from './helpers.js';
import type { SchemaMigrationDefinition } from './types.js';

export const REVIEWER_FAILURE_COUNT_MIGRATION: SchemaMigrationDefinition = {
  version: 21,
  name: 'reviewer_failure_count',
  apply(database: Database) {
    if (!tableHasColumn(database, 'paired_tasks', 'reviewer_failure_count')) {
      database.exec(`
        ALTER TABLE paired_tasks
        ADD COLUMN reviewer_failure_count INTEGER NOT NULL DEFAULT 0
      `);
    }

    database.exec(`
      UPDATE paired_tasks
         SET reviewer_failure_count = 0
       WHERE reviewer_failure_count IS NULL
    `);
  },
};
