import fs from 'fs';
import { Command } from 'commander';
import { initCommand } from './commands/init.js';
import { createCommand } from './commands/create.js';
import { runCommand } from './commands/run.js';
import { stopCommand } from './commands/stop.js';
import { listCommand } from './commands/list.js';
import { installCommand } from './commands/install.js';
import { uninstallCommand } from './commands/uninstall.js';
import { serverCommand } from './commands/server.js';
import { doctorCommand } from './commands/doctor.js';
import { onboardingCommand } from './commands/onboarding.js';
import { virtualOfficeCommand } from './commands/virtual-office.js';
import { jarvisCommand } from './commands/jarvis.js';
import { schedulerCommand } from './commands/scheduler.js';
import { reorganizeCommand } from './commands/reorganize.js';
import { syncTemplatesCommand } from './commands/sync-templates.js';
import { mcpCommand } from './commands/mcp.js';
import { ingestCommand } from './commands/ingest.js';
import { knowledgeCommand } from './commands/knowledge.js';
import { loginCommand } from './commands/login.js';
import { logoutCommand } from './commands/logout.js';
import { whoamiCommand } from './commands/whoami.js';
import { publishCommand } from './commands/publish.js';
import { registryInstallCommand } from './commands/registry-install.js';
import { searchCommand } from './commands/search.js';
import { infoCommand } from './commands/info.js';
import { outdatedCommand } from './commands/outdated.js';
import { updateCommand } from './commands/update.js';
import { findPackageRoot } from './utils/config.js';
function getVersion() {
    try {
        const pkgPath = `${findPackageRoot()}/package.json`;
        const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
        return pkg.version ?? '0.0.0';
    }
    catch {
        return '0.0.0';
    }
}
const program = new Command();
program
    .name('expxagents')
    .description('ExpxAgents — Multi-agent orchestration platform')
    .version(getVersion());
program
    .command('init')
    .description('Initialize project (dirs, .env, admin user)')
    .option('--update', 'Update core agents, agent catalog, and skill to latest version')
    .action(initCommand);
program
    .command('create [description]')
    .description('Create a new squad with Solution Architect')
    .action(createCommand);
program
    .command('run <name>')
    .description('Execute squad pipeline')
    .action(runCommand);
program
    .command('stop <name>')
    .description('Stop running squad')
    .action(stopCommand);
program
    .command('list')
    .description('List squads and skills')
    .action(listCommand);
program
    .command('reorganize')
    .description('Reorganize squads into Setor > Grupo > Sessão > Squad hierarchy')
    .action(reorganizeCommand);
program
    .command('sync-templates [squad]')
    .description('Sync Pencil .pen templates to .md specs for visual squads')
    .action(syncTemplatesCommand);
program
    .command('install <skill>')
    .description('Install skill from registry')
    .action(installCommand);
program
    .command('uninstall <skill>')
    .description('Remove skill')
    .action(uninstallCommand);
program
    .command('server')
    .description('Start the web server')
    .option('--tunnel', 'Create a public URL for remote access via localtunnel')
    .action((options) => serverCommand(options));
program
    .command('doctor')
    .description('Run system diagnostics and health checks')
    .action(doctorCommand);
program
    .command('onboarding')
    .description('Configure company profile and preferences')
    .action(onboardingCommand);
program
    .command('virtual-office')
    .description('Open the virtual office in browser')
    .action(virtualOfficeCommand);
program
    .command('jarvis')
    .description('Open Jarvis voice assistant in browser')
    .action(jarvisCommand);
program.addCommand(schedulerCommand());
program.addCommand(mcpCommand());
program
    .command('ingest <path>')
    .description('Ingest documents into the knowledge base')
    .option('--squad <code>', 'Associate with a squad')
    .option('--tags <tags>', 'Comma-separated tags')
    .option('--watch', 'Watch for changes and auto-ingest')
    .action((targetPath, options) => ingestCommand(targetPath, options));
program
    .command('knowledge <action> [args...]')
    .description('Query and manage the knowledge base (search, stats, list, delete, rebuild)')
    .option('--mode <mode>', 'Query mode: search, graph, auto', 'auto')
    .option('--squad <code>', 'Filter by squad')
    .action((action, args, options) => knowledgeCommand(action, args, options));
program.addCommand(loginCommand);
program.addCommand(logoutCommand);
program.addCommand(whoamiCommand);
program.addCommand(publishCommand);
program.addCommand(registryInstallCommand);
program.addCommand(searchCommand);
program.addCommand(infoCommand);
program.addCommand(outdatedCommand);
program.addCommand(updateCommand);
program.parse();
