/**
 * Comparison operations for DatabaseService
 *
 * Handles CRUD operations for the comparisons table.
 */
import { runWithForeignKeyCheck } from './helpers.js';
/**
 * Insert a comparison record
 */
export function insertComparison(db, comparison) {
    const stmt = db.prepare(`
    INSERT INTO comparisons (id, document_id_1, document_id_2, similarity_ratio,
      text_diff_json, structural_diff_json, summary,
      content_hash, provenance_id, created_at, processing_duration_ms)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  `);
    runWithForeignKeyCheck(stmt, [
        comparison.id,
        comparison.document_id_1,
        comparison.document_id_2,
        comparison.similarity_ratio,
        comparison.text_diff_json,
        comparison.structural_diff_json,
        comparison.summary,
        comparison.content_hash,
        comparison.provenance_id,
        comparison.created_at,
        comparison.processing_duration_ms,
    ], `inserting comparison: FK violation for document_id_1="${comparison.document_id_1}" or document_id_2="${comparison.document_id_2}"`);
    return comparison.id;
}
/**
 * Get a comparison by ID
 */
export function getComparison(db, id) {
    const row = db.prepare('SELECT * FROM comparisons WHERE id = ?').get(id);
    return row ?? null;
}
/**
 * List comparisons with optional document filter and pagination
 */
export function listComparisons(db, options) {
    const params = [];
    let where = '';
    if (options?.document_id) {
        where = 'WHERE document_id_1 = ? OR document_id_2 = ?';
        params.push(options.document_id, options.document_id);
    }
    params.push(options?.limit ?? 50, options?.offset ?? 0);
    return db
        .prepare(`SELECT * FROM comparisons ${where} ORDER BY created_at DESC LIMIT ? OFFSET ?`)
        .all(...params);
}
/**
 * Get comparison summaries for a document (lightweight: no JSON blobs)
 */
export function getComparisonSummariesByDocument(db, documentId) {
    return db
        .prepare(`SELECT id, document_id_1, document_id_2, similarity_ratio, summary, created_at, processing_duration_ms
     FROM comparisons
     WHERE document_id_1 = ? OR document_id_2 = ?
     ORDER BY created_at DESC`)
        .all(documentId, documentId);
}
//# sourceMappingURL=comparison-operations.js.map