// Connector-activity audit ledger for the Figma connector (Epic #750, Issue #760).
//
// Reuses the SAME Evidence audit seam the QI inline-edit audit uses (`appendEditAudit` in
// ../reviewStore.ts → `createNodeContainedJsonArtifactStore` in keiko-evidence): an append-only,
// contained, atomically-written JSON artifact under `<evidenceDir>/qi/`. This module does NOT build
// a parallel store — it records connector actions through that one seam under a distinct suffix.
//
// GOVERNANCE (load-bearing, #760): an audit entry carries ONLY
//   - the action (connect | snapshot | resnapshot | revoke),
//   - the opaque, non-reversible `scopeRef` (never the file key / node id / board link),
//   - the outcome (ok | error) and, on error, a coded `errorCode`,
//   - small INTEGER counts (screens, renders, skipped, design tokens, optional nav transitions),
//   - a wall-clock `at`.
// It carries NO token, NO PII, NO board id / link / name, and NO design content (no screen names,
// no text). Counts are the only board-derived data and they are pure cardinalities.
import { createNodeContainedJsonArtifactStore, } from "@oscharko-dev/keiko-evidence";
export const FIGMA_AUDIT_SCHEMA_VERSION = 1;
const FIGMA_AUDIT_SUFFIX = ".figma-audit.json";
const parseArtifact = (value) => {
    if (typeof value !== "object" || value === null)
        return undefined;
    const record = value;
    if (record.figmaAuditSchemaVersion !== FIGMA_AUDIT_SCHEMA_VERSION)
        return undefined;
    if (typeof record.scopeRef !== "string" || !Array.isArray(record.auditLog))
        return undefined;
    return value;
};
const storeFor = (evidenceDir) => createNodeContainedJsonArtifactStore(evidenceDir, FIGMA_AUDIT_SUFFIX, { parse: parseArtifact });
const emptyArtifact = (scopeRef, now) => ({
    figmaAuditSchemaVersion: FIGMA_AUDIT_SCHEMA_VERSION,
    scopeRef,
    auditLog: [],
    lastUpdatedAt: now,
});
export const loadFigmaConnectorAudit = (scopeRef, evidenceDir) => storeFor(evidenceDir).load(scopeRef);
// Builds the entry with only the governance-permitted fields present. `errorCode` is attached only
// for an error outcome; `counts` only when supplied. No other field is ever spread in, so customer
// content cannot reach the entry even if the caller passed extra data.
const buildEntry = (input) => ({
    at: input.now,
    action: input.action,
    outcome: input.outcome,
    ...(input.outcome === "error" && input.errorCode !== undefined
        ? { errorCode: input.errorCode }
        : {}),
    ...(input.counts !== undefined ? { counts: input.counts } : {}),
});
/**
 * Append an append-only connector-action audit entry through the reused Evidence audit seam.
 * Creates the artifact on first use. Returns the updated artifact. The caller authorises the action.
 */
export const appendFigmaConnectorAudit = (input) => {
    const current = loadFigmaConnectorAudit(input.scopeRef, input.evidenceDir) ??
        emptyArtifact(input.scopeRef, input.now);
    const next = {
        ...current,
        auditLog: [...current.auditLog, buildEntry(input)],
        lastUpdatedAt: input.now,
    };
    storeFor(input.evidenceDir).record(input.scopeRef, next);
    return next;
};
