#!/usr/bin/env node

const args = process.argv.slice(2);
const command = args[0];
const flags = {};
for (let i = 0; i < args.length; i++) {
  if (args[i].startsWith("--")) {
    const key = args[i].slice(2);
    if (args[i + 1] && !args[i + 1].startsWith("--")) {
      flags[key] = args[i + 1];
      i++;
    } else {
      flags[key] = true;
    }
  }
}

async function main() {
  switch (command) {
    case "route":
      return require("../src/commands/route")(args.slice(1), flags);
    case "consult":
      return require("../src/commands/consult")(args.slice(1), flags);
    case "status":
      return require("../src/commands/status")(args.slice(1), flags);
    case "doctor":
      return require("../src/commands/doctor")(args.slice(1), flags);
    case "init":
      return require("../src/commands/init")(args.slice(1), flags);
    case "scaffold":
      return require("../src/commands/scaffold")(args.slice(1), flags);
    case "audit":
      return require("../src/commands/audit")(args.slice(1), flags);
    case "ide":
      return require("../src/commands/ide")(args.slice(1), flags);
    case "uninstall":
      return require("../src/commands/uninstall")(args.slice(1), flags);
    case "upgrade":
      return require("../src/commands/upgrade")(args.slice(1), flags);
    case "memory":
      return require("../src/commands/memory")(args.slice(1), flags);
    case "skill":
      return require("../src/commands/skill")(args.slice(1), flags);
    case "recover":
      return require("../src/commands/recover")(args.slice(1), flags);
    case "ui":
      return require("../src/commands/ui")(args.slice(1), flags);
    case "pipeline":
      return require("../src/commands/pipeline")(args.slice(1), flags);
    case "version":
    case "--version":
    case "-v":
      console.log(`capint v${require("../package.json").version}`);
      return;
    case "help":
    case "--help":
    case "-h":
    case undefined:
      showHelp();
      return;
    default:
      console.error(`Unknown command: ${command}`);
      showHelp();
      process.exit(1);
  }
}

function showHelp() {
  console.log(`
capint ${require("../package.json").version}

Usage:
  capint route "<task>"
  capint route --json "<task>"
  capint route --verbose "<task>"
  capint route --list

  capint consult "<task>" [--json]     dry-run recommendation (no side effects)
  capint status [--json]               read-only project health
  capint doctor [--json]               diagnose drift (no auto-fix)
  capint audit [--json]                project + contract audit (read-only)

  capint init [--preset minimal] [--bundle full|minimal] [--name Project] [--no-ide-sync] [--json]
  capint scaffold [--preset minimal|prismx-compatible] [--list] [--json]

  capint ide sync|check [--dry-run] [--json] [--targets cursor,claude,...]
  capint uninstall [--dry-run] [--yes] [--keep-agent] [--keep-ide] [--include-sidecars]
  capint upgrade [--dry-run] [--apply]
  capint recover [--list] [--latest] [--stamp <id>] [--apply]
  capint skill list|disable|enable|pin [--json]
  capint ui [--port 3721] [--open] [--json]
  capint pipeline ["<task>"] [--json] [--skip-ide] [--force]
  capint memory status [--json]

  capint version
`);
}

main().catch((err) => {
  console.error(err.message);
  process.exit(1);
});
