/**
 * memory-report-list — paginated listing of :Report nodes (Task 332).
 * Optional filters: keyword (any-match against r.keywords array),
 * sourceWorkflow (exact match). Returns metadata only — no body — so the
 * agent can scan the catalogue without paying for full bodies.
 */
import { getSession } from "../lib/neo4j.js";
const DEFAULT_LIMIT = 20;
const MAX_LIMIT = 100;
export async function memoryReportList(params) {
    const { accountId, keyword, sourceWorkflow } = params;
    const limit = Math.max(1, Math.min(params.limit ?? DEFAULT_LIMIT, MAX_LIMIT));
    const offset = Math.max(0, params.offset ?? 0);
    if (!accountId)
        throw new Error("memory-report-list: accountId is required");
    const session = getSession();
    const t0 = Date.now();
    try {
        const filterClauses = [];
        if (keyword)
            filterClauses.push("$keyword IN r.keywords");
        if (sourceWorkflow)
            filterClauses.push("r.sourceWorkflow = $sourceWorkflow");
        const whereExtra = filterClauses.length > 0 ? `AND ${filterClauses.join(" AND ")}` : "";
        const res = await session.run(`MATCH (r:Report {accountId: $accountId})
       WHERE 1=1 ${whereExtra}
       WITH r ORDER BY r.occurredAt DESC
       WITH collect(r) AS rows, count(r) AS total
       UNWIND rows[toInteger($offset)..toInteger($offset) + toInteger($limit)] AS r
       RETURN elementId(r) AS nodeId,
              r.reportId AS reportId,
              r.title AS title,
              r.keywords AS keywords,
              r.sourceWorkflow AS sourceWorkflow,
              toString(r.occurredAt) AS occurredAt,
              size(r.body) AS bodyChars,
              total`, {
            accountId,
            keyword: keyword ?? null,
            sourceWorkflow: sourceWorkflow ?? null,
            limit,
            offset,
        });
        const results = res.records.map((rec) => ({
            nodeId: rec.get("nodeId"),
            reportId: rec.get("reportId"),
            title: rec.get("title"),
            keywords: rec.get("keywords"),
            sourceWorkflow: rec.get("sourceWorkflow"),
            occurredAt: rec.get("occurredAt"),
            bodyChars: Number(rec.get("bodyChars")),
        }));
        const total = res.records.length > 0 ? Number(res.records[0].get("total")) : 0;
        const ms = Date.now() - t0;
        const firstId = results[0]?.reportId.slice(0, 8) ?? "none";
        const kwLog = keyword ?? "*";
        process.stderr.write(`[reports] op=list reportId=${firstId} keywords=${kwLog} ms=${ms} hits=${results.length} total=${total}\n`);
        return { results, total };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-report-list.js.map