import process from 'node:process'
import { Command } from 'commander'
import { VERSION } from '../core/version.ts'
import { archiveCommand } from './commands/archive.ts'
import { daemonCommand } from './commands/daemon.ts'
import { listCommand } from './commands/list.ts'
import { pruneCommand } from './commands/prune.ts'
import { replayCommand } from './commands/replay.ts'
import { reportCommand } from './commands/report.ts'
import { rollbackCommand } from './commands/rollback.ts'
import { routeCommand } from './commands/route.ts'
import { runCommand } from './commands/run.ts'
import { toolCommand } from './commands/tool.ts'
import { updateCommand } from './commands/update.ts'
import { showCommand } from './commands/show.ts'
import { statusCommand } from './commands/status.ts'
import { tailCommand } from './commands/tail.ts'
import { taskCommand } from './commands/task.ts'
import { tasksCommand } from './commands/tasks.ts'
import { uiCommand, openUi } from './commands/ui.ts'
import { unwireCommand } from './commands/unwire.ts'
import { updateSupervisorCommand } from './commands/update-supervisor.ts'
import { wireCommand } from './commands/wire.ts'

const NAME = 'openthomas'
const DESCRIPTION =
  'Cut the cost of your agent fleet — without switching agents. ' +
  'Makes Claude Code Dynamic Workflows cheap: the planner stays on Opus, ' +
  'the hundreds of parallel subagents run on Sonnet (or a model you pick). ' +
  'Free and fully open source.'

export async function run(): Promise<void> {
  const program = new Command()
  program.name(NAME).description(DESCRIPTION).version(VERSION).showHelpAfterError()

  program.addCommand(wireCommand)
  program.addCommand(unwireCommand)
  program.addCommand(runCommand)
  program.addCommand(statusCommand)
  program.addCommand(tasksCommand)
  program.addCommand(taskCommand)
  program.addCommand(listCommand)
  program.addCommand(showCommand)
  program.addCommand(reportCommand)
  program.addCommand(tailCommand)
  program.addCommand(replayCommand)
  program.addCommand(routeCommand)
  program.addCommand(toolCommand)
  program.addCommand(pruneCommand)
  program.addCommand(archiveCommand)
  program.addCommand(daemonCommand)
  program.addCommand(uiCommand)
  program.addCommand(updateCommand)
  program.addCommand(rollbackCommand)
  program.addCommand(updateSupervisorCommand, { hidden: true })

  // Default action: bare `openthomas` opens the UI.
  program.action(async () => {
    await openUi({ noOpen: false })
  })

  await program.parseAsync(process.argv)
}
