/**
 * Read-only projection service for audit activity.
 *
 * Reads PermissionActivityRecord rows and projects them to the bounded public
 * AuditProjectionEntry shape. Tool names, matcher keys, audit ids, raw command
 * bodies, input previews, and affected paths are intentionally NOT projected.
 */
import { AUDIT_PROJECTION_PUBLIC_SOURCE, readAuditOutcome, readAuditTitle, readAuditToolRisk, toneForAudit, } from "./auditProjectionContract.js";
const AUDIT_TITLE_BY_KIND = {
    request_created: "Permission requested",
    request_reply_failed: "Permission reply failed",
    request_answered: "Permission answered",
    session_decision_recorded: "Session decision recorded",
    session_decision_revoked: "Session decision revoked",
};
export class AuditProjectionService {
    store;
    constructor(store) {
        this.store = store;
    }
    listForWorkspace(workspaceId, limit = 50) {
        if (!workspaceId) {
            return {
                contractVersion: "v1",
                generatedAt: new Date().toISOString(),
                entries: [],
            };
        }
        const page = this.store.listPermissionActivity({
            workspaceId,
            limit,
        });
        const entries = page.events.map((event) => {
            const decision = event.decisionId
                ? this.store.findPermissionDecision(event.decisionId)
                : undefined;
            const outcome = readAuditOutcome({
                kind: event.kind,
                effect: decision?.effect ?? null,
                selectedOption: event.selectedOption ?? null,
            });
            const toolRisk = readAuditToolRisk(event.toolRisk);
            const title = readAuditTitle(AUDIT_TITLE_BY_KIND[event.kind]);
            return {
                source: AUDIT_PROJECTION_PUBLIC_SOURCE,
                id: event.id,
                kind: event.kind,
                at: event.at,
                workspaceId: event.workspaceId,
                runId: event.runId ?? null,
                toolRisk,
                outcome,
                tone: toneForAudit({ kind: event.kind, outcome }),
                title,
            };
        });
        return {
            contractVersion: "v1",
            generatedAt: new Date().toISOString(),
            entries,
        };
    }
}
