/**
 * seed-gen-cli.ts — CLI for generating diverse Push3 seed variants (#638).
 *
 * Usage:
 *   tsx seed-gen-cli.ts --count <N> --output-dir <dir>
 *
 * Writes N Push3 variant files to <dir>/variant_000.push3 ... variant_NNN.push3
 * and prints each file path to stdout (one per line).
 *
 * The variants are systematically diverse: different staked% thresholds,
 * penalty thresholds, bull/bear output params, and tax rate distributions.
 *
 * Options:
 *   --count <N>         Number of variants to generate (required, positive integer)
 *   --output-dir <dir>  Directory to write variant files (required, created if absent)
 */

import { mkdirSync, writeFileSync } from 'fs';
import { join } from 'path';
import { selectVariants, generateSeedVariant } from './seed-generator';

function usage(): void {
  process.stderr.write('Usage: seed-gen-cli --count <N> --output-dir <dir>\n');
}

const args = process.argv.slice(2);
let count: number | undefined;
let outputDir: string | undefined;

for (let i = 0; i < args.length; i++) {
  if (args[i] === '--count' && args[i + 1] !== undefined) {
    count = parseInt(args[++i]!, 10);
  } else if (args[i] === '--output-dir' && args[i + 1] !== undefined) {
    outputDir = args[++i];
  } else {
    process.stderr.write(`Unknown argument: ${args[i]!}\n`);
    usage();
    process.exit(1);
  }
}

if (count === undefined || isNaN(count) || count < 1) {
  process.stderr.write('Error: --count must be a positive integer\n');
  usage();
  process.exit(1);
}

if (!outputDir) {
  process.stderr.write('Error: --output-dir is required\n');
  usage();
  process.exit(1);
}

mkdirSync(outputDir, { recursive: true });

const variants = selectVariants(count);
if (variants.length < count) {
  process.stderr.write(
    `[seed-gen] Note: --count ${count} exceeds the 1152-variant parameter space;` +
      ` generating ${variants.length} variants. The remaining ${count - variants.length}` +
      ` slots in evolve.sh will be filled by mutating the seed.\n`,
  );
}

for (let i = 0; i < variants.length; i++) {
  const text = generateSeedVariant(variants[i]!);
  const filename = `variant_${String(i).padStart(3, '0')}.push3`;
  const filepath = join(outputDir, filename);
  writeFileSync(filepath, text + '\n', 'utf8');
  process.stdout.write(filepath + '\n');
}
