// Prepared-statement helpers for the `chunks` table (Epic #189, Issue #195). Every helper
// wraps a single statement so the runner can compose them inside a transaction in
// `chunker-runner.ts`. The runner is the only module that issues transactions for chunks —
// mirrors the `discovery/persist.ts` boundary convention so a per-document failure rolls
// back exactly the rows from that document.
import { CHUNKING_STRATEGY_VERSION } from "./types.js";
const INSERT_CHUNK_SQL = [
    "INSERT INTO chunks (",
    "  id, capsule_id, source_id, document_id, parsed_unit_id,",
    "  order_index, token_count, safe_excerpt_hash, chunking_strategy_version",
    ") VALUES (",
    "  :id, :capsule_id, :source_id, :document_id, :parsed_unit_id,",
    "  :order_index, :token_count, :safe_excerpt_hash, :chunking_strategy_version",
    ")",
].join(" ");
const DELETE_CHUNKS_FOR_DOCUMENT_SQL = "DELETE FROM chunks WHERE capsule_id = :c AND document_id = :d";
const COUNT_CHUNKS_FOR_DOCUMENT_SQL = "SELECT COUNT(*) AS n FROM chunks WHERE capsule_id = :c AND document_id = :d";
const COUNT_STALE_CHUNKS_FOR_DOCUMENT_SQL = [
    "SELECT COUNT(*) AS n FROM chunks",
    "WHERE capsule_id = :c AND document_id = :d",
    "  AND (chunking_strategy_version IS NULL OR chunking_strategy_version <> :v)",
].join(" ");
const SELECT_PARSED_UNITS_FOR_DOCUMENT_SQL = [
    "SELECT id, kind, page_number, page_label, section_path_json,",
    "  json_pointer, table_name, row_index, heading_path_json,",
    "  unsupported_reason, character_start, character_end",
    "FROM parsed_units",
    "WHERE capsule_id = :c AND document_id = :d",
    "ORDER BY rowid ASC",
].join(" ");
export function insertChunkRow(db, row) {
    db.prepare(INSERT_CHUNK_SQL).run({
        id: String(row.id),
        capsule_id: String(row.capsuleId),
        source_id: String(row.sourceId),
        document_id: String(row.documentId),
        parsed_unit_id: row.parsedUnitId,
        order_index: row.orderIndex,
        token_count: row.tokenCount,
        safe_excerpt_hash: row.safeExcerptHash,
        chunking_strategy_version: row.chunkingStrategyVersion,
    });
}
export function deleteChunksForDocument(db, capsuleId, documentId) {
    db.prepare(DELETE_CHUNKS_FOR_DOCUMENT_SQL).run({ c: capsuleId, d: documentId });
}
export function countChunksForDocument(db, capsuleId, documentId) {
    const row = db.prepare(COUNT_CHUNKS_FOR_DOCUMENT_SQL).get({ c: capsuleId, d: documentId });
    return typeof row?.n === "number" ? row.n : 0;
}
export function hasStaleChunksForDocument(db, capsuleId, documentId) {
    const row = db.prepare(COUNT_STALE_CHUNKS_FOR_DOCUMENT_SQL).get({
        c: capsuleId,
        d: documentId,
        v: CHUNKING_STRATEGY_VERSION,
    });
    return (row?.n ?? 0) > 0;
}
export function selectDocumentSourceId(db, capsuleId, documentId) {
    const row = db
        .prepare("SELECT source_id FROM documents WHERE capsule_id = :c AND id = :d")
        .get({ c: capsuleId, d: documentId });
    return row === undefined ? undefined : row.source_id;
}
export function selectParsedUnitsForDocument(db, capsuleId, documentId) {
    const rows = db
        .prepare(SELECT_PARSED_UNITS_FOR_DOCUMENT_SQL)
        .all({ c: capsuleId, d: documentId });
    return rows;
}
