"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentMetricsCommand = exports.AgentStatusCommand = exports.AgentRunCommand = exports.AgentListCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const registry_1 = require("../agents/registry");
exports.AgentListCommand = new commander_1.Command("agent:list")
    .description("List all registered agents")
    .option("--capability <capability>", "filter by capability")
    .option("--scope <scope>", "filter by scope")
    .option("--json", "output as JSON")
    .action(async (options) => {
    const registry = (0, registry_1.getDefaultRegistry)();
    await registry.load();
    let definitions = registry.listDefinitions();
    if (options.capability) {
        definitions = registry.findByCapability(options.capability);
    }
    if (options.scope) {
        definitions = registry.findByScope(options.scope);
    }
    if (options.json) {
        console.log(JSON.stringify(definitions, null, 2));
        return;
    }
    if (definitions.length === 0) {
        console.log(chalk_1.default.dim("\n  No agents registered\n"));
        return;
    }
    console.log(chalk_1.default.bold(`\n🤖 Registered Agents (${definitions.length})`));
    console.log(chalk_1.default.dim("─".repeat(60)));
    for (const def of definitions) {
        console.log(`  ${chalk_1.default.cyan("▸")} ${chalk_1.default.bold(def.name)}`);
        console.log(`     ${chalk_1.default.dim(def.description)}`);
        console.log(`     ${chalk_1.default.dim("Capabilities:")} ${def.capabilities.join(", ")}`);
        console.log(`     ${chalk_1.default.dim("Scopes:")} ${def.scopes.join(", ")}`);
        if (def.triggers && def.triggers.length > 0) {
            console.log(`     ${chalk_1.default.dim("Triggers:")} ${def.triggers.join(", ")}`);
        }
    }
    console.log();
});
exports.AgentRunCommand = new commander_1.Command("agent:run")
    .description("Run a specific agent by name")
    .argument("<agent-name>", "name of the agent to run")
    .argument("[input]", "optional JSON input for the agent")
    .action(async (agentName, input) => {
    const registry = (0, registry_1.getDefaultRegistry)();
    await registry.load();
    const agent = registry.get(agentName);
    if (!agent) {
        console.error(chalk_1.default.red(`Agent "${agentName}" not found`));
        console.error(chalk_1.default.dim(`Available agents: ${registry.list().map((a) => a.name).join(", ")}`));
        process.exit(1);
    }
    let parsedInput = {};
    if (input) {
        try {
            parsedInput = JSON.parse(input);
        }
        catch {
            parsedInput = { prompt: input };
        }
    }
    console.log(chalk_1.default.cyan(`\n  Running agent: ${agentName}...`));
    try {
        const result = await agent.run(parsedInput);
        registry.recordRun(agentName, true);
        await registry.save();
        console.log(chalk_1.default.green(`  ✓ Agent completed successfully`));
        console.log();
        console.log(JSON.stringify(result, null, 2));
        console.log();
    }
    catch (error) {
        registry.recordRun(agentName, false);
        await registry.save();
        console.error(chalk_1.default.red(`  ✗ Agent failed: ${error.message}`));
        process.exit(1);
    }
});
exports.AgentStatusCommand = new commander_1.Command("agent:status")
    .description("Show status of all agents or a specific agent")
    .argument("[agent-name]", "agent name (shows all if omitted)")
    .option("--json", "output as JSON")
    .action(async (agentName, options) => {
    const registry = (0, registry_1.getDefaultRegistry)();
    await registry.load();
    if (agentName) {
        const entry = registry.getEntry(agentName);
        if (!entry) {
            console.error(chalk_1.default.red(`Agent "${agentName}" not found`));
            process.exit(1);
        }
        if (options.json) {
            console.log(JSON.stringify(entry, null, 2));
            return;
        }
        console.log(chalk_1.default.bold(`\n🤖 Agent: ${agentName}`));
        console.log(chalk_1.default.dim("─".repeat(40)));
        console.log(`  ${chalk_1.default.dim("Description:")} ${entry.definition.description}`);
        console.log(`  ${chalk_1.default.dim("Runs:")}       ${entry.runCount}`);
        console.log(`  ${chalk_1.default.dim("Errors:")}     ${entry.errorCount}`);
        console.log(`  ${chalk_1.default.dim("Successes:")}  ${entry.successCount}`);
        console.log(`  ${chalk_1.default.dim("Last run:")}   ${entry.lastRun || "never"}`);
        console.log();
        return;
    }
    const report = registry.getMetricsReport();
    if (options.json) {
        console.log(JSON.stringify(report, null, 2));
        return;
    }
    console.log(chalk_1.default.bold(`\n📊 Agent Status`));
    console.log(chalk_1.default.dim("─".repeat(50)));
    console.log(`  ${chalk_1.default.dim("Registered:")} ${report.totalAgents}`);
    console.log(`  ${chalk_1.default.dim("Total runs:")} ${report.totalRuns}`);
    console.log(`  ${chalk_1.default.dim("Successes:")} ${chalk_1.default.green(report.totalSuccesses)}`);
    console.log(`  ${chalk_1.default.dim("Errors:")}    ${report.totalErrors > 0 ? chalk_1.default.red(report.totalErrors) : chalk_1.default.dim("0")}`);
    console.log(chalk_1.default.dim("\n  Per-agent:"));
    for (const a of report.agents) {
        const health = a.errorCount > 0 ? chalk_1.default.red(`✗`) : chalk_1.default.green("✓");
        console.log(`    ${health} ${chalk_1.default.cyan(a.name.padEnd(20))} ${a.runCount} runs · ${a.errorCount} errors`);
    }
    console.log();
});
exports.AgentMetricsCommand = new commander_1.Command("agent:metrics")
    .description("Show aggregate agent metrics dashboard")
    .option("--json", "output as JSON")
    .action(async (options) => {
    const registry = (0, registry_1.getDefaultRegistry)();
    await registry.load();
    const report = registry.getMetricsReport();
    if (options.json) {
        console.log(JSON.stringify(report, null, 2));
        return;
    }
    console.log(chalk_1.default.bold(`\n📈 Agent Metrics Dashboard`));
    console.log(chalk_1.default.dim("─".repeat(50)));
    console.log(`  ${chalk_1.default.dim("Total agents:")}  ${chalk_1.default.cyan(report.totalAgents)}`);
    console.log(`  ${chalk_1.default.dim("Total runs:")}    ${chalk_1.default.cyan(report.totalRuns)}`);
    console.log(`  ${chalk_1.default.dim("Errors:")}        ${report.totalErrors > 0 ? chalk_1.default.red(String(report.totalErrors)) : chalk_1.default.green("0")}`);
    console.log(`  ${chalk_1.default.dim("Error rate:")}    ${report.totalRuns > 0 ? chalk_1.default.yellow(`${Math.round((report.totalErrors / report.totalRuns) * 100)}%`) : chalk_1.default.green("0%")}`);
    if (Object.keys(report.byCapability).length > 0) {
        console.log(chalk_1.default.dim(`\n  By capability:`));
        for (const [cap, count] of Object.entries(report.byCapability)) {
            console.log(`    · ${cap}: ${count} agent(s)`);
        }
    }
    if (Object.keys(report.byScope).length > 0) {
        console.log(chalk_1.default.dim(`\n  By scope:`));
        for (const [scope, count] of Object.entries(report.byScope)) {
            console.log(`    · ${scope}: ${count} agent(s)`);
        }
    }
    console.log();
});
//# sourceMappingURL=agent-commands.js.map