#!/usr/bin/env node
/**
 * @fileoverview OrdoJS CLI - Command-line interface entry point
 */

import { Command } from 'commander';
import { registerCommands } from './commands/index.js';
import { CLI_VERSION } from './index.js';
import { logger } from './utils/index.js';

/**
 * Main CLI function
 */
export async function cli(args: string[] = process.argv): Promise<void> {
  try {
    const program = new Command();

    // Set up program metadata with enhanced version info
    program
        .name('ordojs')
  .description('OrdoJS Framework CLI - Revolutionary web framework with compile-time optimizations')
      .version(CLI_VERSION, '-v, --version', 'Output the current version')
      .helpOption('-h, --help', 'Display help information');

    // Add a custom version command that shows more details
    program
      .command('version')
      .description('Show detailed version information')
      .action(() => {
        console.log(`OrdoJS Framework CLI v${CLI_VERSION}`);
        console.log('A revolutionary web framework with compile-time optimizations');
        console.log('Status: Beta - Not recommended for production use');
        console.log('For more information: https://github.com/Piggzy76/OrdoJS');
      });

    // Register all commands
    registerCommands(program);

    // Add global options
    program
      .option('-d, --debug', 'Enable debug mode')
      .option('-s, --silent', 'Suppress all output');

    // Parse arguments
    await program.parseAsync(args);

    // If no command is provided, show help
    if (!process.argv.slice(2).length) {
      program.outputHelp();
    }
  } catch (error) {
    logger.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
    process.exit(1);
  }
}

// Run CLI if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
  cli().catch((error) => {
    logger.error(`Fatal error: ${error instanceof Error ? error.message : String(error)}`);
    process.exit(1);
  });
}
