/**
 * memory-compiled-truth-history — Task 391 / Task 597.
 *
 * Returns the :CompiledTruthRevision chain for one entity (newest first,
 * capped at MAX_REVISIONS_RETURNED) and the entity's :TimelineEvent rows
 * (newest valid-time first, capped at MAX_TIMELINE_EVENTS_RETURNED).
 *
 * Task 597 adds bi-temporal distinction: each TimelineEventRow carries
 * `eventDate` (valid-time: when the fact was true) and `learnedAt`
 * (transaction-time: when the store first ingested it).
 *
 * Read-only. Account-scoped at both the entity and child nodes (defence
 * in depth — accountId mismatch on either side returns an empty list).
 */
import { int } from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
const MAX_REVISIONS_RETURNED = 20;
const MAX_TIMELINE_EVENTS_RETURNED = 50;
export async function memoryCompiledTruthHistory(params) {
    const { accountId, nodeId } = params;
    if (!accountId) {
        throw new Error("memory-compiled-truth-history: accountId is required");
    }
    if (!nodeId) {
        throw new Error("memory-compiled-truth-history: nodeId is required");
    }
    const session = getSession();
    const t0 = Date.now();
    try {
        const revRes = await session.run(`MATCH (e) WHERE elementId(e) = $nodeId AND e.accountId = $accountId
       MATCH (e)-[:HAS_REVISION]->(r:CompiledTruthRevision)
       WHERE r.accountId = $accountId
       RETURN r.revisionId AS revisionId,
              r.truth      AS truth,
              coalesce(r.twin, 'compiledTruth') AS twin,
              toString(r.writtenAt) AS writtenAt,
              r.source     AS source
       ORDER BY r.writtenAt DESC
       LIMIT $limit`, { nodeId, accountId, limit: int(MAX_REVISIONS_RETURNED) });
        const revisions = revRes.records.map((rec) => ({
            revisionId: String(rec.get("revisionId")),
            truth: String(rec.get("truth") ?? ""),
            twin: rec.get("twin"),
            writtenAt: String(rec.get("writtenAt")),
            source: rec.get("source"),
        }));
        const teRes = await session.run(`MATCH (e) WHERE elementId(e) = $nodeId AND e.accountId = $accountId
       MATCH (e)-[:HAS_TIMELINE_EVENT]->(t:TimelineEvent)
       WHERE t.accountId = $accountId
       RETURN t.timelineEventId       AS timelineEventId,
              toString(t.occurredAt)  AS eventDate,
              toString(t.learnedAt)   AS learnedAt,
              t.kind                  AS kind,
              t.summary               AS summary
       ORDER BY t.occurredAt DESC
       LIMIT $limit`, { nodeId, accountId, limit: int(MAX_TIMELINE_EVENTS_RETURNED) });
        const timelineEvents = teRes.records.map((rec) => ({
            timelineEventId: String(rec.get("timelineEventId")),
            eventDate: String(rec.get("eventDate") ?? ""),
            learnedAt: rec.get("learnedAt") != null ? String(rec.get("learnedAt")) : null,
            kind: String(rec.get("kind") ?? ""),
            summary: String(rec.get("summary") ?? ""),
        }));
        const ms = Date.now() - t0;
        process.stderr.write(`[compiled-truth-history] nodeId=${nodeId.slice(0, 12)} ms=${ms} revisions=${revisions.length} timelineEvents=${timelineEvents.length}\n`);
        return { entityNodeId: nodeId, revisions, timelineEvents };
    }
    finally {
        await session.close();
    }
}
export { MAX_REVISIONS_RETURNED, MAX_TIMELINE_EVENTS_RETURNED };
//# sourceMappingURL=memory-compiled-truth-history.js.map