/**
 * DatabaseService class for all database operations
 *
 * Provides CRUD operations for documents, OCR results, chunks, embeddings,
 * and provenance records. Uses prepared statements for security and performance.
 */
import { createDatabase, openDatabase, listDatabases, deleteDatabase, databaseExists, } from './static-operations.js';
import { getStats, updateMetadataCounts, updateMetadataModified, getTimelineStats, getQualityTrends, getThroughputAnalytics, } from './stats-operations.js';
import * as docOps from './document-operations.js';
import * as ocrOps from './ocr-operations.js';
import * as chunkOps from './chunk-operations.js';
import * as embOps from './embedding-operations.js';
import * as provOps from './provenance-operations.js';
import * as extOps from './extraction-operations.js';
import * as ffOps from './form-fill-operations.js';
import { updateImageProvenance } from './image-operations.js';
import { reassignDocument, mergeClusters } from './cluster-operations.js';
import * as tagOps from './tag-operations.js';
/**
 * DatabaseService class for all database operations
 */
export class DatabaseService {
    db;
    name;
    path;
    constructor(db, name, path) {
        this.db = db;
        this.name = name;
        this.path = path;
    }
    static create(name, description, storagePath) {
        const result = createDatabase(name, description, storagePath);
        return new DatabaseService(result.db, result.name, result.path);
    }
    static open(name, storagePath) {
        const result = openDatabase(name, storagePath);
        return new DatabaseService(result.db, result.name, result.path);
    }
    static list(storagePath) {
        return listDatabases(storagePath);
    }
    static delete(name, storagePath) {
        deleteDatabase(name, storagePath);
    }
    static exists(name, storagePath) {
        return databaseExists(name, storagePath);
    }
    getStats() {
        return getStats(this.db, this.name, this.path);
    }
    close() {
        try {
            this.db.pragma('optimize');
        }
        catch (error) {
            console.error('[DatabaseService] pragma optimize failed:', error instanceof Error ? error.message : String(error));
        }
        this.db.close();
    }
    getName() {
        return this.name;
    }
    getPath() {
        return this.path;
    }
    transaction(fn) {
        return this.db.transaction(fn)();
    }
    getConnection() {
        return this.db;
    }
    // ==================== DOCUMENT OPERATIONS ====================
    insertDocument(doc) {
        return docOps.insertDocument(this.db, doc, () => {
            updateMetadataCounts(this.db);
        });
    }
    getDocument(id) {
        return docOps.getDocument(this.db, id);
    }
    getDocumentByPath(filePath) {
        return docOps.getDocumentByPath(this.db, filePath);
    }
    getDocumentByHash(fileHash) {
        return docOps.getDocumentByHash(this.db, fileHash);
    }
    listDocuments(options) {
        return docOps.listDocuments(this.db, options);
    }
    updateDocumentStatus(id, status, errorMessage) {
        docOps.updateDocumentStatus(this.db, id, status, errorMessage, () => {
            updateMetadataModified(this.db);
        });
    }
    updateDocumentOCRComplete(id, pageCount, ocrCompletedAt) {
        docOps.updateDocumentOCRComplete(this.db, id, pageCount, ocrCompletedAt, () => {
            updateMetadataModified(this.db);
        });
    }
    deleteDocument(id) {
        this.transaction(() => {
            docOps.deleteDocument(this.db, id, () => {
                updateMetadataCounts(this.db);
            });
        });
    }
    cleanDocumentDerivedData(id) {
        this.transaction(() => {
            docOps.cleanDocumentDerivedData(this.db, id);
        });
    }
    // ==================== OCR RESULT OPERATIONS ====================
    insertOCRResult(result) {
        return ocrOps.insertOCRResult(this.db, result, () => {
            updateMetadataCounts(this.db);
        });
    }
    getOCRResult(id) {
        return ocrOps.getOCRResult(this.db, id);
    }
    getOCRResultByDocumentId(documentId) {
        return ocrOps.getOCRResultByDocumentId(this.db, documentId);
    }
    // ==================== CHUNK OPERATIONS ====================
    insertChunk(chunk) {
        return chunkOps.insertChunk(this.db, chunk, () => {
            updateMetadataCounts(this.db);
        });
    }
    insertChunks(chunks) {
        return chunkOps.insertChunks(this.db, chunks, () => {
            updateMetadataCounts(this.db);
        }, (fn) => this.transaction(fn));
    }
    getChunk(id) {
        return chunkOps.getChunk(this.db, id);
    }
    hasChunksByDocumentId(documentId) {
        return chunkOps.hasChunksByDocumentId(this.db, documentId);
    }
    getChunksByDocumentId(documentId) {
        return chunkOps.getChunksByDocumentId(this.db, documentId);
    }
    getChunksByOCRResultId(ocrResultId) {
        return chunkOps.getChunksByOCRResultId(this.db, ocrResultId);
    }
    getPendingEmbeddingChunks(limit) {
        return chunkOps.getPendingEmbeddingChunks(this.db, limit);
    }
    updateChunkEmbeddingStatus(id, status, embeddedAt) {
        chunkOps.updateChunkEmbeddingStatus(this.db, id, status, embeddedAt, () => {
            updateMetadataModified(this.db);
        });
    }
    getChunksFiltered(documentId, filters) {
        return chunkOps.getChunksFiltered(this.db, documentId, filters);
    }
    getChunkNeighbors(documentId, chunkIndex, count) {
        return chunkOps.getChunkNeighbors(this.db, documentId, chunkIndex, count);
    }
    // ==================== EMBEDDING OPERATIONS ====================
    insertEmbedding(embedding) {
        return embOps.insertEmbedding(this.db, embedding, () => {
            updateMetadataCounts(this.db);
        });
    }
    insertEmbeddings(embeddings) {
        return embOps.insertEmbeddings(this.db, embeddings, () => {
            updateMetadataCounts(this.db);
        }, (fn) => this.transaction(fn));
    }
    getEmbedding(id) {
        return embOps.getEmbedding(this.db, id);
    }
    getEmbeddingByChunkId(chunkId) {
        return embOps.getEmbeddingByChunkId(this.db, chunkId);
    }
    getEmbeddingByExtractionId(extractionId) {
        return embOps.getEmbeddingByExtractionId(this.db, extractionId);
    }
    getEmbeddingsByDocumentId(documentId) {
        return embOps.getEmbeddingsByDocumentId(this.db, documentId);
    }
    getEmbeddingsFiltered(filters) {
        return embOps.getEmbeddingsFiltered(this.db, filters);
    }
    getEmbeddingStats(documentId) {
        return embOps.getEmbeddingStats(this.db, documentId);
    }
    deleteEmbeddingsByChunkId(chunkId) {
        return embOps.deleteEmbeddingsByChunkId(this.db, chunkId);
    }
    deleteEmbeddingsByImageId(imageId) {
        return embOps.deleteEmbeddingsByImageId(this.db, imageId);
    }
    deleteEmbeddingsByDocumentId(documentId) {
        return embOps.deleteEmbeddingsByDocumentId(this.db, documentId);
    }
    // ==================== PROVENANCE OPERATIONS ====================
    insertProvenance(record) {
        return provOps.insertProvenance(this.db, record);
    }
    getProvenance(id) {
        return provOps.getProvenance(this.db, id);
    }
    getProvenanceChain(id) {
        return provOps.getProvenanceChain(this.db, id);
    }
    getProvenanceByRootDocument(rootDocumentId) {
        return provOps.getProvenanceByRootDocument(this.db, rootDocumentId);
    }
    getProvenanceChildren(parentId) {
        return provOps.getProvenanceChildren(this.db, parentId);
    }
    queryProvenance(filters) {
        return provOps.queryProvenance(this.db, filters);
    }
    getProvenanceProcessorStats(filters) {
        return provOps.getProvenanceProcessorStats(this.db, filters);
    }
    // ==================== EXTRACTION OPERATIONS ====================
    insertExtraction(extraction) {
        return extOps.insertExtraction(this.db, extraction, () => {
            updateMetadataCounts(this.db);
        });
    }
    getExtractionsByDocument(documentId) {
        return extOps.getExtractionsByDocument(this.db, documentId);
    }
    getExtraction(id) {
        return extOps.getExtraction(this.db, id);
    }
    searchExtractions(query, filters) {
        return extOps.searchExtractions(this.db, query, filters);
    }
    // ==================== FORM FILL OPERATIONS ====================
    insertFormFill(formFill) {
        return ffOps.insertFormFill(this.db, formFill, () => {
            updateMetadataCounts(this.db);
        });
    }
    getFormFill(id) {
        return ffOps.getFormFill(this.db, id);
    }
    listFormFills(options) {
        return ffOps.listFormFills(this.db, options);
    }
    searchFormFills(query, options) {
        return ffOps.searchFormFills(this.db, query, options);
    }
    deleteFormFill(id) {
        return ffOps.deleteFormFill(this.db, id);
    }
    // ==================== DOCUMENT METADATA ====================
    updateDocumentMetadata(id, metadata) {
        docOps.updateDocumentMetadata(this.db, id, metadata, () => {
            updateMetadataModified(this.db);
        });
    }
    // ==================== TIMELINE & ANALYTICS OPERATIONS ====================
    getTimelineStats(options) {
        return getTimelineStats(this.db, options);
    }
    getQualityTrends(options) {
        return getQualityTrends(this.db, options);
    }
    getThroughputAnalytics(options) {
        return getThroughputAnalytics(this.db, options);
    }
    // ==================== CLUSTER OPERATIONS ====================
    reassignDocument(documentId, targetClusterId) {
        return reassignDocument(this.db, documentId, targetClusterId);
    }
    mergeClusters(clusterId1, clusterId2) {
        return mergeClusters(this.db, clusterId1, clusterId2);
    }
    // ==================== IMAGE OPERATIONS ====================
    updateImageProvenance(id, provenanceId) {
        updateImageProvenance(this.db, id, provenanceId);
    }
    // ==================== TAG OPERATIONS ====================
    createTag(tag) {
        return tagOps.createTag(this.db, tag);
    }
    getTagByName(name) {
        return tagOps.getTagByName(this.db, name);
    }
    getAllTags() {
        return tagOps.getAllTags(this.db);
    }
    getTagsWithCounts() {
        return tagOps.getTagsWithCounts(this.db);
    }
    applyTag(tagId, entityId, entityType) {
        return tagOps.applyTag(this.db, tagId, entityId, entityType);
    }
    removeTag(tagId, entityId, entityType) {
        return tagOps.removeTag(this.db, tagId, entityId, entityType);
    }
    getTagsForEntity(entityId, entityType) {
        return tagOps.getTagsForEntity(this.db, entityId, entityType);
    }
    searchByTags(tagNames, entityType, matchAll) {
        return tagOps.searchByTags(this.db, tagNames, entityType, matchAll);
    }
    deleteTag(tagId) {
        return tagOps.deleteTag(this.db, tagId);
    }
}
//# sourceMappingURL=service.js.map