/**
 * olam setup metrics — dogfood metrics subcommand (olam-setup-epic Phase F, F2).
 *
 * Reads the trust-audit-log and computes aggregate setup statistics:
 *   medianMinutes, p95Minutes, completedCount, abortedCount, ratio
 *
 * Usage:
 *   olam setup metrics --since <date>
 *
 * --since accepts ISO 8601 (2026-05-18 or 2026-05-18T00:00:00Z).
 * Malformed dates are rejected with a clear error message (exit 1).
 *
 * The command outputs JSON to stdout so it is pipeline-friendly.
 *
 * Design constraint: this file lives in @pleri/olam-cli and delegates to
 * the pure @olam/core helper (queryDogfoodMetrics). @olam/core does NOT
 * import from @pleri/olam-cli (dep-cycle rule).
 */
import { queryDogfoodMetrics } from '@olam/core/src/setup/dogfood-metrics.js';
// ── Date parsing ──────────────────────────────────────────────────────────────
/**
 * Parse an ISO 8601 date string (date-only or datetime).
 * Returns a Date object or throws with a descriptive message on failure.
 */
export function parseSinceDate(raw) {
    // Accept date-only (YYYY-MM-DD) by normalising to midnight UTC.
    const normalised = /^\d{4}-\d{2}-\d{2}$/.test(raw.trim())
        ? `${raw.trim()}T00:00:00Z`
        : raw.trim();
    const d = new Date(normalised);
    if (Number.isNaN(d.getTime())) {
        throw new Error(`--since: invalid date "${raw}". Use ISO 8601 format, e.g. 2026-05-18 or 2026-05-18T00:00:00Z`);
    }
    return d;
}
// ── Command registration ──────────────────────────────────────────────────────
export function registerSetupMetrics(program) {
    program
        .command('setup-metrics')
        .description('Query trust-audit-log for olam setup dogfood statistics. ' +
        'Returns { medianMinutes, p95Minutes, completedCount, abortedCount, ratio }.')
        .requiredOption('--since <date>', 'ISO 8601 lower bound (e.g. 2026-05-18)')
        .action((opts) => {
        let since;
        try {
            since = parseSinceDate(opts.since);
        }
        catch (err) {
            process.stderr.write(`error: ${err instanceof Error ? err.message : String(err)}\n`);
            process.exitCode = 1;
            return;
        }
        const result = queryDogfoodMetrics({ since });
        process.stdout.write(JSON.stringify(result, null, 2) + '\n');
    });
}
//# sourceMappingURL=setup-metrics.js.map