/**
 * Wide synthetic seed entrypoint.
 *
 * Bootstraps ports + roles + super-admin profile (idempotent), then
 * generates faker-driven bulk client + interest rows per port. Default
 * 1000 clients/port; override via `WIDE_SEED_COUNT`.
 *
 * Run with: pnpm db:seed:wide-synthetic
 */

import 'dotenv/config';
import { seedBootstrap } from './seed-bootstrap';
import { seedWideSyntheticPortData, type WideSeedSummary } from './seed-wide-synthetic-data';

const DEFAULT_COUNT = 1000;

async function seed() {
  const target = Number(process.env.WIDE_SEED_COUNT ?? DEFAULT_COUNT);
  if (!Number.isFinite(target) || target < 1) {
    console.error(`Invalid WIDE_SEED_COUNT: ${process.env.WIDE_SEED_COUNT}`);
    process.exit(1);
  }

  console.log(`Seeding Port Nimara CRM (wide synthetic - ${target} clients/port)...`);

  const portIds = await seedBootstrap();

  console.log('');
  console.log('Seeding per-port wide synthetic data...');

  const summaries: Array<{ name: string; summary: WideSeedSummary | null }> = [];
  for (const p of portIds) {
    console.log(`  [${p.slug}] seeding ${target} synthetic clients...`);
    const summary = await seedWideSyntheticPortData(p.id, p.slug, target);
    summaries.push({ name: p.name, summary });
  }

  console.log('');
  console.log('─── Summary ───────────────────────────────────────────────');
  for (const s of summaries) {
    if (s.summary === null) {
      console.log(`  ✓ Port "${s.name}" - already at target count (skipped)`);
    } else {
      const x = s.summary;
      console.log(`  ✓ Port "${s.name}" - +${x.clients} clients, +${x.interests} interests`);
    }
  }
  console.log('');
  console.log('Wide synthetic seed complete!');

  process.exit(0);
}

seed().catch((err) => {
  console.error('Wide synthetic seed failed:', err);
  process.exit(1);
});
