"use strict";

function cmdHelp() {
  const text = [
    "SDTK-SPEC CLI -- deterministic documentation toolkit",
    "",
    "Usage:",
    "  sdtk-spec <command> [options]",
    "",
    "Commands:",
    "  init          Initialize SDTK-SPEC workspace (runtime adapter + config)",
    "  atlas         Build and browse the free local Atlas compatibility graph",
    "  runtime       Manage runtime asset installation (install, uninstall, status)",
    "  update        Refresh the npm package and optionally re-apply managed project/runtime assets",
    "  auth          Manage GitHub authentication and entitlement",
    "  activate      Activate Pro features with a license key (public activation flow)",
    "  entitlement   Inspect local entitlement and capability state",
    "  generate      Create the standard 17-file feature scaffold from templates",
    "  analyze       Preserve requirement source, bootstrap scaffold, and create an agent task packet",
    "",
    "Init options:",
    "  --runtime <codex|claude>       Runtime adapter (default: codex)",
    "  --runtime-scope <project|user> Install scope (default: project for claude, user for codex)",
    "  --project-path <path>          Target project directory (default: cwd)",
    "  --force                        Overwrite existing files",
    "  --skip-runtime-assets          Skip runtime asset installation",
    "  --skip-skills                  (deprecated, use --skip-runtime-assets)",
    "  --verbose                      Show detailed PowerShell script output",
    "",
    "Runtime subcommands:",
    "  sdtk-spec runtime install   --runtime <codex|claude> [--scope <project|user>]",
    "  sdtk-spec runtime uninstall --runtime <codex|claude> [--scope <project|user>] [--all]",
    "  sdtk-spec runtime status    --runtime <codex|claude>",
    "",
    "Update options:",
    "  --version <latest|x.y.z>    Target published npm package version (default: latest)",
    "  --runtime <codex|claude>    Optional runtime refresh target after npm update",
    "  --scope <project|user>      Runtime asset scope when --runtime is provided",
    "  --project-path <path>       Target project directory for managed file refresh (default: cwd)",
    "  --check-only                Print the planned npm/init refresh steps without making changes",
    "  --skip-project-files        Skip sdtk-spec init --force during update",
    "  --skip-runtime-assets       Skip runtime asset refresh during update",
    "  --verbose                   Show detailed command output",
    "  Maintainer-root guard: init/update refuse the SDTK maintainer monorepo root as a managed project target.",
    "  Use a consumer project path, or use --skip-project-files when update should avoid project-file refresh.",
    "",
    "Runtime options:",
    "  --runtime <codex|claude>   Target runtime (required)",
    "  --scope <project|user>     Install scope (default: project for claude, user for codex)",
    "  --project-path <path>      Target project directory (default: cwd)",
    "  --force                    Overwrite existing skills",
    "  --all                      Uninstall all managed skills",
    "  --verbose                  Show detailed script output",
    "",
    "Scope behavior:",
    "  project  Claude installs to <project>/.claude/skills and Codex installs to <project>/.codex/skills",
    "  user     Skills are installed in the runtime home (~/.claude/skills/ or $CODEX_HOME/skills / ~/.codex/skills)",
    "  Note: Codex project scope is supported only via the explicit local CODEX_HOME=<project>/.codex contract.",
    "  Note: This CLI does not claim native .codex/skills auto-discovery.",
    "",
    "Uninstall behavior:",
    "  runtime uninstall removes only SDTK-managed skill folders for the selected runtime/scope.",
    "  It does not delete parent runtime roots such as .claude/, .claude/skills/, $CODEX_HOME/, or $CODEX_HOME/skills/.",
    "  npm uninstall -g sdtk-spec-kit removes the CLI package only. Runtime assets remain until runtime uninstall or manual cleanup.",
    "",
    "Auth options:",
    "  --token <value>            Store GitHub PAT",
    "  --verify                   Check private repo access",
    "  --status                   Show authentication status",
    "  --logout                   Clear stored credentials",
    "",
    "Activate options:",
    "  --license <KEY>            License key for Pro activation (required)",
    "  --json                     Print result as JSON",
    "",
    "Activation notes:",
    "  - sdtk-spec activate is the public Pro activation path for paid users.",
    "  - You will receive a license key (SDTK-XXXX-YYYY format) by email after purchase.",
    "  - Activation is machine-bound: each license can be activated on up to 5 machines.",
    "  - Same-machine reactivation is idempotent (safe to retry).",
    "  - Once activated, you can use Pro commands without additional authentication.",
    "  - For immediate Premium access, run: sdtk-spec activate --license <KEY>",
    "",
    "Entitlement subcommands:",
    "  sdtk-spec entitlement status   Show local entitlement and capability state (local-only, no network)",
    "  sdtk-spec entitlement sync     Fetch and verify a signed entitlement manifest; install required SPEC packs",
    "",
    "Generate options:",
    "  --feature-key <KEY>        Feature identifier (UPPER_SNAKE_CASE, required)",
    "  --feature-name \"<text>\"    Human-readable feature name (required)",
    "  --project-path <path>      Target project directory (default: cwd)",
    "  --force                    Overwrite existing files",
    "  --validate-only            Validate templates without writing files",
    "  --verbose                  Show detailed PowerShell script output",
    "",
    "Analyze options:",
    "  --source <file>            Customer requirement Markdown/text file (required)",
    "  --feature-key <KEY>        Feature identifier (UPPER_SNAKE_CASE, required)",
    "  --feature-name \"<text>\"    Human-readable feature name (required)",
    "  --project-path <path>      Target project directory (default: cwd)",
    "  --force                    Overwrite managed analysis artifacts",
    "  --json                     Print result summary as JSON",
    "",
    "Atlas subcommands (free compatibility path):",
    "  sdtk-spec atlas init     Initialize Atlas config and run first build (opens viewer)",
    "  sdtk-spec atlas build    Rebuild the local document graph from project markdown",
    "  sdtk-spec atlas open     Open the last successful atlas build in a local browser",
    "  sdtk-spec atlas watch    Watch for markdown changes and rebuild automatically",
    "  sdtk-spec atlas status   Show initialization state and last build summary",
    "",
    "Project subcommands (Pro):",
    "  sdtk-spec project ingest   Build reusable project census and profile foundation from an existing repo (Pro)",
    "  sdtk-spec project audit    Read-only audit: readiness score, gap list, risk list, next steps (Pro)",
    "  sdtk-spec project refresh  Incremental managed refresh of .sdtk/project/ artifacts (Pro)",
    "",
    "Atlas options (init):",
    "  --project-path <path>    Target project directory (default: cwd)",
    "  --output-dir <path>      Atlas output directory (default: <project>/.sdtk/atlas)",
    "  --scan-root <path>       Scan root (repeatable; default: project root)",
    "  --force                  Overwrite existing Atlas config",
    "  --no-build               Skip initial build",
    "  --no-open                Skip opening the viewer",
    "  --verbose                Show detailed build output",
    "",
    "Atlas options (build):",
    "  --project-path <path>    Target project directory (default: cwd)",
    "  --output-dir <path>      Atlas output directory (default: <project>/.sdtk/atlas)",
    "  --scan-root <path>       Scan root (repeatable; default: project root)",
    "  --verbose                Show incremental build detail",
    "",
    "Atlas options (open):",
    "  --project-path <path>    Target project directory (default: cwd)",
    "  --output-dir <path>      Atlas output directory (default: <project>/.sdtk/atlas)",
    "  --host <host>            Viewer server host (default: 127.0.0.1)",
    "  --port <port>            Viewer server port (default: 8765)",
    "  --no-open                Print viewer URL without opening browser",
    "",
    "Atlas options (watch):",
    "  --project-path <path>    Target project directory (default: cwd)",
    "  --output-dir <path>      Atlas output directory (default: <project>/.sdtk/atlas)",
    "  --scan-root <path>       Scan root (repeatable; default: project root)",
    "  --port <port>            Viewer server port (default: 8765)",
    "  --no-open                Skip opening the viewer after initial build",
    "",
    "Atlas notes:",
    "  - Free Atlas commands (init, build, open, watch, status) require no authentication or entitlement.",
    "  - Atlas scans markdown files and builds a local document graph and static viewer.",
    "  - sdtk-spec atlas is the R1 compatibility path; SDTK-WIKI is the canonical home for new wiki work.",
    "  - Artifacts are written under <project>/.sdtk/atlas/ (project-local compatibility output).",
    "  - Native SDTK-WIKI uses <project>/.sdtk/wiki/ and <project>/.sdtk/wiki/graph/.",
    "  - The old SPEC ask command path is not supported; native Ask is sdtk-wiki ask with capability wiki.ask.",
    "  - Viewer server binds to 127.0.0.1 by default (loopback only).",
    "  - Requires Python 3.8+ in PATH for the build step.",
    "",
    "Project ingest options (Pro):",
    "  --project-path <path>    Target project directory to ingest (default: cwd)",
    "  --output-dir <path>      Foundation output directory (default: <project>/.sdtk/project)",
    "  --json                   Print result summary as JSON",
    "",
    "Project ingest notes:",
    "  - project ingest is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
    "  - Run 'sdtk-spec entitlement sync' to install the required premium pack for project ingest.",
    "  - project ingest is not part of sdtk-spec init. It is a separate premium command.",
    "  - Foundation artifacts are written under <project>/.sdtk/project/ (project-local, not global).",
    "  - Artifacts include census, source inventory, profile, module graph, runtime markers, and evidence packs.",
    "  - project ingest now stages a draft SDTK docs baseline under .sdtk/project/DOC_SYNTHESIS_BASELINE/.",
    "  - Staged docs are draft / human-review-first outputs. Human review is required before use.",
    "  - Live project /docs/ is not modified by project ingest.",
    "  - Maintainer-root guard: project ingest refuses the SDTK maintainer monorepo root as an ingest target.",
    "",
    "Project audit options (Pro):",
    "  --project-path <path>    Target project directory to audit (default: cwd)",
    "  --output-dir <path>      Foundation read path (default: <project>/.sdtk/project)",
    "  --json                   Print audit result as deterministic JSON",
    "",
    "Project audit notes:",
    "  - project audit is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
    "  - Run 'sdtk-spec entitlement sync' to install the required premium pack for project audit.",
    "  - project audit is read-only. It does not modify live /docs/ or write new staged docs.",
    "  - project audit does not write new .sdtk/project/ foundation artifacts.",
    "  - When cached ingest artifacts (.sdtk/project/) exist, project audit uses them as its source.",
    "  - When no prior ingest has been run, project audit performs bounded ephemeral analysis in memory.",
    "  - Output includes readiness score/band, gap list, risk list, and bounded next-step recommendations.",
    "  - Maintainer-root guard: project audit refuses the SDTK maintainer monorepo root as an audit target.",
    "",
    "Project refresh options (Pro):",
    "  --project-path <path>    Target project directory to refresh (default: cwd)",
    "  --output-dir <path>      Foundation output directory (default: <project>/.sdtk/project)",
    "  --json                   Print result summary as JSON",
    "",
    "Project refresh notes:",
    "  - project refresh is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
    "  - Run 'sdtk-spec entitlement sync' to install the required premium pack for project refresh.",
    "  - project refresh is incremental. It requires an existing managed ingest baseline.",
    "  - If no prior ingest baseline exists, run 'sdtk-spec project ingest' first.",
    "  - project refresh updates only .sdtk/project/ artifacts (foundation JSON, evidence packs, staged docs).",
    "  - project refresh may remove stale managed staged-doc files from the staged baseline (.sdtk/project/DOC_SYNTHESIS_BASELINE/docs/).",
    "  - project refresh does not modify live /docs/.",
    "  - project refresh does not replace or extend project ingest (first-run bootstrap).",
    "  - project refresh does not perform a read-only audit; use 'project audit' for that.",
    "  - Maintainer-root guard: project refresh refuses the SDTK maintainer monorepo root as a refresh target.",
    "",
    "Generate behavior:",
    "  The generate command creates the standard 17-file scaffold with TBD placeholders.",
    "  Scaffolds define document structure for each SDLC phase.",
    "  Content is filled in by agents or manually (PM -> BA -> ARCH -> DEV -> QA).",
    "  Output is written to docs/ subdirectories under the project path.",
    "",
    "Analyze behavior:",
    "  The analyze command preserves a requirement source under .sdtk/spec/requirements/.",
    "  It bootstraps the standard 17-file scaffold when missing by reusing generate behavior.",
    "  It creates .sdtk/spec/tasks/<KEY>/ task packets for PM/BA/ARCH/DEV/QA agent analysis.",
    "  It reports likely mojibake/encoding risk and source hash traceability.",
    "  It does not fully understand arbitrary requirements by itself.",
    "  PM/BA/ARCH agents complete source-backed analysis inside the generated artifacts.",
    "  It does not modify the source project and does not create .sdtk/atlas.",
    "",
    "Global options:",
    "  -h, --help                 Show this help",
    "  -v, --version              Show version",
    "",
    "Exit codes:",
    "  0  Success",
    "  1  Validation or user error",
    "  2  Dependency error (e.g., PowerShell not found)",
    "  3  Integrity error (payload hash mismatch)",
    "  4  Unexpected internal error",
    "",
    "Environment variables:",
    "  SDTK_ENTITLEMENT_REPO          Override default entitlement repo (owner/repo format)",
    "  SDTK_ENTITLEMENT_MANIFEST_PATH Override entitlement manifest path in repo (default: entitlements/spec/entitlements.json)",
    "  SDTK_ENTITLEMENT_REF           Override entitlement manifest ref/branch (default: main)",
    "  SDTK_ENTITLEMENT_PUBLIC_KEY    RSA public key (PEM) used to verify entitlement manifests",
    "",
    "Examples:",
    "  sdtk-spec activate --license SDTK-XXXX-YYYY",
    "  sdtk-spec activate --license SDTK-XXXX-YYYY --json",
    "  sdtk-spec atlas init --project-path ./my-project",
    "  sdtk-spec atlas init --project-path ./my-project --no-open",
    "  sdtk-spec atlas build --project-path ./my-project --verbose",
    "  sdtk-spec atlas open --project-path ./my-project --port 9000",
    "  sdtk-spec atlas open --project-path ./my-project --no-open",
    "  sdtk-spec atlas watch --project-path ./my-project",
    "  sdtk-spec atlas status --project-path ./my-project",
    "  sdtk-spec init --runtime codex",
    "  sdtk-spec init --runtime codex --runtime-scope project --project-path ./my-project",
    "  sdtk-spec init --runtime claude --project-path ./my-project",
    "  sdtk-spec update --check-only",
    "  sdtk-spec update --runtime claude --project-path ./my-project",
    "  sdtk-spec update --version 1.2.3 --runtime codex --scope user",
    "  sdtk-spec auth --token ghp_xxxxxxxxxxxx",
    "  sdtk-spec auth --verify",
    '  sdtk-spec generate --feature-key USER_PROFILE --feature-name "User Profile"',
    "  sdtk-spec generate --feature-key ORDER_MGMT --feature-name \"Order Management\" --validate-only",
    "  sdtk-spec analyze --source ./requirements.md --feature-key ORDER_PORTAL --feature-name \"Order Portal\"",
    "  sdtk-spec init --runtime claude --runtime-scope user",
    "  sdtk-spec runtime install --runtime claude --scope project",
    "  sdtk-spec runtime install --runtime codex --scope user",
    "  sdtk-spec runtime install --runtime codex --scope project --project-path ./my-project",
    "  sdtk-spec runtime status --runtime claude",
    "  sdtk-spec runtime uninstall --runtime claude --scope project --all",
    "  sdtk-spec entitlement status",
    "  sdtk-spec entitlement sync",
    "  sdtk-spec project ingest --project-path ./my-project",
    "  sdtk-spec project ingest --project-path ./my-project --json",
    "  sdtk-spec project audit --project-path ./my-project",
    "  sdtk-spec project audit --project-path ./my-project --json",
    "  sdtk-spec project refresh --project-path ./my-project",
    "  sdtk-spec project refresh --project-path ./my-project --json",
    "",
    "  # Override entitlement repo (bash/zsh):",
    "  export SDTK_ENTITLEMENT_REPO=owner/repo",
    "  # Override entitlement repo (PowerShell):",
    '  $env:SDTK_ENTITLEMENT_REPO="owner/repo"',
  ];
  console.log(text.join("\n"));
  return 0;
}

module.exports = {
  cmdHelp,
};
