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

interface TokenResponse {
  access_token: string;
  expires_in: number;
  token_type: string;
  scope?: string;
}

export function registerIssueToken(program: Command): void {
  program
    .command('issue-token')
    .description('Issue an OAuth2 access token for an agent')
    .requiredOption('--agent-id <id>', 'Agent ID to issue a token for')
    .action(async (options: { agentId: string }) => {
      const config = requireConfig();

      try {
        const body = new URLSearchParams({
          grant_type: 'client_credentials',
          client_id: config.clientId,
          client_secret: config.clientSecret,
          agent_id: options.agentId,
        });

        const res = await fetch(`${config.apiUrl}/oauth2/token`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: body.toString(),
        });

        if (!res.ok) {
          const text = await res.text();
          throw new Error(`Token issuance failed (${res.status}): ${text}`);
        }

        const data = (await res.json()) as TokenResponse;
        const expiresAt = new Date(
          Date.now() + data.expires_in * 1000,
        ).toISOString();

        console.log(chalk.green('✓') + ' Token issued successfully');
        console.log();
        console.log(chalk.bold('Access Token:'));
        console.log(chalk.cyan(data.access_token));
        console.log();
        console.log(
          chalk.bold('Token Type:  ') + data.token_type,
        );
        console.log(
          chalk.bold('Expires In:  ') + `${data.expires_in}s`,
        );
        console.log(
          chalk.bold('Expires At:  ') + chalk.dim(expiresAt),
        );
        if (data.scope !== undefined) {
          console.log(chalk.bold('Scope:       ') + data.scope);
        }

      } catch (err) {
        console.error(
          chalk.red('Error:'),
          err instanceof Error ? err.message : String(err),
        );
        process.exit(1);
      }
    });
}
