import { Command } from 'commander';
import chalk from 'chalk';
import { requireConfig } from '../config';
import { apiRequest } from '../api';

interface AgentResponse {
  id: string;
  name: string;
  description?: string;
  status: string;
  createdAt: string;
}

export function registerRegisterAgent(program: Command): void {
  program
    .command('register-agent')
    .description('Register a new agent')
    .requiredOption('--name <name>', 'Agent name')
    .option('--description <desc>', 'Agent description')
    .action(async (options: { name: string; description?: string }) => {
      const config = requireConfig();

      try {
        const body: { name: string; description?: string } = {
          name: options.name,
        };
        if (options.description !== undefined) {
          body.description = options.description;
        }

        const agent = await apiRequest<AgentResponse>(config, '/agents', {
          method: 'POST',
          body,
        });

        console.log(chalk.green('✓') + ' Agent registered successfully');
        console.log();
        console.log(
          chalk.bold('Agent ID:   ') + chalk.cyan(agent.id),
        );
        console.log(chalk.bold('Name:       ') + agent.name);
        if (agent.description !== undefined) {
          console.log(chalk.bold('Description:') + ' ' + agent.description);
        }
        console.log(chalk.bold('Status:     ') + agent.status);
      } catch (err) {
        console.error(
          chalk.red('Error:'),
          err instanceof Error ? err.message : String(err),
        );
        process.exit(1);
      }
    });
}
