// engine/audit-trail/attestation.ts — Generate attestation document for a spec (SPEC-167)
// Produces structured JSON (no runtime PDF deps required).
import { randomUUID } from 'node:crypto';
import { queryAuditLog } from './query.js';
import { verifyChainIntegrity } from './verifier.js';
/**
 * Build an event-type frequency map from an array of events.
 */
function buildEventTypeSummary(events) {
    const counts = {};
    for (const ev of events) {
        counts[ev.eventType] = (counts[ev.eventType] ?? 0) + 1;
    }
    return counts;
}
/**
 * Generate an attestation document for all events of a given spec.
 * Verifies chain integrity and embeds the result in the attestation.
 *
 * @param filePath  Absolute path to the project's audit-log.jsonl.
 * @param projectId Project identifier.
 * @param specId    Spec identifier to filter events for.
 */
export async function generateAttestation(filePath, projectId, specId) {
    const [queryResult, chainResult] = await Promise.all([
        queryAuditLog(filePath, { projectId, specId, limit: 1000 }),
        verifyChainIntegrity(filePath),
    ]);
    const events = queryResult.events;
    const timestamps = events.map((e) => e.timestamp).sort();
    const summary = {
        totalEvents: events.length,
        firstEvent: timestamps[0],
        lastEvent: timestamps[timestamps.length - 1],
        eventTypes: buildEventTypeSummary(events),
    };
    return {
        attestationId: randomUUID(),
        generatedAt: new Date().toISOString(),
        projectId,
        specId,
        events,
        chainValid: chainResult.valid,
        summary,
    };
}
//# sourceMappingURL=attestation.js.map