/**
 * conversation-archive-list-chunks MCP tool.
 *
 * Page reader over the :Section chunks of one named :ConversationArchive
 * (keyed on conversationIdentity — Task 397). Returns each chunk's
 * elementId, ordered index, summary, and body so the conversation-archive-enrich
 * skill can drive the dispatched specialist to emit per-chunk claims in-turn
 * (Task 433). The skill then hands those claims back to
 * `conversation-archive-derive-insights` for per-kind cypher emission.
 *
 * Read-only. No proposal building, no rejection sidecar — both belong on the
 * derive-insights tool.
 */
import { getSession } from "../lib/neo4j.js";
const DEFAULT_CHUNK_LIMIT = 5;
const MAX_CHUNK_LIMIT = 20;
async function loadArchiveMeta(archiveElementId, accountId) {
    const session = getSession();
    try {
        const result = await session.run(`MATCH (a:ConversationArchive { accountId: $accountId })
       WHERE elementId(a) = $eid AND a.conversationIdentity IS NOT NULL
       OPTIONAL MATCH (a)-[:HAS_SECTION]->(c:Section)
       RETURN a.title AS title, count(c) AS totalChunks`, { eid: archiveElementId, accountId });
        if (result.records.length === 0) {
            return "not-found";
        }
        const rec = result.records[0];
        const totalRaw = rec.get("totalChunks");
        const totalChunks = typeof totalRaw === "number"
            ? totalRaw
            : typeof totalRaw?.toNumber === "function"
                ? totalRaw.toNumber()
                : Number(totalRaw ?? 0);
        if (totalChunks === 0) {
            return "not-found";
        }
        return {
            title: rec.get("title") ?? null,
            totalChunks,
        };
    }
    finally {
        await session.close();
    }
}
async function loadChunkPage(archiveElementId, accountId, offset, limit) {
    const session = getSession();
    try {
        const result = await session.run(`MATCH (a:ConversationArchive { accountId: $accountId })-[:HAS_SECTION]->(c:Section)
       WHERE elementId(a) = $eid AND a.conversationIdentity IS NOT NULL
       RETURN elementId(c) AS elementId,
              coalesce(c.body, '') AS body,
              coalesce(c.summary, '') AS summary
       ORDER BY coalesce(c.firstMessageAt, c.createdAt) ASC, elementId(c) ASC
       SKIP toInteger($offset) LIMIT toInteger($limit)`, {
            eid: archiveElementId,
            accountId,
            offset,
            limit,
        });
        return result.records.map((rec, i) => ({
            elementId: rec.get("elementId"),
            body: rec.get("body"),
            summary: rec.get("summary"),
            index: offset + i,
        }));
    }
    finally {
        await session.close();
    }
}
export async function conversationArchiveListChunks(params) {
    const { accountId, archiveElementId, chunkOffset = 0, chunkLimit = DEFAULT_CHUNK_LIMIT, } = params;
    const limit = Math.min(Math.max(1, chunkLimit), MAX_CHUNK_LIMIT);
    const offset = Math.max(0, chunkOffset);
    const archive = await loadArchiveMeta(archiveElementId, accountId);
    if (archive === "not-found") {
        throw new Error(`[conversation-archive-list-chunks] FAIL phase=archive-not-found reason="no conversation-document matched accountId=${accountId} elementId=${archiveElementId}, or it lacks conversationIdentity or has zero :Section chunks"`);
    }
    const start = Date.now();
    process.stderr.write(`[conversation-archive-list-chunks] start accountId=${accountId} archiveElementId=${archiveElementId} offset=${offset} limit=${limit} totalChunks=${archive.totalChunks}\n`);
    const chunks = await loadChunkPage(archiveElementId, accountId, offset, limit);
    const walkedTo = offset + chunks.length;
    const chunksRemaining = walkedTo < archive.totalChunks;
    process.stderr.write(`[conversation-archive-list-chunks] done accountId=${accountId} archiveElementId=${archiveElementId} walked=${chunks.length} totalChunks=${archive.totalChunks} ms=${Date.now() - start}\n`);
    return {
        archiveElementId,
        archiveTitle: archive.title,
        totalChunks: archive.totalChunks,
        walkedFrom: offset,
        walkedTo,
        chunksRemaining,
        chunks,
    };
}
//# sourceMappingURL=conversation-archive-list-chunks.js.map