#!/usr/bin/env node
import { Command } from 'commander';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { basename, dirname, join } from 'path';
import { runInit } from './commands/init.js';
import { runOnboard } from './commands/onboard.js';
import { runServe } from './commands/serve.js';
import { runWatch } from './commands/watch.js';
import { runReview } from './commands/review.js';
import { runStatus } from './commands/status.js';
import { runDiagnose } from './commands/diagnose.js';
import { runOptimize } from './commands/optimize.js';
import { runImpact } from './commands/impact.js';
import { runIssue } from './commands/issue.js';
import { runRun } from './commands/run.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const { version } = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf8'));
const invokedAs = basename(process.argv[1] ?? 'crosscheck').replace(/\.js$/, '');
const programName = invokedAs === 'ck' ? 'ck' : 'crosscheck';
const program = new Command();
program
    .name(programName)
    .description('Cross-vendor AI code review — Claude Code ↔ Codex')
    .version(`❤️  ${version}`);
program
    .command('init')
    .description('Check environment, verify CLI auth, write starter config')
    .option('-c, --config <path>', 'path to write config file')
    .action((opts) => runInit(opts.config));
program
    .command('onboard')
    .description('Guided setup — select repos to monitor and write config')
    .option('-c, --config <path>', 'config file path to write')
    .option('-y, --yes', 'skip confirmation prompts, accept defaults')
    .option('--personal', 'pre-select personal deployment mode, skip persona prompt')
    .option('--team', 'pre-select team deployment mode, skip persona prompt')
    .option('--reconfigure', 're-run setup (accepted for compatibility; onboard always reconfigures)')
    .action((opts) => void runOnboard(opts));
program
    .command('serve')
    .description('[BETA] Always-on webhook server (mac-mini / home server mode)')
    .option('-c, --config <path>', 'config file path')
    .option('--personal', 'personal mode this session only (does not save to config)')
    .option('--team', 'team mode this session only (does not save to config)')
    .option('--reconfigure', 're-run deployment setup and save new choice to config')
    .option('--backtrace', 'enable startup scan for unreviewed open PRs this session (overrides backtrace.enabled: false)')
    .option('--no-backtrace', 'skip startup scan for unreviewed open PRs this session (overrides backtrace.enabled: true)')
    .action((opts) => void runServe(opts));
program
    .command('watch')
    .description('Local dev mode — listen for PRs via gh webhook forward')
    .option('-c, --config <path>', 'config file path')
    .option('--personal', 'personal mode this session only (does not save to config)')
    .option('--team', 'team mode this session only (does not save to config)')
    .option('--reconfigure', 're-run deployment setup and save new choice to config')
    .option('--backtrace', 'enable startup scan for unreviewed open PRs this session (overrides backtrace.enabled: false)')
    .option('--no-backtrace', 'skip startup scan for unreviewed open PRs this session (overrides backtrace.enabled: true)')
    .action((opts) => void runWatch(opts));
program
    .command('review <pr-url>')
    .description('Manually trigger a review for a single PR URL')
    .option('-c, --config <path>', 'config file path')
    .option('-r, --reviewer <vendor>', 'force a specific reviewer: codex | claude (bypasses auto-detection)')
    .option('--vendor <vendor>', 'alias for --reviewer')
    .action((prUrl, opts) => void runReview(prUrl, opts.config, opts.reviewer ?? opts.vendor));
program
    .command('run <pr-url>')
    .description('Execute the full configured workflow against a single PR (review → fix → recheck)')
    .option('-c, --config <path>', 'config file path')
    .option('-r, --reviewer <vendor>', 'force a specific reviewer: codex | claude (bypasses attribution detection)')
    .option('--vendor <vendor>', 'alias for --reviewer')
    .option('--steps <list>', 'run only these step types, comma-separated: review,fix,recheck')
    .option('--dry-run', 'run the review but do not post a comment or apply fixes')
    .action((prUrl, opts) => void runRun(prUrl, { ...opts, reviewer: opts.reviewer ?? opts.vendor }));
program
    .command('status')
    .description('Show auth state, config summary, and CLI versions')
    .option('-c, --config <path>', 'config file path')
    .action((opts) => void runStatus(opts.config));
program
    .command('diagnose')
    .description('Analyze review logs — surface failure patterns, error trends, and improvement suggestions')
    .option('--json', 'output full report as JSON')
    .option('--since <date>', 'only analyze logs from this date onward (YYYY-MM-DD)')
    .action((opts) => void runDiagnose(opts));
program
    .command('optimize')
    .description('Use AI to improve review instructions based on diagnose output')
    .option('--apply', 'write the improved instructions to the review step in ~/.crosscheck/workflow.yml')
    .option('--dry-run', 'show diff without writing (default behavior)')
    .option('--agent <vendor>', 'force a specific agent: claude | codex')
    .option('--since <date>', 'limit the diagnose window (YYYY-MM-DD)')
    .option('-c, --config <path>', 'config file path')
    .action((opts) => void runOptimize(opts));
program
    .command('impact')
    .description('Report time saved, issues caught, and code quality trend from review history')
    .option('--json', 'output full report as JSON')
    .option('--since <date>', 'only analyze logs from this date onward (YYYY-MM-DD)')
    .option('--money', 'include a rough monetary estimate')
    .option('-c, --config <path>', 'config file path')
    .action((opts) => void runImpact(opts));
program
    .command('issue')
    .description('Detect errors in recent logs, draft a GitHub issue with AI, and submit after confirmation')
    .option('--since <date>', 'only look at logs from this date onward (YYYY-MM-DD, default: 3 days ago)')
    .option('--dry-run', 'print the draft without submitting')
    .option('-y, --yes', 'skip interactive questions and confirmation')
    .option('-c, --config <path>', 'config file path')
    .option('--opportunities', 'analyze logs for reliability patterns and improvement opportunities instead of error patterns')
    .action((opts) => void runIssue(opts));
program.parse();
//# sourceMappingURL=cli.js.map