/**
 * Delete all children (Section, Chunk) and REFERENCES edges of a
 * KnowledgeDocument identified by attachmentId. The document node
 * itself is NOT deleted — caller decides whether to keep or remove it.
 *
 * Returns counts of deleted nodes/edges for observability.
 */
export async function deleteDocumentChildren(attachmentId, session) {
    // Delete Sections and their Chunks
    const cleanupResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })-[:HAS_SECTION]->(s:Section)
     OPTIONAL MATCH (s)-[:HAS_CHUNK]->(c:Chunk)
     DETACH DELETE s, c
     RETURN count(DISTINCT s) AS sections, count(DISTINCT c) AS chunks`, { attachmentId });
    const sections = cleanupResult.records[0]?.get("sections")?.toNumber?.() ?? 0;
    const chunks = cleanupResult.records[0]?.get("chunks")?.toNumber?.() ?? 0;
    // Delete REFERENCES edges
    const refsResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })-[r:REFERENCES]->()
     DELETE r
     RETURN count(r) AS refs`, { attachmentId });
    const references = refsResult.records[0]?.get("refs")?.toNumber?.() ?? 0;
    return { sections, chunks, references };
}
/**
 * Set deletedAt on a KnowledgeDocument and all its children (Section, Chunk).
 * Used for soft delete — nodes remain in the graph but are excluded from search.
 *
 * Returns counts of marked nodes for observability.
 */
export async function softDeleteDocument(attachmentId, deletedAt, session) {
    // Mark the document itself
    const docResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })
     WHERE d.deletedAt IS NULL
     SET d.deletedAt = $deletedAt
     RETURN count(d) AS docs`, { attachmentId, deletedAt });
    const document = docResult.records[0]?.get("docs")?.toNumber?.() ?? 0;
    if (document === 0) {
        // Already soft-deleted or doesn't exist
        return { document: 0, sections: 0, chunks: 0 };
    }
    // Mark all children — filter nulls from OPTIONAL MATCH before UNWIND
    const childResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })-[:HAS_SECTION]->(s:Section)
     OPTIONAL MATCH (s)-[:HAS_CHUNK]->(c:Chunk)
     SET s.deletedAt = $deletedAt
     WITH s, [x IN collect(c) WHERE x IS NOT NULL] AS chunks
     UNWIND chunks AS c
     SET c.deletedAt = $deletedAt
     RETURN count(DISTINCT s) AS sections, count(DISTINCT c) AS chunkCount`, { attachmentId, deletedAt });
    const sections = childResult.records[0]?.get("sections")?.toNumber?.() ?? 0;
    const chunks = childResult.records[0]?.get("chunkCount")?.toNumber?.() ?? 0;
    return { document, sections, chunks };
}
/**
 * Hard-delete a KnowledgeDocument node itself (after children have been removed
 * via deleteDocumentChildren).
 */
export async function deleteDocumentNode(attachmentId, session) {
    const result = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })
     DETACH DELETE d
     RETURN count(d) AS deleted`, { attachmentId });
    return (result.records[0]?.get("deleted")?.toNumber?.() ?? 0) > 0;
}
//# sourceMappingURL=document-hierarchy.js.map