/**
 * Standalone test for CNAS Layout-B parser.
 *
 * Reads pdftotext -layout output for the 8 known Layout-B PDFs (the 9th is
 * an empty form template), parses with parseProviderTextJudetGrouped(), and
 * prints results for manual inspection.
 *
 * Usage:
 *   npx tsx scripts/test-cnas-layout-b.ts /tmp/cnas-pdfs/Lista-furnizori-testare-genetica-2024-2025_all.pdf
 *   npx tsx scripts/test-cnas-layout-b.ts /tmp/cnas-pdfs/*.pdf
 */
import { execFile } from 'child_process';
import { promisify } from 'util';
import { basename } from 'path';
import { parseProviderTextJudetGrouped, parseProviderTextRadio, parseProviderTextSingleCAS, parseProviderTextNumberedDot } from '../src/cnas-layout-b.js';

const execFileAsync = promisify(execFile);

async function pdftotextLayout(pdfPath: string): Promise<string> {
  const { stdout } = await execFileAsync('pdftotext', ['-layout', '-enc', 'UTF-8', pdfPath, '-'], {
    maxBuffer: 64 * 1024 * 1024,
  });
  return stdout;
}

async function main() {
  const files = process.argv.slice(2);
  if (files.length === 0) {
    console.error('Usage: tsx test-cnas-layout-b.ts <pdf>...');
    process.exit(1);
  }
  for (const f of files) {
    const fn = basename(f);
    console.log(`\n=== ${fn} ===`);
    const text = await pdftotextLayout(f);
    let rows;
    if (/radioterapie/i.test(fn)) {
      rows = parseProviderTextRadio(text, { tip: 'radioterapie' });
    } else if (/CAS-GORJ.*PNS/i.test(fn) || /Valori-de-contract-furnizori-PNS/i.test(fn)) {
      rows = parseProviderTextSingleCAS(text, { tip: 'pns', judet: 'GORJ' });
    } else if (/ASISTENTA-MEDICALA-PRIMARA/i.test(fn)) {
      rows = parseProviderTextNumberedDot(text, { tip: 'medicina_familie', judet: 'SIBIU' });
    } else {
      rows = parseProviderTextJudetGrouped(text, { tip: 'oncologie' });
    }
    const limit = parseInt(process.env.TEST_LIMIT || '20');
    console.log(`Parsed ${rows.length} rows`);
    for (let i = 0; i < Math.min(rows.length, limit); i++) {
      const r = rows[i];
      console.log(`  [${i + 1}] judet=${r.judet || '-'} name="${r.name}" sediu="${r.sediu || '-'}" tel=${r.telefon || '-'} email=${r.email || '-'} flags=${r.specialitate || '-'}`);
    }
    if (rows.length > limit) console.log(`  ... and ${rows.length - limit} more`);
  }
}

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