/**
 * Dry-run all migration adapters against the live DB.
 * Usage: bun run server/local-first/migration/dry-run.ts
 */

import postgres from 'postgres';
import {
  migrateModule,
  notesMigration,
  workMigration,
  calMigration,
  voteMigration,
  booksMigration,
  cartMigration,
  providersMigration,
  filesMigration,
  tripsMigration,
  inboxMigration,
  splatMigration,
  type MigrationResult,
} from './pg-to-automerge';

const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) {
  throw new Error('DATABASE_URL environment variable is required');
}

const sql = postgres(DATABASE_URL, { max: 5, idle_timeout: 10 });

// Wrap postgres.js in a pg-compatible pool.query() interface
const pool = {
  async query(text: string, params?: any[]) {
    const result = params
      ? await sql.unsafe(text, params)
      : await sql.unsafe(text);
    return { rows: Array.from(result) };
  },
};

const space = process.argv[2] || 'demo';

const allMigrations = [
  notesMigration,
  workMigration,
  calMigration,
  voteMigration,
  booksMigration,
  cartMigration,
  providersMigration,
  filesMigration,
  tripsMigration,
  inboxMigration,
  splatMigration,
];

async function main() {
  console.log(`\n=== DRY-RUN MIGRATION (space: "${space}") ===\n`);

  const results: MigrationResult[] = [];

  for (const migration of allMigrations) {
    const result = await migrateModule(migration, pool, space, undefined, {
      dryRun: true,
    });
    results.push(result);
    console.log('');
  }

  console.log('\n=== SUMMARY ===\n');
  console.log(
    `${'Module'.padEnd(12)} ${'Docs'.padStart(5)} ${'Rows'.padStart(6)} ${'Errors'.padStart(7)} ${'Time'.padStart(8)}`
  );
  console.log('-'.repeat(40));

  let totalDocs = 0;
  let totalRows = 0;
  let totalErrors = 0;

  for (const r of results) {
    console.log(
      `${r.module.padEnd(12)} ${String(r.docsCreated).padStart(5)} ${String(r.rowsMigrated).padStart(6)} ${String(r.errors.length).padStart(7)} ${(r.durationMs + 'ms').padStart(8)}`
    );
    totalDocs += r.docsCreated;
    totalRows += r.rowsMigrated;
    totalErrors += r.errors.length;
  }

  console.log('-'.repeat(40));
  console.log(
    `${'TOTAL'.padEnd(12)} ${String(totalDocs).padStart(5)} ${String(totalRows).padStart(6)} ${String(totalErrors).padStart(7)}`
  );

  if (totalErrors > 0) {
    console.log('\n=== ERRORS ===\n');
    for (const r of results) {
      for (const e of r.errors) {
        console.error(`[${r.module}] ${e}`);
      }
    }
  }

  await sql.end();
}

main().catch((e) => {
  console.error('Fatal:', e);
  process.exit(1);
});
