import { Command } from 'commander';
import { runSync } from './commands/sync.js';
import { runEval } from './commands/eval.js';

const program = new Command();

program.name('claw').description('ServerlessClaw CLI for syncing and evaluation').version('1.0.0');

program
  .command('sync')
  .description('Sync your repository with the ServerlessClaw Mother Hub')
  .requiredOption('--hub <owner/repo>', 'Hub repository in format owner/repo')
  .option('--action <action>', 'Sync action: pull (update) or push (contribute)', 'pull')
  .option(
    '--branch <branch>',
    'Branch to push to (required for action=push)',
    'evolution-contribution'
  )
  .option('--prefix <path>', 'Subtree prefix (e.g., core/)', 'core/')
  .option('--method <method>', 'Sync method: fork or subtree', 'subtree')
  .option('--working-dir <path>', 'Working directory', process.cwd())
  .option('--check', 'Check if sync is feasible without making changes')
  .option('--abort-on-conflict', 'Abort immediately if conflicts detected')
  .option('--json', 'Output results as JSON')
  .option('--dry-run', 'Validate sync without making changes')
  .option('--verbose', 'Verbose output')
  .action((options) => {
    runSync({
      action: options.action,
      branch: options.branch,
      hub: options.hub,
      prefix: options.prefix,
      method: options.method,
      workingDir: options.workingDir,
      check: options.check,
      abortOnConflict: options.abortOnConflict,
      json: options.json,
      dryRun: options.dryRun,
      verbose: options.verbose,
    }).catch((err) => {
      console.error(err);
      process.exit(1);
    });
  });

program
  .command('eval')
  .description('Run Integrated Quantitative Evals (TDAD)')
  .option('--suite <suite>', 'Eval suite to run', 'default')
  .option('--json', 'Output results as JSON')
  .option('--verbose', 'Verbose output')
  .action((options) => {
    runEval(options).catch((err) => {
      console.error(err);
      process.exit(1);
    });
  });

program.parse(process.argv);
