/**
 * Discovery CLI Runner — closes the customer-facing loop.
 *
 * Wires together (all already shipped):
 *   W2.1 discoverAgents             — walk the project, build manifests
 *   signed-manifest helper          — Ed25519 + canonical JSON
 *   ManifestPushEmitter             — POST with retry on transient failures
 *
 * Customer invocation pattern (final binary lives in bin/discover.ts which
 * imports runDiscoveryCli; this module stays pure / testable):
 *
 *   npx @connexum/ai-governance discover \
 *       --root ./my-project \
 *       --org-id my-org \
 *       --key /etc/connexum/org-private-key.pem \
 *       --endpoint https://proxy.my-cc.io/api/v1/manifests
 *
 * @connexum/ai-governance  W2.1 + push transport binary
 */
import { discoverAgents } from './agent-discovery.js';
import { ManifestPushEmitter } from './manifest-push-emitter.js';
/**
 * Pure runnable surface: discover -> for each -> push -> aggregate.
 */
export async function runDiscoveryCli(input) {
    if (!input.rootDir || input.rootDir.trim() === '') {
        throw new Error('runDiscoveryCli: rootDir is required');
    }
    if (!input.orgId || input.orgId.trim() === '') {
        throw new Error('runDiscoveryCli: orgId is required');
    }
    if (!input.endpoint || input.endpoint.trim() === '') {
        throw new Error('runDiscoveryCli: endpoint is required');
    }
    const manifests = discoverAgents(input.rootDir, input.discovery);
    const reports = [];
    if (input.dryRun) {
        for (const manifest of manifests) {
            reports.push({ manifest, push: null });
        }
        return {
            rootDir: input.rootDir,
            agentCount: manifests.length,
            successCount: 0,
            failureCount: 0,
            reports,
            ok: true,
        };
    }
    const emitter = new ManifestPushEmitter({
        orgId: input.orgId,
        privateKey: input.privateKey,
        endpoint: input.endpoint,
        transport: input.transport,
        sleep: input.sleep,
        maxRetries: input.maxRetries,
    });
    let successCount = 0;
    let failureCount = 0;
    for (const manifest of manifests) {
        const push = await emitter.push(manifest);
        reports.push({ manifest, push });
        if (push.ok)
            successCount += 1;
        else
            failureCount += 1;
    }
    return {
        rootDir: input.rootDir,
        agentCount: manifests.length,
        successCount,
        failureCount,
        reports,
        ok: failureCount === 0,
    };
}
/**
 * Format a CLI result as a human-readable text report (for stdout).
 * Browser-bundleable; no node-builtin imports.
 */
export function formatCliReport(result) {
    const lines = [];
    lines.push(`Connexum Discovery — ${result.rootDir}`);
    lines.push(`Found ${result.agentCount} agent${result.agentCount === 1 ? '' : 's'}. ` +
        `Pushed ${result.successCount} ok, ${result.failureCount} failed.`);
    lines.push('');
    for (const r of result.reports) {
        const m = r.manifest;
        const status = r.push === null ? '[DRY-RUN]' : r.push.ok ? '[OK]' : '[FAIL]';
        lines.push(`${status}  ${m.agentId.padEnd(40)}  tools=${m.tools.length}  ` +
            (r.push?.receiptId ? `receipt=${r.push.receiptId}` : r.push?.error ?? ''));
    }
    return lines.join('\n');
}
//# sourceMappingURL=discovery-cli.js.map