import chalk from 'chalk';
import { getSkillDetail } from '../utils/versions.js';

export function registerInfoCommand(program) {
  program
    .command('info')
    .description('Show detailed metadata of a skill')
    .argument('<skill-name>', 'Name of the skill')
    .option('--json', 'Output as raw JSON')
    .action(async (skillName, opts) => {
      try {
        const data = await getSkillDetail(skillName);
        if (!data) {
          console.error(chalk.red(`\n  Skill "${skillName}" not found.\n`));
          console.error(chalk.gray('  Run "sophhub list" to see all available skills.'));
          process.exit(1);
        }

        if (opts.json) {
          console.log(JSON.stringify(data, null, 2));
          return;
        }

        const types = (data.types || []).join(', ');
        const label = (key) => chalk.cyan(key.padEnd(14));

        console.log();
        console.log(`  ${label('Skill:')}${data.name}`);
        if (data.displayName) {
          console.log(`  ${label('Display Name:')}${data.displayName}`);
        }
        console.log(`  ${label('Version:')}${data.version}`);
        console.log(`  ${label('Types:')}${types}`);
        if (data.description) {
          console.log(`  ${label('Description:')}${data.description}`);
        }
        console.log(`  ${label('Created:')}${data.createdAt || '-'}`);
        console.log(`  ${label('Updated:')}${data.updatedAt || '-'}`);

        const changelog = data.changelog || [];
        if (changelog.length > 0) {
          console.log();
          console.log(`  ${chalk.cyan('Changelog:')}`);
          for (const entry of changelog) {
            console.log(chalk.white(`    v${entry.version}`) + chalk.gray(` (${entry.date})`));
            for (const change of entry.changes || []) {
              console.log(chalk.gray(`      - ${change}`));
            }
          }
        }

        console.log();
      } catch (err) {
        console.error(chalk.red(`\n  Failed to get skill info: ${err.message}\n`));
        process.exit(1);
      }
    });
}
