/**
 * Bulk import all Pokémon cards from pokemon-tcg-data GitHub JSON.
 *
 * Fetches sets/en.json, then upserts every card in each set (keyed on scryfall_id).
 * Populates legalities from set metadata where available.
 *
 * Usage:
 *   POSTGRES_URL=<url> node scripts/bulk-import-pokemon.js
 *   node --env-file=.env.local scripts/bulk-import-pokemon.js
 *
 * Options (env vars):
 *   BATCH_SIZE  — rows per INSERT batch (default 100)
 *   DRY_RUN     — "true" to fetch and count without writing to DB
 */

import { neon } from '@neondatabase/serverless';

import { runBulkPokemonSync } from '../lib/card-import/pokemon-bulk.js';

if (!process.env.POSTGRES_URL) {
  console.error('POSTGRES_URL is required');
  process.exit(1);
}

const sql = neon(process.env.POSTGRES_URL, { fullResults: false });

const BATCH_SIZE = parseInt(process.env.BATCH_SIZE || '100', 10);
const DRY_RUN = process.env.DRY_RUN === 'true';

async function run() {
  console.log(
    `[bulk-import-pokemon] Starting (batch=${BATCH_SIZE}, dryRun=${DRY_RUN})`
  );

  const summary = await runBulkPokemonSync({
    batchSize: BATCH_SIZE,
    dryRun: DRY_RUN,
    query: (query, values) => sql.query(query, values),
  });

  console.log('[bulk-import-pokemon] Complete!');
  console.log(`[bulk-import-pokemon] Sets processed: ${summary.setsProcessed}/${summary.totalSets}`);
  console.log(`[bulk-import-pokemon] Upserted: ${summary.upserted}`);
  console.log(`[bulk-import-pokemon] Skipped (dry run): ${summary.skipped}`);
  console.log(`[bulk-import-pokemon] Errors: ${summary.errors}`);
}

run().catch((err) => {
  console.error('[bulk-import-pokemon] Fatal error:', err);
  process.exit(1);
});
