function generateCLIEntry(context) {
  const { spec } = context;
  const commands = context.commands ? Array.isArray(context.commands) ? context.commands : Object.entries(context.commands).map(([name, def]) => ({ name, ...def })) : extractCommands(spec);
  if (commands.length === 0) {
    return generateMinimalCLI(spec);
  }
  const rootCommand = commands[0];
  const subcommands = rootCommand.subcommands ? Object.entries(rootCommand.subcommands) : [];
  const imports = subcommands.map(
    ([name, _]) => `import { register${capitalize(name)}Command } from './commands/${name}.js';`
  ).join("\n");
  const registrations = subcommands.map(
    ([name, _]) => `  register${capitalize(name)}Command(program);`
  ).join("\n");
  const version = spec.version || spec.metadata?.version || context.componentVersion || (Array.isArray(spec.components) ? spec.components[0]?.version : Object.values(spec.components || {})[0]?.version) || "1.0.0";
  return `#!/usr/bin/env node
/**
 * ${rootCommand.description || "CLI Application"}
 * Generated from SpecVerse specification
 */

import { Command } from 'commander';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
${imports}

// Read version from package.json at runtime (not hardcoded)
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let _version = '${version}';
try {
  // Walk up to find package.json
  for (const rel of ['../../../package.json', '../../../../package.json', '../../../../../package.json']) {
    try {
      const pkg = JSON.parse(readFileSync(join(__dirname, rel), 'utf8'));
      if (pkg.version) { _version = pkg.version; break; }
    } catch { /* try next */ }
  }
} catch { /* use fallback */ }

const program = new Command();

program
  .name('${rootCommand.name}')
  .description('${rootCommand.description || ""}')
  .version(_version);

// Register all commands
${registrations}

// Parse and execute
program.parseAsync(process.argv).catch((error) => {
  console.error('Error:', error.message);
  process.exit(1);
});
`;
}
function extractCommands(spec) {
  if (spec.commands) {
    if (Array.isArray(spec.commands)) return spec.commands;
    return Object.entries(spec.commands).map(([name, def]) => ({
      name,
      ...def
    }));
  }
  const components = spec.components || [];
  const componentList = Array.isArray(components) ? components : Object.entries(components).map(([name, c]) => ({ name, ...c }));
  for (const comp of componentList) {
    if (comp.commands) {
      if (Array.isArray(comp.commands)) return comp.commands;
      return Object.entries(comp.commands).map(([name, def]) => ({
        name,
        ...def
      }));
    }
  }
  return [];
}
function generateMinimalCLI(spec) {
  return `#!/usr/bin/env node
/**
 * CLI Application
 * Generated from SpecVerse specification
 */

import { Command } from 'commander';

const program = new Command();

program
  .name('app')
  .description('Generated CLI application')
  .version('1.0.0');

program
  .command('status')
  .description('Show application status')
  .action(() => {
    console.log('Application is running');
  });

program.parseAsync(process.argv);
`;
}
function capitalize(str) {
  return str.split("-").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
}
export {
  generateCLIEntry as default
};
