/**
 * List generators command
 * Lists all available code generators
 */

import { globalRegistry } from '../../generators/generator-registry';
import * as output from '../output';
import '../../generators'; // Ensure generators are registered

interface ListGeneratorsOptions {
  verbose?: boolean;
  category?: string;
}

export function listGeneratorsCommand(options: ListGeneratorsOptions = {}) {
  output.banner('Bangstack Generators', 'Available code generators for your workflows');
  output.blank();

  // Filter by category if specified
  const shouldShow = (category: string) => !options.category || options.category === category;

  // Frontend generators
  const frontendFrameworks = globalRegistry.listFrontendFrameworks();
  if (frontendFrameworks.length > 0 && shouldShow('frontend')) {
    output.header('Frontend Frameworks');
    for (const framework of frontendFrameworks) {
      const generator = globalRegistry.getFrontendGenerator(framework);
      if (generator) {
        output.success(`${generator.name || framework}`);
        if (generator.description) {
          console.log(output.dim(`  ${generator.description}`));
        }
        if (options.verbose) {
          console.log(output.dim(`  Usage in workflow: ${output.code(`stack.frontend.technology: "${framework}"`)}`));
          if (generator.framework) {
            console.log(output.dim(`  Framework: ${generator.framework}`));
          }
        }
        output.blank();
      }
    }
  }

  // Backend generators
  const backendLanguages = globalRegistry.listBackendLanguages();
  if (backendLanguages.length > 0 && shouldShow('backend')) {
    output.header('Backend Languages');
    for (const language of backendLanguages) {
      const generator = globalRegistry.getBackendGenerator(language);
      if (generator) {
        output.success(`${generator.name || language}`);
        if (generator.description) {
          console.log(output.dim(`  ${generator.description}`));
        }
        if (options.verbose) {
          const techString = generator.framework ? `${language}/${generator.framework}` : language;
          console.log(output.dim(`  Usage in workflow: ${output.code(`stack.backend.technology: "${techString}"`)}`));
          if (generator.language) {
            console.log(output.dim(`  Language: ${generator.language}`));
          }
          if (generator.framework) {
            console.log(output.dim(`  Framework: ${generator.framework}`));
          }
        }
        output.blank();
      }
    }
  }

  // Documentation generators
  const docFormats = globalRegistry.listDocumentationFormats();
  if (docFormats.length > 0 && shouldShow('documentation')) {
    output.header('Documentation Formats');
    for (const format of docFormats) {
      const generator = globalRegistry.getDocumentationGenerator(format);
      if (generator) {
        output.success(`${generator.name || format}`);
        if (generator.description) {
          console.log(output.dim(`  ${generator.description}`));
        }
        if (options.verbose && generator.format) {
          console.log(output.dim(`  Format: ${generator.format}`));
        }
        output.blank();
      }
    }
  }

  // Database generators
  const databases = globalRegistry.listDatabases();
  if (databases.length > 0 && shouldShow('database')) {
    output.header('Database Generators');
    for (const db of databases) {
      const generator = globalRegistry.getDatabaseGenerator(db);
      if (generator) {
        output.success(`${generator.name || db}`);
        if (generator.description) {
          console.log(output.dim(`  ${generator.description}`));
        }
        if (options.verbose) {
          console.log(output.dim(`  Usage in workflow: ${output.code(`stack.database.technology: "${db}"`)}`));
          if (generator.database) {
            console.log(output.dim(`  Database: ${generator.database}`));
          }
        }
        output.blank();
      }
    }
  }

  // Test generators
  const testFrameworks = globalRegistry.listTestFrameworks();
  if (testFrameworks.length > 0 && shouldShow('test')) {
    output.header('Testing Frameworks');
    for (const framework of testFrameworks) {
      const generator = globalRegistry.getTestGenerator(framework);
      if (generator) {
        output.success(`${generator.name || framework}`);
        if (generator.description) {
          console.log(output.dim(`  ${generator.description}`));
        }
        if (options.verbose && generator.testFramework) {
          console.log(output.dim(`  Framework: ${generator.testFramework}`));
        }
        output.blank();
      }
    }
  }

  // Summary
  const total =
    frontendFrameworks.length +
    backendLanguages.length +
    docFormats.length +
    databases.length +
    testFrameworks.length;

  output.blank();
  output.header('Summary');
  output.table([
    ['Frontend Frameworks', frontendFrameworks.length.toString()],
    ['Backend Languages', backendLanguages.length.toString()],
    ['Documentation Formats', docFormats.length.toString()],
    ['Database Generators', databases.length.toString()],
    ['Testing Frameworks', testFrameworks.length.toString()],
    ['Total', output.bold(total.toString())],
  ]);

  output.blank();
  output.info(`Run ${output.code('bang list-generators --verbose')} for detailed information`);
  output.info(`Run ${output.code('bang list-generators --category <category>')} to filter by category`);
}
