/**
 * CLI bootstrap — PLAN_RELAY_PHASE1 C2. The `dist/cli.js` entrypoint (the `#!/usr/bin/env node`
 * shebang is prepended at build time via esbuild `--banner`, NOT here). Reads argv, builds the
 * real CliDeps, dispatches through `runCli`, and maps any error to a clean stderr line + exit code
 * (usage errors ⇒ 2, everything else ⇒ 1) so no invocation ever crashes with a raw stack trace.
 */
import { parseArgs, runCli, CliUsageError } from './cli.js'
import { createCliDeps } from './cli/deps.js'

async function main(): Promise<number> {
  const argv = process.argv.slice(2)
  const deps = createCliDeps()
  try {
    return await runCli(parseArgs(argv), deps)
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err)
    process.stderr.write(`web-terminal-agent: ${message}\n`)
    return err instanceof CliUsageError ? 2 : 1
  }
}

main()
  .then((code) => {
    process.exitCode = code
  })
  .catch((err: unknown) => {
    const message = err instanceof Error ? err.message : String(err)
    process.stderr.write(`web-terminal-agent: fatal ${message}\n`)
    process.exitCode = 1
  })
