#!/usr/bin/env bun
import { Command } from "commander";
import { commitCommand } from "./commands/commit";
import { configCommand, setConfigValue } from "./commands/config/index";
import { addCommonOptions } from "./utils/cli-options";

const program = new Command();

program
  .name("agi")
  .description("AI-powered development assistant CLI")
  .version("0.1.0");

program
  .command("status")
  .alias("st")
  .description("Show current provider and model")
  .action(async () => {
    const { statusCommand } = await import("./commands/config/index");
    await statusCommand();
  });

const commitCmd = program
  .command("commit")
  .alias("cm")
  .description("Generate AI-powered commit message from staged changes")
  .option("-y, --yes", "Skip confirmation prompt")
  .option("-d, --dry-run", "Preview commit message without creating commit")
  .option("-s, --show-diff", "Show staged changes before generating message");

// Add common options
addCommonOptions(commitCmd)
  .action(commitCommand);

const reviewCmd = program
  .command("review")
  .alias("rv")
  .description("AI-powered code review for changes")
  .option("-t, --target <target>", "What to review: staged, branch, commit (default: staged)");

// Add common options
addCommonOptions(reviewCmd)
  .action(async (options) => {
    const { reviewCommand } = await import("./commands/review");
    await reviewCommand(options);
  });

const configCmd = program
  .command("config")
  .description("Manage AGI configuration")
  .option("--init", "Initialize config with defaults")
  .option("--list", "Show full configuration")
  .option("--get <key>", "Get a config value")
  .option("--provider", "Interactive provider configuration")
  .action(configCommand);

configCmd
  .command("set <key> <value>")
  .description("Set a config key and value")
  .action(setConfigValue);

configCmd
  .command("provider <name>")
  .description("Set default provider")
  .action(async (name: string) => {
    await setConfigValue("defaultProvider", name);
  });

configCmd
  .command("key <provider> <api-key>")
  .description("Set API key for a provider")
  .action(async (provider: string, apiKey: string) => {
    const { setApiKeyCommand } = await import("./commands/config/index");
    await setApiKeyCommand(provider, apiKey);
  });

configCmd
  .command("add <provider>")
  .description("Add a new provider to configuration")
  .action(async (provider: string) => {
    const { addProviderCommand } = await import("./commands/config/index");
    await addProviderCommand(provider);
  });

configCmd
  .command("remove <provider>")
  .description("Remove a provider from configuration")
  .action(async (provider: string) => {
    const { removeProviderCommand } = await import("./commands/config/index");
    await removeProviderCommand(provider);
  });

// Handle case when no command is provided
program.action(() => {
  console.log("Welcome to AGI - AI-powered development assistant");
  console.log("=================================================\n");
  program.help();
});

program.parse();