import fs from 'fs';
import readline from 'readline';
import { prisma } from '../src/config/database.js';

const BATCH_SIZE = 2000;
const CSV_PATH = process.argv[2] || '/tmp/claves_prod_serv.csv';

async function main() {
  if (!fs.existsSync(CSV_PATH)) {
    console.error(`Archivo no encontrado: ${CSV_PATH}`);
    console.error('Uso: npx tsx scripts/import-clave-prod-serv.ts [ruta/al/csv]');
    process.exit(1);
  }

  const existing = await prisma.catClaveProdServ.count();
  console.log(`Registros existentes: ${existing}`);
  if (existing > 0) {
    console.log('El catálogo ya tiene datos. No se importará nada.');
    process.exit(0);
  }

  const fileStream = fs.createReadStream(CSV_PATH, { encoding: 'utf-8' });
  const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });

  let batch: { clave: string; descripcion: string }[] = [];
  let total = 0;

  for await (const line of rl) {
    const idx = line.indexOf(',');
    if (idx === -1) continue;
    const clave = line.slice(0, idx).trim();
    const descripcion = line.slice(idx + 1).trim();
    if (!clave || !descripcion) continue;
    batch.push({ clave, descripcion });

    if (batch.length >= BATCH_SIZE) {
      await prisma.catClaveProdServ.createMany({ data: batch, skipDuplicates: true });
      total += batch.length;
      console.log(`Importados: ${total}`);
      batch = [];
    }
  }

  if (batch.length > 0) {
    await prisma.catClaveProdServ.createMany({ data: batch, skipDuplicates: true });
    total += batch.length;
  }

  console.log(`Importación completada. Total: ${total}`);
  process.exit(0);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
