/*
 * Libre WebUI
 * Copyright (C) 2025 Kroonen AI, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { v4 as uuidv4 } from 'uuid';
import bcrypt from 'bcrypt';
import getDatabase, { isDatabaseInitialized } from './db.js';
import { encryptionService } from './services/encryptionService.js';
// Get __dirname equivalent for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class StorageService {
    constructor() {
        this.useSQLite = false;
        this.sessionsFile = path.join(__dirname, '..', 'sessions.json');
        this.preferencesFile = path.join(__dirname, '..', 'preferences.json');
        this.documentsFile = path.join(__dirname, '..', 'documents.json');
        this.documentChunksFile = path.join(__dirname, '..', 'document-chunks.json');
        // Check if SQLite should be used
        this.useSQLite = isDatabaseInitialized();
        console.log(`Storage mode: ${this.useSQLite ? 'SQLite' : 'JSON'}`);
    }
    // =================================
    // USER MANAGEMENT
    // =================================
    async createUser(username, email, password, role = 'user') {
        const userId = uuidv4();
        const passwordHash = await bcrypt.hash(password, 12);
        const now = Date.now();
        const user = {
            id: userId,
            username,
            email,
            password_hash: passwordHash,
            role,
            created_at: now,
            updated_at: now,
        };
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare(`
        INSERT INTO users (id, username, email, password_hash, role, created_at, updated_at)
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `);
            // Encrypt sensitive user data
            const encryptedEmail = user.email
                ? encryptionService.encrypt(user.email)
                : null;
            stmt.run(user.id, user.username, encryptedEmail, user.password_hash, user.role, user.created_at, user.updated_at);
        }
        return user;
    }
    getUser(userId) {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('SELECT * FROM users WHERE id = ?');
            const user = stmt.get(userId);
            if (user && user.email) {
                // Decrypt email
                user.email = encryptionService.decrypt(user.email);
            }
            return user;
        }
        return undefined;
    }
    getUserByUsername(username) {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('SELECT * FROM users WHERE username = ?');
            const user = stmt.get(username);
            if (user && user.email) {
                // Decrypt email
                user.email = encryptionService.decrypt(user.email);
            }
            return user;
        }
        return undefined;
    }
    // =================================
    // SESSION MANAGEMENT
    // =================================
    getAllSessions(userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            // Get sessions
            const sessionsStmt = db.prepare(`
        SELECT * FROM sessions WHERE user_id = ? ORDER BY updated_at DESC
      `);
            const sessions = sessionsStmt.all(userId);
            // Get ALL messages for each session (including branches for side-by-side display)
            const messagesStmt = db.prepare(`
        SELECT * FROM session_messages
        WHERE session_id = ?
        ORDER BY message_index ASC, branch_index ASC
      `);
            // Get sibling counts for branching (count all variants for each parent)
            const siblingCountStmt = db.prepare(`
        SELECT parent_id, COUNT(*) as count FROM session_messages
        WHERE session_id = ? AND parent_id IS NOT NULL
        GROUP BY parent_id
      `);
            return sessions.map(session => {
                const messages = messagesStmt.all(session.id);
                const siblingCounts = siblingCountStmt.all(session.id);
                // Create a map for quick lookup of sibling counts
                const siblingCountMap = new Map();
                for (const sc of siblingCounts) {
                    // Add 1 to include the original message in the count
                    siblingCountMap.set(sc.parent_id, sc.count + 1);
                }
                // Decrypt session data
                const decryptedTitle = encryptionService.decrypt(session.title);
                return {
                    id: session.id,
                    title: decryptedTitle,
                    model: session.model,
                    personaId: session.persona_id || undefined,
                    createdAt: session.created_at,
                    updatedAt: session.updated_at,
                    messages: messages.map(msg => {
                        // Decrypt message data
                        const decryptedContent = encryptionService.decrypt(msg.content);
                        const decryptedImages = msg.images
                            ? JSON.parse(encryptionService.decrypt(msg.images))
                            : undefined;
                        const decryptedStatistics = msg.statistics
                            ? JSON.parse(encryptionService.decrypt(msg.statistics))
                            : undefined;
                        const decryptedArtifacts = msg.artifacts
                            ? JSON.parse(encryptionService.decrypt(msg.artifacts))
                            : undefined;
                        // Calculate sibling count: if this message has variants, count them
                        // A message has siblings if it's a parent (has variants) or is a variant itself
                        const parentId = msg.parent_id || msg.id;
                        const siblingCount = siblingCountMap.get(parentId) || 1;
                        return {
                            id: msg.id,
                            role: msg.role,
                            content: decryptedContent,
                            timestamp: msg.timestamp,
                            model: msg.model,
                            images: decryptedImages,
                            statistics: decryptedStatistics,
                            artifacts: decryptedArtifacts,
                            parentId: msg.parent_id,
                            branchIndex: msg.branch_index ?? 0,
                            isActive: msg.is_active !== 0,
                            siblingCount: siblingCount > 1 ? siblingCount : undefined,
                        };
                    }),
                };
            });
        }
        else {
            // Fallback to JSON
            try {
                if (fs.existsSync(this.sessionsFile)) {
                    const data = fs.readFileSync(this.sessionsFile, 'utf8');
                    return JSON.parse(data);
                }
            }
            catch (error) {
                console.error('Failed to load sessions from JSON:', error);
            }
        }
        return [];
    }
    getSession(sessionId, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            // Get session
            const sessionStmt = db.prepare(`
        SELECT * FROM sessions WHERE id = ? AND user_id = ?
      `);
            const session = sessionStmt.get(sessionId, userId);
            if (!session)
                return undefined;
            // Get ALL messages (including branches for side-by-side display)
            const messagesStmt = db.prepare(`
        SELECT * FROM session_messages
        WHERE session_id = ?
        ORDER BY message_index ASC, branch_index ASC
      `);
            const messages = messagesStmt.all(sessionId);
            // Get sibling counts for branching
            const siblingCountStmt = db.prepare(`
        SELECT parent_id, COUNT(*) as count FROM session_messages
        WHERE session_id = ? AND parent_id IS NOT NULL
        GROUP BY parent_id
      `);
            const siblingCounts = siblingCountStmt.all(sessionId);
            // Create a map for quick lookup of sibling counts
            const siblingCountMap = new Map();
            for (const sc of siblingCounts) {
                siblingCountMap.set(sc.parent_id, sc.count + 1);
            }
            // Decrypt session data
            const decryptedTitle = encryptionService.decrypt(session.title);
            return {
                id: session.id,
                title: decryptedTitle,
                model: session.model,
                createdAt: session.created_at,
                updatedAt: session.updated_at,
                messages: messages.map(msg => {
                    // Decrypt message data
                    const decryptedContent = encryptionService.decrypt(msg.content);
                    const decryptedImages = msg.images
                        ? JSON.parse(encryptionService.decrypt(msg.images))
                        : undefined;
                    const decryptedStatistics = msg.statistics
                        ? JSON.parse(encryptionService.decrypt(msg.statistics))
                        : undefined;
                    const decryptedArtifacts = msg.artifacts
                        ? JSON.parse(encryptionService.decrypt(msg.artifacts))
                        : undefined;
                    const parentId = msg.parent_id || msg.id;
                    const siblingCount = siblingCountMap.get(parentId) || 1;
                    return {
                        id: msg.id,
                        role: msg.role,
                        content: decryptedContent,
                        timestamp: msg.timestamp,
                        model: msg.model,
                        images: decryptedImages,
                        statistics: decryptedStatistics,
                        artifacts: decryptedArtifacts,
                        parentId: msg.parent_id,
                        branchIndex: msg.branch_index ?? 0,
                        isActive: msg.is_active !== 0,
                        siblingCount: siblingCount > 1 ? siblingCount : undefined,
                    };
                }),
            };
        }
        else {
            // Fallback to JSON
            const sessions = this.getAllSessions();
            return sessions.find(s => s.id === sessionId);
        }
    }
    saveSession(session, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            // Use transaction for consistency
            const transaction = db.transaction((session) => {
                // Insert or update session
                const sessionStmt = db.prepare(`
          INSERT OR REPLACE INTO sessions (id, user_id, title, model, persona_id, created_at, updated_at)
          VALUES (?, ?, ?, ?, ?, ?, ?)
        `);
                // Encrypt sensitive session data
                const encryptedTitle = encryptionService.encrypt(session.title);
                sessionStmt.run(session.id, userId, encryptedTitle, session.model, session.personaId || null, session.createdAt, session.updatedAt);
                // Delete existing messages
                const deleteMessagesStmt = db.prepare('DELETE FROM session_messages WHERE session_id = ?');
                deleteMessagesStmt.run(session.id);
                // Insert messages
                if (session.messages && session.messages.length > 0) {
                    const insertMessageStmt = db.prepare(`
            INSERT INTO session_messages (id, session_id, role, content, timestamp, message_index, model, images, statistics, artifacts, parent_id, branch_index, is_active)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
          `);
                    session.messages.forEach((message, index) => {
                        // Encrypt sensitive data before storing
                        const encryptedContent = encryptionService.encrypt(message.content);
                        const encryptedImages = message.images
                            ? encryptionService.encrypt(JSON.stringify(message.images))
                            : null;
                        const encryptedStatistics = message.statistics
                            ? encryptionService.encrypt(JSON.stringify(message.statistics))
                            : null;
                        const encryptedArtifacts = message.artifacts
                            ? encryptionService.encrypt(JSON.stringify(message.artifacts))
                            : null;
                        // Use the message's own ID if it has one, otherwise generate a new one
                        const messageId = message.id || uuidv4();
                        insertMessageStmt.run(messageId, session.id, message.role, encryptedContent, message.timestamp, index, message.model || null, encryptedImages, encryptedStatistics, encryptedArtifacts, message.parentId || null, message.branchIndex ?? 0, message.isActive !== false ? 1 : 0);
                    });
                }
            });
            transaction(session);
        }
        else {
            // Fallback to JSON
            try {
                const sessions = this.getAllSessions();
                const existingIndex = sessions.findIndex(s => s.id === session.id);
                if (existingIndex >= 0) {
                    sessions[existingIndex] = session;
                }
                else {
                    sessions.push(session);
                }
                fs.writeFileSync(this.sessionsFile, JSON.stringify(sessions, null, 2));
            }
            catch (error) {
                console.error('Failed to save session to JSON:', error);
            }
        }
    }
    deleteSession(sessionId, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('DELETE FROM sessions WHERE id = ? AND user_id = ?');
            const result = stmt.run(sessionId, userId);
            return result.changes > 0;
        }
        else {
            // Fallback to JSON
            try {
                const sessions = this.getAllSessions();
                const filteredSessions = sessions.filter(s => s.id !== sessionId);
                if (filteredSessions.length !== sessions.length) {
                    fs.writeFileSync(this.sessionsFile, JSON.stringify(filteredSessions, null, 2));
                    return true;
                }
            }
            catch (error) {
                console.error('Failed to delete session from JSON:', error);
            }
        }
        return false;
    }
    clearAllSessions(userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('DELETE FROM sessions WHERE user_id = ?');
            const result = stmt.run(userId);
            return result.changes;
        }
        else {
            // Fallback to JSON
            try {
                const currentSessions = this.getAllSessions();
                const deletedCount = currentSessions.length;
                fs.writeFileSync(this.sessionsFile, JSON.stringify([], null, 2));
                return deletedCount;
            }
            catch (error) {
                console.error('Failed to clear all sessions from JSON:', error);
                return 0;
            }
        }
    }
    // =================================
    // PREFERENCES MANAGEMENT
    // =================================
    /**
     * Safely decrypt and parse a preference value with proper error handling
     * Returns null for corrupted data (which will be cleaned up)
     */
    safeDecryptPreference(key, value, _userId) {
        try {
            const decryptedValue = encryptionService.decrypt(value);
            try {
                return JSON.parse(decryptedValue);
            }
            catch {
                // Corrupted - will be cleaned up
                return null;
            }
        }
        catch {
            // Try as unencrypted legacy data
            try {
                return JSON.parse(value);
            }
            catch {
                // Corrupted - will be cleaned up
                return null;
            }
        }
    }
    /**
     * Delete a corrupted preference from the database
     */
    deleteCorruptedPreference(userId, key) {
        if (this.useSQLite) {
            const db = getDatabase();
            db.prepare('DELETE FROM user_preferences WHERE user_id = ? AND key = ?').run(userId, key);
            console.log(`Cleaned up corrupted preference: ${key}`);
        }
    }
    getPreferences(userId) {
        if (this.useSQLite) {
            const db = getDatabase();
            // If no userId provided, get the first user (single-user mode)
            if (!userId) {
                const firstUser = db.prepare('SELECT id FROM users LIMIT 1').get();
                if (firstUser) {
                    userId = firstUser.id;
                }
                else {
                    return null; // No users found, return null
                }
            }
            const stmt = db.prepare('SELECT key, value FROM user_preferences WHERE user_id = ?');
            const rows = stmt.all(userId);
            if (rows.length === 0)
                return null;
            const preferences = {};
            const corruptedKeys = [];
            rows.forEach(row => {
                const value = this.safeDecryptPreference(row.key, row.value, userId);
                if (value === null) {
                    corruptedKeys.push(row.key);
                }
                else {
                    preferences[row.key] = value;
                }
            });
            // Clean up corrupted preferences
            if (corruptedKeys.length > 0 && userId) {
                corruptedKeys.forEach(key => this.deleteCorruptedPreference(userId, key));
            }
            return Object.keys(preferences).length > 0
                ? preferences
                : null;
        }
        else {
            // Fallback to JSON
            try {
                if (fs.existsSync(this.preferencesFile)) {
                    const data = fs.readFileSync(this.preferencesFile, 'utf8');
                    return JSON.parse(data);
                }
            }
            catch (error) {
                console.error('Failed to load preferences from JSON:', error);
            }
        }
        return null;
    }
    savePreferences(preferences, userId) {
        if (this.useSQLite) {
            const db = getDatabase();
            const now = Date.now();
            // If no userId provided, get the first user (single-user mode)
            if (!userId) {
                const firstUser = db.prepare('SELECT id FROM users LIMIT 1').get();
                if (firstUser) {
                    userId = firstUser.id;
                }
                else {
                    throw new Error('No users found in database');
                }
            }
            const transaction = db.transaction((preferences) => {
                // Delete existing preferences for this user
                const deleteStmt = db.prepare('DELETE FROM user_preferences WHERE user_id = ?');
                deleteStmt.run(userId);
                // Insert new preferences
                const insertStmt = db.prepare(`
          INSERT INTO user_preferences (id, user_id, key, value, created_at, updated_at)
          VALUES (?, ?, ?, ?, ?, ?)
        `);
                Object.entries(preferences).forEach(([key, value]) => {
                    // Skip undefined values - they would cause NOT NULL constraint errors
                    if (value === undefined) {
                        return;
                    }
                    // Encrypt the preference value before storing
                    const encryptedValue = encryptionService.encrypt(JSON.stringify(value));
                    insertStmt.run(uuidv4(), userId, key, encryptedValue, now, now);
                });
            });
            transaction(preferences);
        }
        else {
            // Fallback to JSON
            try {
                fs.writeFileSync(this.preferencesFile, JSON.stringify(preferences, null, 2));
            }
            catch (error) {
                console.error('Failed to save preferences to JSON:', error);
            }
        }
    }
    // =================================
    // DOCUMENT MANAGEMENT
    // =================================
    getAllDocuments(userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare(`
        SELECT * FROM documents WHERE user_id = ? ORDER BY uploaded_at DESC
      `);
            const rows = stmt.all(userId);
            return rows.map(row => {
                // Decrypt document data
                const decryptedTitle = row.title
                    ? encryptionService.decrypt(row.title)
                    : undefined;
                const decryptedContent = row.content
                    ? encryptionService.decrypt(row.content)
                    : undefined;
                const decryptedMetadata = row.metadata
                    ? JSON.parse(encryptionService.decrypt(row.metadata))
                    : undefined;
                return {
                    id: row.id,
                    filename: row.filename,
                    title: decryptedTitle,
                    content: decryptedContent,
                    fileType: row.file_type,
                    size: row.size,
                    sessionId: row.session_id,
                    uploadedAt: row.uploaded_at,
                    createdAt: row.created_at,
                    metadata: decryptedMetadata,
                };
            });
        }
        else {
            // Fallback to JSON
            try {
                if (fs.existsSync(this.documentsFile)) {
                    const data = fs.readFileSync(this.documentsFile, 'utf8');
                    return JSON.parse(data);
                }
            }
            catch (error) {
                console.error('Failed to load documents from JSON:', error);
            }
        }
        return [];
    }
    getDocument(documentId, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('SELECT * FROM documents WHERE id = ? AND user_id = ?');
            const row = stmt.get(documentId, userId);
            if (!row)
                return undefined;
            // Decrypt document data
            const decryptedTitle = row.title
                ? encryptionService.decrypt(row.title)
                : undefined;
            const decryptedContent = row.content
                ? encryptionService.decrypt(row.content)
                : undefined;
            const decryptedMetadata = row.metadata
                ? JSON.parse(encryptionService.decrypt(row.metadata))
                : undefined;
            return {
                id: row.id,
                filename: row.filename,
                title: decryptedTitle,
                content: decryptedContent,
                fileType: row.file_type,
                size: row.size,
                sessionId: row.session_id,
                uploadedAt: row.uploaded_at,
                createdAt: row.created_at,
                metadata: decryptedMetadata,
            };
        }
        else {
            // Fallback to JSON
            const documents = this.getAllDocuments();
            return documents.find(d => d.id === documentId);
        }
    }
    saveDocument(document, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const now = Date.now();
            const stmt = db.prepare(`
        INSERT OR REPLACE INTO documents 
        (id, user_id, filename, title, content, metadata, uploaded_at, created_at, updated_at)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
      `);
            // Encrypt sensitive document data
            const encryptedTitle = document.title
                ? encryptionService.encrypt(document.title)
                : null;
            const encryptedContent = document.content
                ? encryptionService.encrypt(document.content)
                : null;
            const encryptedMetadata = document.metadata
                ? encryptionService.encrypt(JSON.stringify(document.metadata))
                : null;
            stmt.run(document.id, userId, document.filename, encryptedTitle, encryptedContent, encryptedMetadata, document.uploadedAt, document.createdAt || now, now);
        }
        else {
            // Fallback to JSON
            try {
                const documents = this.getAllDocuments();
                const existingIndex = documents.findIndex(d => d.id === document.id);
                if (existingIndex >= 0) {
                    documents[existingIndex] = document;
                }
                else {
                    documents.push(document);
                }
                fs.writeFileSync(this.documentsFile, JSON.stringify(documents, null, 2));
            }
            catch (error) {
                console.error('Failed to save document to JSON:', error);
            }
        }
    }
    deleteDocument(documentId, userId = 'default') {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('DELETE FROM documents WHERE id = ? AND user_id = ?');
            const result = stmt.run(documentId, userId);
            return result.changes > 0;
        }
        else {
            // Fallback to JSON
            try {
                const documents = this.getAllDocuments();
                const filteredDocuments = documents.filter(d => d.id !== documentId);
                if (filteredDocuments.length !== documents.length) {
                    fs.writeFileSync(this.documentsFile, JSON.stringify(filteredDocuments, null, 2));
                    return true;
                }
            }
            catch (error) {
                console.error('Failed to delete document from JSON:', error);
            }
        }
        return false;
    }
    // =================================
    // DOCUMENT CHUNKS MANAGEMENT
    // =================================
    getDocumentChunks(documentId) {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare(`
        SELECT * FROM document_chunks WHERE document_id = ? ORDER BY chunk_index ASC
      `);
            const rows = stmt.all(documentId);
            return rows.map(row => {
                // Decrypt document chunk data
                const decryptedContent = encryptionService.decrypt(row.content);
                const decryptedEmbedding = row.embedding
                    ? JSON.parse(encryptionService.decrypt(row.embedding))
                    : undefined;
                const decryptedMetadata = row.metadata
                    ? JSON.parse(encryptionService.decrypt(row.metadata))
                    : undefined;
                return {
                    id: row.id,
                    documentId: row.document_id,
                    content: decryptedContent,
                    embedding: decryptedEmbedding,
                    chunkIndex: row.chunk_index,
                    startChar: row.start_char,
                    endChar: row.end_char,
                    metadata: decryptedMetadata,
                };
            });
        }
        else {
            // Fallback to JSON
            try {
                if (fs.existsSync(this.documentChunksFile)) {
                    const data = fs.readFileSync(this.documentChunksFile, 'utf8');
                    const chunksData = JSON.parse(data);
                    return chunksData[documentId] || [];
                }
            }
            catch (error) {
                console.error('Failed to load document chunks from JSON:', error);
            }
        }
        return [];
    }
    saveDocumentChunks(documentId, chunks) {
        if (this.useSQLite) {
            const db = getDatabase();
            const now = Date.now();
            const transaction = db.transaction((documentId, chunks) => {
                // Delete existing chunks
                const deleteStmt = db.prepare('DELETE FROM document_chunks WHERE document_id = ?');
                deleteStmt.run(documentId);
                // Insert new chunks
                if (chunks.length > 0) {
                    const insertStmt = db.prepare(`
            INSERT INTO document_chunks 
            (id, document_id, chunk_index, content, start_char, end_char, embedding, created_at)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
          `);
                    chunks.forEach(chunk => {
                        // Encrypt chunk data
                        const encryptedContent = encryptionService.encrypt(chunk.content);
                        const encryptedEmbedding = chunk.embedding
                            ? encryptionService.encrypt(JSON.stringify(chunk.embedding))
                            : null;
                        insertStmt.run(chunk.id, documentId, chunk.chunkIndex, encryptedContent, chunk.startChar || null, chunk.endChar || null, encryptedEmbedding, now);
                    });
                }
            });
            transaction(documentId, chunks);
        }
        else {
            // Fallback to JSON
            try {
                let chunksData = {};
                if (fs.existsSync(this.documentChunksFile)) {
                    const data = fs.readFileSync(this.documentChunksFile, 'utf8');
                    chunksData = JSON.parse(data);
                }
                chunksData[documentId] = chunks;
                fs.writeFileSync(this.documentChunksFile, JSON.stringify(chunksData, null, 2));
            }
            catch (error) {
                console.error('Failed to save document chunks to JSON:', error);
            }
        }
    }
    deleteDocumentChunks(documentId) {
        if (this.useSQLite) {
            const db = getDatabase();
            const stmt = db.prepare('DELETE FROM document_chunks WHERE document_id = ?');
            const result = stmt.run(documentId);
            return result.changes > 0;
        }
        else {
            // Fallback to JSON
            try {
                if (fs.existsSync(this.documentChunksFile)) {
                    const data = fs.readFileSync(this.documentChunksFile, 'utf8');
                    const chunksData = JSON.parse(data);
                    if (chunksData[documentId]) {
                        delete chunksData[documentId];
                        fs.writeFileSync(this.documentChunksFile, JSON.stringify(chunksData, null, 2));
                        return true;
                    }
                }
            }
            catch (error) {
                console.error('Failed to delete document chunks from JSON:', error);
            }
        }
        return false;
    }
}
// Export singleton instance
const storageService = new StorageService();
export default storageService;
//# sourceMappingURL=storage.js.map