/**
 * memory-report-read-latest — fetch the most recent :Report(s) matching a
 * keyword (Task 332). Resolves "what was last night's dream cycle" /
 * "show me the latest briefing" routes by name; default `limit=1`.
 */
import { getSession } from "../lib/neo4j.js";
const DEFAULT_LIMIT = 1;
const MAX_LIMIT = 20;
export async function memoryReportReadLatest(params) {
    const { accountId, keyword } = params;
    const limit = Math.max(1, Math.min(params.limit ?? DEFAULT_LIMIT, MAX_LIMIT));
    if (!accountId)
        throw new Error("memory-report-read-latest: accountId is required");
    if (!keyword || !keyword.trim()) {
        throw new Error("memory-report-read-latest: keyword is required");
    }
    const session = getSession();
    const t0 = Date.now();
    try {
        const res = await session.run(`MATCH (r:Report {accountId: $accountId})
       WHERE $keyword IN r.keywords
       RETURN elementId(r) AS nodeId,
              r.reportId AS reportId,
              r.title AS title,
              r.body AS body,
              r.keywords AS keywords,
              r.sourceWorkflow AS sourceWorkflow,
              toString(r.occurredAt) AS occurredAt
       ORDER BY r.occurredAt DESC
       LIMIT toInteger($limit)`, { accountId, keyword, limit });
        const results = res.records.map((rec) => ({
            nodeId: rec.get("nodeId"),
            reportId: rec.get("reportId"),
            title: rec.get("title"),
            body: rec.get("body"),
            keywords: rec.get("keywords"),
            sourceWorkflow: rec.get("sourceWorkflow"),
            occurredAt: rec.get("occurredAt"),
        }));
        const ms = Date.now() - t0;
        const firstId = results[0]?.reportId.slice(0, 8) ?? "none";
        process.stderr.write(`[reports] op=read-latest reportId=${firstId} keywords=${keyword} ms=${ms} hits=${results.length}\n`);
        return { results };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-report-read-latest.js.map