/**
 * Delete Manager
 *
 * Handles all delete operations for the library:
 * - Document deletion
 * - Blob-only deletion
 * - Collection deletion
 * - Bulk operations
 * - Remove from collection
 *
 * Note: This module only uses internal API endpoints (no external URLs).
 * URLValidator is not needed here as all fetch() calls are to internal paths.
 */

// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)

// API Endpoints
const DELETE_API = {
    DOCUMENT: '/library/api/document/',
    DOCUMENT_BLOB: '/library/api/document/{id}/blob',
    DOCUMENT_PREVIEW: '/library/api/document/{id}/preview',
    COLLECTION: '/library/api/collections/',
    COLLECTION_INDEX: '/library/api/collections/{id}/index',
    COLLECTION_PREVIEW: '/library/api/collections/{id}/preview',
    COLLECTION_DOCUMENT: '/library/api/collection/{collectionId}/document/{documentId}',
    BULK_DOCUMENTS: '/library/api/documents/bulk',
    BULK_BLOBS: '/library/api/documents/blobs',
    BULK_COLLECTION_DOCUMENTS: '/library/api/collection/{collectionId}/documents/bulk',
    BULK_PREVIEW: '/library/api/documents/preview'
};

/**
 * Make a DELETE request to the API
 */
async function deleteRequest(url, body = null) {
    const options = { method: 'DELETE' };
    if (body) {
        options.body = JSON.stringify(body);
    }
    return window.api.fetchWithErrorHandling(url, options);
}

/**
 * Make a GET request to the API
 */
async function getRequest(url) {
    return window.api.fetchWithErrorHandling(url, { method: 'GET' });
}

/**
 * Make a POST request to the API
 */
async function postRequest(url, body) {
    return window.api.fetchWithErrorHandling(url, {
        method: 'POST',
        body: JSON.stringify(body)
    });
}

/**
 * Format bytes to human readable size.
 * Delegates to the shared window.formatBytes (utils/format-bytes.js, loaded
 * globally in base.html). Kept as a thin local wrapper so the internal call
 * sites and the DeleteManager.formatBytes export stay unchanged.
 */
function formatBytes(bytes) {
    return window.formatBytes(bytes);
}

// =============================================================================
// Document Operations
// =============================================================================

/**
 * Delete a document completely
 *
 * @param {string} documentId - The document ID
 * @param {Object} options - Options
 * @param {boolean} options.skipConfirm - Skip confirmation dialog
 * @param {Function} options.onSuccess - Callback on success
 * @param {Function} options.onError - Callback on error
 */
async function deleteDocument(documentId, options = {}) {
    const confirmOptions = {
        action: 'deleteDocument',
        onConfirm: async () => {
            try {
                const result = await deleteRequest(DELETE_API.DOCUMENT + documentId);

                if (result.success) {
                    showNotification('success', 'Document deleted successfully');

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error deleting document:', error);
                showNotification('error', 'Failed to delete document: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        // Get preview for better confirmation message
        try {
            const preview = await getRequest(DELETE_API.DOCUMENT_PREVIEW.replace('{id}', documentId));
            if (preview.success) {
                confirmOptions.title = `Delete "${preview.title}"?`;
                confirmOptions.details = [];
                if (preview.has_blob) {
                    confirmOptions.details.push(`PDF file (${formatBytes(preview.blob_size)})`);
                }
                if (preview.has_text) {
                    confirmOptions.details.push('Extracted text content');
                }
                if (preview.chunks_count > 0) {
                    confirmOptions.details.push(`${preview.chunks_count} RAG index chunks`);
                }
                if (preview.collections_count > 0) {
                    confirmOptions.details.push(`Links to ${preview.collections_count} collection(s)`);
                }
            }
        } catch (e) {
            SafeLogger.warn('Could not get deletion preview:', e);
        }

        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

/**
 * Delete only the PDF blob, keeping text content
 *
 * @param {string} documentId - The document ID
 * @param {Object} options - Options
 */
async function deleteDocumentBlob(documentId, options = {}) {
    const confirmOptions = {
        action: 'deleteBlob',
        onConfirm: async () => {
            try {
                const result = await deleteRequest(
                    DELETE_API.DOCUMENT_BLOB.replace('{id}', documentId)
                );

                if (result.success) {
                    showNotification('success', `PDF removed (${formatBytes(result.bytes_freed)} freed)`);

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error deleting blob:', error);
                showNotification('error', 'Failed to remove PDF: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

/**
 * Remove document from a collection
 *
 * @param {string} documentId - The document ID
 * @param {string} collectionId - The collection ID
 * @param {Object} options - Options
 */
async function removeFromCollection(documentId, collectionId, options = {}) {
    const confirmOptions = {
        action: 'removeFromCollection',
        message: 'Document will be removed from this collection. If it\'s not in any other collection, it will be permanently deleted.',
        onConfirm: async () => {
            try {
                const url = DELETE_API.COLLECTION_DOCUMENT
                    .replace('{collectionId}', collectionId)
                    .replace('{documentId}', documentId);

                const result = await deleteRequest(url);

                if (result.success) {
                    const message = result.document_deleted
                        ? 'Document removed and deleted (not in other collections)'
                        : 'Document removed from collection';
                    showNotification('success', message);

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Remove failed');
                }
            } catch (error) {
                SafeLogger.error('Error removing from collection:', error);
                showNotification('error', 'Failed to remove: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

// =============================================================================
// Collection Operations
// =============================================================================

/**
 * Delete a collection
 *
 * @param {string} collectionId - The collection ID
 * @param {Object} options - Options
 */
async function deleteCollection(collectionId, options = {}) {
    const confirmOptions = {
        action: 'deleteCollection',
        onConfirm: async () => {
            try {
                const result = await deleteRequest(DELETE_API.COLLECTION + collectionId);

                if (result.success) {
                    showNotification('success',
                        `Collection deleted (${result.documents_unlinked} documents unlinked, ${result.chunks_deleted} chunks removed)`
                    );

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error deleting collection:', error);
                showNotification('error', 'Failed to delete collection: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        // Get preview for better confirmation message
        try {
            const preview = await getRequest(
                DELETE_API.COLLECTION_PREVIEW.replace('{id}', collectionId)
            );
            if (preview.success) {
                confirmOptions.title = `Delete "${preview.name}"?`;
                confirmOptions.details = [];
                if (preview.documents_count > 0) {
                    confirmOptions.details.push(`${preview.documents_count} document(s) will be unlinked`);
                }
                if (preview.chunks_count > 0) {
                    confirmOptions.details.push(`${preview.chunks_count} RAG index chunks`);
                }
                if (preview.folders_count > 0) {
                    confirmOptions.details.push(`${preview.folders_count} linked folder(s)`);
                }
            }
        } catch (e) {
            SafeLogger.warn('Could not get deletion preview:', e);
        }

        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

/**
 * Delete only the RAG index for a collection
 *
 * @param {string} collectionId - The collection ID
 * @param {Object} options - Options
 */
async function deleteCollectionIndex(collectionId, options = {}) {
    const confirmOptions = {
        action: 'deleteCollection',
        title: 'Delete RAG Index?',
        message: 'This will delete the RAG index for this collection. You can rebuild it later.',
        buttonText: 'Delete Index',
        onConfirm: async () => {
            try {
                const result = await deleteRequest(
                    DELETE_API.COLLECTION_INDEX.replace('{id}', collectionId)
                );

                if (result.success) {
                    showNotification('success',
                        `Index deleted (${result.chunks_deleted} chunks removed)`
                    );

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error deleting index:', error);
                showNotification('error', 'Failed to delete index: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

// =============================================================================
// Bulk Operations
// =============================================================================

/**
 * Delete multiple documents
 *
 * @param {Array<string>} documentIds - Array of document IDs
 * @param {Object} options - Options
 */
async function bulkDeleteDocuments(documentIds, options = {}) {
    const confirmOptions = {
        action: 'bulkDeleteDocuments',
        title: `Delete ${documentIds.length} Documents?`,
        message: `This will permanently delete ${documentIds.length} document(s) and all associated data.`,
        onConfirm: async () => {
            try {
                const result = await deleteRequest(DELETE_API.BULK_DOCUMENTS, {
                    document_ids: documentIds
                });

                if (result.success) {
                    showNotification('success',
                        `Deleted ${result.deleted}/${result.total} documents (${formatBytes(result.total_bytes_freed)} freed)`
                    );

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Bulk delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error in bulk delete:', error);
                showNotification('error', 'Bulk delete failed: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        // Get preview for better confirmation message
        try {
            const preview = await postRequest(DELETE_API.BULK_PREVIEW, {
                document_ids: documentIds,
                operation: 'delete'
            });
            if (preview.success) {
                confirmOptions.details = [];
                if (preview.documents_with_blobs > 0) {
                    confirmOptions.details.push(`${preview.documents_with_blobs} PDF file(s) (${formatBytes(preview.total_blob_size)})`);
                }
                if (preview.total_chunks > 0) {
                    confirmOptions.details.push(`${preview.total_chunks} RAG index chunks`);
                }
            }
        } catch (e) {
            SafeLogger.warn('Could not get bulk preview:', e);
        }

        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

/**
 * Delete blobs for multiple documents
 *
 * @param {Array<string>} documentIds - Array of document IDs
 * @param {Object} options - Options
 */
async function bulkDeleteBlobs(documentIds, options = {}) {
    const confirmOptions = {
        action: 'bulkDeleteBlobs',
        title: `Remove PDFs from ${documentIds.length} Documents?`,
        message: 'PDF files will be removed but text content is preserved for searching.',
        onConfirm: async () => {
            try {
                const result = await deleteRequest(DELETE_API.BULK_BLOBS, {
                    document_ids: documentIds
                });

                if (result.success) {
                    showNotification('success',
                        `Removed ${result.deleted} PDF(s) (${formatBytes(result.total_bytes_freed)} freed)`
                    );

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Bulk blob delete failed');
                }
            } catch (error) {
                SafeLogger.error('Error in bulk blob delete:', error);
                showNotification('error', 'Failed to remove PDFs: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        // Get preview
        try {
            const preview = await postRequest(DELETE_API.BULK_PREVIEW, {
                document_ids: documentIds,
                operation: 'delete_blobs'
            });
            if (preview.success && preview.documents_with_blobs > 0) {
                confirmOptions.details = [
                    `${preview.documents_with_blobs} PDF file(s)`,
                    `${formatBytes(preview.total_blob_size)} will be freed`
                ];
            }
        } catch (e) {
            SafeLogger.warn('Could not get bulk preview:', e);
        }

        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

/**
 * Remove multiple documents from a collection
 *
 * @param {Array<string>} documentIds - Array of document IDs
 * @param {string} collectionId - The collection ID
 * @param {Object} options - Options
 */
async function bulkRemoveFromCollection(documentIds, collectionId, options = {}) {
    const confirmOptions = {
        action: 'removeFromCollection',
        title: `Remove ${documentIds.length} Documents?`,
        message: 'Documents will be removed from this collection. Documents not in other collections will be deleted.',
        onConfirm: async () => {
            try {
                const url = DELETE_API.BULK_COLLECTION_DOCUMENTS.replace('{collectionId}', collectionId);
                const result = await deleteRequest(url, {
                    document_ids: documentIds
                });

                if (result.success) {
                    const message = result.deleted > 0
                        ? `Removed ${result.unlinked} (${result.deleted} deleted, not in other collections)`
                        : `Removed ${result.unlinked} document(s) from collection`;
                    showNotification('success', message);

                    if (options.onSuccess) {
                        options.onSuccess(result);
                    }
                } else {
                    throw new Error(result.error || 'Bulk remove failed');
                }
            } catch (error) {
                SafeLogger.error('Error in bulk remove:', error);
                showNotification('error', 'Failed to remove documents: ' + error.message);

                if (options.onError) {
                    options.onError(error);
                }
            }
        }
    };

    if (options.skipConfirm) {
        confirmOptions.onConfirm();
    } else {
        await window.DeleteConfirmation.confirmAndRun(confirmOptions, confirmOptions.onConfirm);
    }
}

// =============================================================================
// Notification Helper
// =============================================================================

/**
 * Show a notification message
 *
 * @param {string} type - 'success', 'error', 'warning', 'info'
 * @param {string} message - The message to display
 */
function showNotification(type, message) {
    // Check if there's a global notification function
    if (typeof window.showToast === 'function') {
        window.showToast(message, type);
        return;
    }

    // Check for Bootstrap toast
    if (typeof bootstrap !== 'undefined' && typeof bootstrap.Toast !== 'undefined') {
        // Create toast element
        const toastContainer = document.querySelector('.ldr-toast-container') ||
            createToastContainer();

        const toastEl = document.createElement('div');
        toastEl.className = `toast align-items-center text-white bg-${window.LdrAlertHelpers.mapAlertType(type)}`;
        toastEl.setAttribute('role', 'alert');
        const escapeHtml = window.escapeHtml || escapeHtmlFallback;
        // bearer:disable javascript_lang_dangerous_insert_html
        toastEl.innerHTML = `
            <div class="d-flex">
                <div class="toast-body">${escapeHtml(message)}</div>
                <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
            </div>
        `;

        toastContainer.appendChild(toastEl);
        const toast = new bootstrap.Toast(toastEl, { delay: 5000 });
        toast.show();

        toastEl.addEventListener('hidden.bs.toast', () => toastEl.remove());
        return;
    }

    // Fallback to console
    SafeLogger.log(`[${type.toUpperCase()}] ${message}`);
}

/**
 * Create toast container if not exists
 */
function createToastContainer() {
    const container = document.createElement('div');
    container.className = 'ldr-toast-container position-fixed bottom-0 end-0 p-3';
    container.style.zIndex = '11000';
    document.body.appendChild(container);
    return container;
}

// =============================================================================
// Export
// =============================================================================

// Export for use in other modules
if (typeof window !== 'undefined') {
    window.DeleteManager = {
        // Document operations
        deleteDocument,
        deleteDocumentBlob,
        removeFromCollection,

        // Collection operations
        deleteCollection,
        deleteCollectionIndex,

        // Bulk operations
        bulkDeleteDocuments,
        bulkDeleteBlobs,
        bulkRemoveFromCollection,

        // Utilities
        formatBytes,
        showNotification
    };
}
