import { printHelp } from './commands/help.js';
import { runAi } from './commands/ai.js';
import { runAuth } from './commands/auth.js';
import { runDb } from './commands/db.js';
import { runDeploy } from './commands/deploy.js';
import { runDev } from './commands/dev.js';
import { runDoctor } from './commands/doctor.js';
import { runEnv } from './commands/env.js';
import { runExport } from './commands/export.js';
import { runImport } from './commands/import.js';
import { runInit } from './commands/init.js';
import { runInvoke } from './commands/invoke.js';
import { runConnect } from './commands/connect.js';
import { runLink } from './commands/link.js';
import { runLogin } from './commands/login.js';
import { runLogout } from './commands/logout.js';
import { runLogs } from './commands/logs.js';
import { runProjects } from './commands/projects.js';
import { runSetupCommand } from './commands/setup.js';
import { runStorage } from './commands/storage.js';
import { printVersion } from './commands/version.js';
import { runWhoami } from './commands/whoami.js';
import { readProjectConfig } from './project-config.js';

export async function run(argv: readonly string[]): Promise<number> {
  const [first, ...rest] = argv;

  if (!first) {
    // Convex-style default: unlinked folder → setup; linked → watch.
    const local = await readProjectConfig();
    if (!local?.projectId) {
      return runSetupCommand([]);
    }
    return runDev([]);
  }
  if (first === '--help' || first === '-h' || first === 'help') {
    printHelp();
    return 0;
  }

  if (first === '--version' || first === '-v' || first === 'version') {
    printVersion();
    return 0;
  }

  switch (first) {
    case 'setup':
      return runSetupCommand(rest);
    case 'init':
      return runInit(rest);
    case 'connect':
      return runConnect(rest);
    case 'link':
      return runLink(rest);
    case 'login':
      return runLogin(rest);
    case 'logout':
      return runLogout(rest);
    case 'whoami':
      return runWhoami();
    case 'deploy':
      return runDeploy(rest);
    case 'invoke':
      return runInvoke(rest);
    case 'env':
      return runEnv(rest);
    case 'db':
      return runDb(rest);
    case 'logs':
      return runLogs(rest);
    case 'dev':
      return runDev(rest);
    case 'projects':
      return runProjects(rest);
    case 'export':
      return runExport(rest);
    case 'import':
      return runImport(rest);
    case 'doctor':
      return runDoctor(rest);
    case 'ai':
      return runAi(rest);
    case 'auth':
      return runAuth(rest);
    case 'storage':
      return runStorage(rest);
  }

  process.stderr.write(`briven: unknown command '${first}'\n`);
  process.stderr.write(`run 'briven --help' for usage\n`);
  return 1;
}
