const SUBCOMMAND_HELP = {
    config: [
        'Usage: nerf config',
        '',
        'Re-run the interactive setup wizard to configure:',
        '  - LLM provider (Anthropic, OpenAI, Google, etc.)',
        '  - Web search provider (Brave, Tavily, built-in)',
        '  - Remote questions (Discord, Slack, Telegram)',
        '  - Tool API keys (Context7, Jina, Groq)',
        '',
        'All steps are skippable and can be changed later with /login or /search-provider.',
    ].join('\n'),
    update: [
        'Usage: nerf update',
        '',
        'Update NERF to the latest version.',
        '',
        'Equivalent to: npm install -g @defconxt/nerf@latest',
    ].join('\n'),
    sessions: [
        'Usage: nerf sessions',
        '',
        'List all saved sessions for the current directory and interactively',
        'pick one to resume. Shows date, message count, and a preview of the',
        'first message for each session.',
        '',
        'Sessions are stored per-directory, so you only see sessions that were',
        'started from the current working directory.',
        '',
        'Compare with --continue (-c) which always resumes the most recent session.',
    ].join('\n'),
    install: [
        'Usage: nerf install <source> [-l, --local]',
        '',
        'Install a package/extension source and run post-install validation (dependency checks, setup).',
        '',
        'Examples:',
        '  nerf install npm:@foo/bar',
        '  nerf install git:github.com/user/repo',
        '  nerf install https://github.com/user/repo',
        '  nerf install ./local/path',
    ].join('\n'),
    remove: [
        'Usage: nerf remove <source> [-l, --local]',
        '',
        'Remove an installed package source and its settings entry.',
    ].join('\n'),
    list: [
        'Usage: nerf list',
        '',
        'List installed package sources from user and project settings.',
    ].join('\n'),
    worktree: [
        'Usage: nerf worktree <command> [args]',
        '',
        'Manage isolated git worktrees for parallel work streams.',
        '',
        'Commands:',
        '  list                 List worktrees with status (files changed, commits, dirty)',
        '  merge [name]         Squash-merge a worktree into main and clean up',
        '  clean                Remove all worktrees that have been merged or are empty',
        '  remove <name>        Remove a worktree (--force to remove with unmerged changes)',
        '',
        'The -w flag creates/resumes worktrees for interactive sessions:',
        '  nerf -w               Auto-name a new worktree, or resume the only active one',
        '  nerf -w my-feature    Create or resume a named worktree',
        '',
        'Lifecycle:',
        '  1. nerf -w             Create worktree, start session inside it',
        '  2. (work normally)    All changes happen on the worktree branch',
        '  3. Ctrl+C             Exit — dirty work is auto-committed',
        '  4. nerf -w             Resume where you left off',
        '  5. nerf worktree merge Squash-merge into main when done',
        '',
        'Examples:',
        '  nerf -w                              Start in a new auto-named worktree',
        '  nerf -w auth-refactor                Create/resume "auth-refactor" worktree',
        '  nerf worktree list                   See all worktrees and their status',
        '  nerf worktree merge auth-refactor    Merge and clean up',
        '  nerf worktree clean                  Remove all merged/empty worktrees',
        '  nerf worktree remove old-branch      Remove a specific worktree',
        '  nerf worktree remove old-branch --force  Remove even with unmerged changes',
    ].join('\n'),
    headless: [
        'Usage: nerf headless [flags] [command] [args...]',
        '',
        'Run /nerf commands without the TUI. Default command: auto',
        '',
        'Flags:',
        '  --timeout N            Overall timeout in ms (default: 300000)',
        '  --json                 JSONL event stream to stdout (alias for --output-format stream-json)',
        '  --output-format <fmt>  Output format: text (default), json (structured result), stream-json (JSONL events)',
        '  --bare                 Minimal context: skip CLAUDE.md, AGENTS.md, user settings, user skills',
        '  --resume <id>          Resume a prior headless session by ID',
        '  --model ID             Override model',
        '  --supervised           Forward interactive UI requests to orchestrator via stdout/stdin',
        '  --response-timeout N   Timeout (ms) for orchestrator response (default: 30000)',
        '  --answers <path>       Pre-supply answers and secrets (JSON file)',
        '  --events <types>       Filter JSONL output to specific event types (comma-separated)',
        '',
        'Commands:',
        '  auto                 Run all queued units continuously (default)',
        '  next                 Run one unit',
        '  status               Show progress dashboard',
        '  new-milestone        Create a milestone from a specification document',
        '  query                JSON snapshot: state + next dispatch + costs (no LLM)',
        '',
        'new-milestone flags:',
        '  --context <path>     Path to spec/PRD file (use \'-\' for stdin)',
        '  --context-text <txt> Inline specification text',
        '  --auto               Start auto-mode after milestone creation',
        '  --verbose            Show tool calls in progress output',
        '',
        'Output formats:',
        '  text         Human-readable progress on stderr (default)',
        '  json         Collect events silently, emit structured HeadlessJsonResult on stdout at exit',
        '  stream-json  Stream JSONL events to stdout in real time (same as --json)',
        '',
        'Examples:',
        '  nerf headless                                    Run /nerf auto',
        '  nerf headless next                               Run one unit',
        '  nerf headless --output-format json auto           Structured JSON result on stdout',
        '  nerf headless --json status                      Machine-readable JSONL stream',
        '  nerf headless --timeout 60000                    With 1-minute timeout',
        '  nerf headless --bare auto                        Minimal context (CI/ecosystem use)',
        '  nerf headless --resume abc123 auto               Resume a prior session',
        '  nerf headless new-milestone --context spec.md    Create milestone from file',
        '  cat spec.md | nerf headless new-milestone --context -   From stdin',
        '  nerf headless new-milestone --context spec.md --auto    Create + auto-execute',
        '  nerf headless --supervised auto                     Supervised orchestrator mode',
        '  nerf headless --answers answers.json auto              With pre-supplied answers',
        '  nerf headless --events agent_end,extension_ui_request auto   Filtered event stream',
        '  nerf headless query                              Instant JSON state snapshot',
        '',
        'Exit codes: 0 = success, 1 = error/timeout, 10 = blocked, 11 = cancelled',
    ].join('\n'),
};
// Alias: `nerf wt --help` → same as `nerf worktree --help`
SUBCOMMAND_HELP['wt'] = SUBCOMMAND_HELP['worktree'];
export function printHelp(version) {
    // R8: single write instead of 34 separate syscalls
    process.stdout.write([
        `NERF v${version}`,
        '',
        'Usage: nerf [options] [message...]',
        '',
        'Options:',
        '  --mode <text|json|rpc|mcp> Output mode (default: interactive)',
        '  --print, -p              Single-shot print mode',
        '  --continue, -c           Resume the most recent session',
        '  --worktree, -w [name]    Start in an isolated worktree (auto-named if omitted)',
        '  --model <id>             Override model (e.g. claude-opus-4-6)',
        '  --no-session             Disable session persistence',
        '  --extension <path>       Load additional extension',
        '  --tools <a,b,c>          Restrict available tools',
        '  --list-models [search]   List available models and exit',
        '  --version, -v, -V        Print version and exit',
        '  --help, -h               Print this help and exit',
        '  --auto <mode> [task]     Run autonomous mode (modes: red, blue, purple, ...)',
        '',
        'Security Commands:',
        '  status                   Show NERF status and configuration',
        '  version                  Print version as JSON',
        '  doctor                   Run health checks on tools and configuration',
        '  scan [target]            Run vulnerability scan',
        '  compliance [framework]   Run compliance assessment',
        '  query <message>          One-shot security query',
        '',
        'Subcommands:',
        '  config                   Re-run the setup wizard',
        '  install <source>         Install a package/extension source',
        '  remove <source>          Remove an installed package source',
        '  list                     List installed package sources',
        '  update                   Update NERF to the latest version',
        '  sessions                 List and resume a past session',
        '  worktree <cmd>           Manage worktrees (list, merge, clean, remove)',
        '  headless [cmd] [args]    Run /nerf commands without TUI (default: auto)',
        '',
        'Run nerf <subcommand> --help for subcommand-specific help.',
        '',
    ].join('\n'));
}
export function printSubcommandHelp(subcommand, version) {
    const help = SUBCOMMAND_HELP[subcommand];
    if (!help)
        return false;
    process.stdout.write(`NERF v${version}\n\n`);
    process.stdout.write(help + '\n');
    return true;
}
