/**
 * memory-review-queue — Task 327.
 *
 * Read-only operator surface for the dream-cycle's review queues.
 * Returns counts plus a small sample of `:OrphanCandidate` and
 * `:CitationProposal` rows so the operator can decide whether to triage
 * them. Without this, the dream-cycle's queue nodes are invisible — write-only
 * shape.
 *
 * Phases 5 (backlinks) and 6 (tag consistency) are not in v1, so neither
 * `:BackLinkGap` nor `:TagMergeProposal` appears here yet — when they
 * land, this tool extends to surface them.
 */
import { int } from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
const QUEUES = [
    {
        label: "OrphanCandidate",
        sampleQuery: `MATCH (o:OrphanCandidate) WHERE o.accountId = $accountId
                  RETURN o.orphanCandidateId AS id,
                         o.targetLabel AS targetLabel,
                         o.targetName AS targetName,
                         toString(o.since) AS since
                  ORDER BY o.createdAt DESC LIMIT $limit`,
        countQuery: `MATCH (o:OrphanCandidate) WHERE o.accountId = $accountId RETURN count(o) AS c`,
    },
    {
        label: "CitationProposal",
        sampleQuery: `MATCH (p:CitationProposal) WHERE p.accountId = $accountId
                  RETURN p.citationProposalId AS id,
                         p.timelineEventId AS timelineEventId,
                         toString(coalesce(p.updatedAt, p.createdAt)) AS observedAt,
                         coalesce(p.reasoning, '') AS reasoning
                  ORDER BY coalesce(p.updatedAt, p.createdAt) DESC LIMIT $limit`,
        countQuery: `MATCH (p:CitationProposal) WHERE p.accountId = $accountId RETURN count(p) AS c`,
    },
];
export async function memoryReviewQueue(params) {
    const { accountId } = params;
    const sampleSize = Math.min(Math.max(params.sampleSize ?? 5, 1), 25);
    const session = getSession();
    try {
        const queues = [];
        let totalPending = 0;
        for (const q of QUEUES) {
            const countRes = await session.run(q.countQuery, { accountId });
            const count = Number(countRes.records[0]?.get("c") ?? 0);
            totalPending += count;
            const sampleRes = await session.run(q.sampleQuery, { accountId, limit: int(sampleSize) });
            const sample = sampleRes.records.map((rec) => {
                const row = {};
                for (const key of rec.keys) {
                    const value = rec.get(key);
                    row[String(key)] = value == null ? "" : String(value);
                }
                return row;
            });
            queues.push({ label: q.label, count, sample });
        }
        process.stderr.write(`[dream-cycle:review-queue] pending=${totalPending} queues=${queues.length}\n`);
        return { queues, totalPending };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-review-queue.js.map