/**
 * Delete Confirmation Modal Handler
 *
 * Provides a consistent interface for showing delete confirmation dialogs.
 * Respects the user's "confirm_deletions" setting.
 *
 * Note: This module does not handle external URLs - only UI interactions.
 * URLValidator is not needed here as no URL assignments occur.
 */

// Store the current confirmation callback
let currentConfirmCallback = null;
let currentCancelCallback = null;

/**
 * Delete action configurations with tooltips
 */
const DELETE_ACTIONS = {
    deleteDocument: {
        tooltip: "Permanently delete this document, including PDF and text content. This cannot be undone.",
        title: "Delete Document?",
        message: "This will permanently delete the document and all associated data.",
        buttonText: "Delete Document",
        dangerous: true
    },
    deleteBlob: {
        tooltip: "Remove the PDF file to save space. Text content will be preserved for searching.",
        title: "Remove PDF?",
        message: "The PDF will be deleted but extracted text remains searchable.",
        buttonText: "Remove PDF",
        dangerous: false
    },
    removeFromCollection: {
        tooltip: "Remove from this collection. If not in any other collection, the document will be deleted.",
        title: "Remove from Collection?",
        message: "Document will be removed from this collection.",
        buttonText: "Remove",
        dangerous: false
    },
    deleteCollection: {
        tooltip: "Delete this collection. Documents will remain in the library but will be unlinked from this collection.",
        title: "Delete Collection?",
        message: "All documents will be unlinked. RAG index will be deleted.",
        buttonText: "Delete Collection",
        dangerous: true
    },
    bulkDeleteDocuments: {
        tooltip: "Permanently delete all selected documents and their associated data.",
        title: "Delete Selected Documents?",
        message: "This will permanently delete all selected documents.",
        buttonText: "Delete All",
        dangerous: true
    },
    bulkDeleteBlobs: {
        tooltip: "Remove PDF files from selected documents to free up database space. Text content is preserved.",
        title: "Remove PDFs from Selected?",
        message: "PDF files will be removed from selected documents. Text content is preserved.",
        buttonText: "Remove PDFs",
        dangerous: false
    }
};

/**
 * Initialize the confirmation modal
 */
function initDeleteConfirmModal() {
    const modal = document.getElementById('deleteConfirmModal');
    if (!modal) {
        console.warn('Delete confirmation modal not found in DOM');
        return;
    }

    // Set up confirm button handler
    const confirmBtn = document.getElementById('deleteConfirmBtn');
    if (confirmBtn) {
        confirmBtn.addEventListener('click', function() {
            if (currentConfirmCallback) {
                currentConfirmCallback();
            }
            hideDeleteModal();
        });
    }

    // Set up cancel handlers
    modal.addEventListener('hidden.bs.modal', function() {
        if (currentCancelCallback) {
            currentCancelCallback();
        }
        currentConfirmCallback = null;
        currentCancelCallback = null;
    });
}

/**
 * Show the delete confirmation modal
 *
 * @param {Object} options - Configuration options
 * @param {string} options.action - The action type (e.g., 'deleteDocument')
 * @param {string} options.title - Custom title (overrides action default)
 * @param {string} options.message - Custom message (overrides action default)
 * @param {Array<string>} options.details - List of items that will be deleted
 * @param {string} options.warning - Custom warning message
 * @param {string} options.buttonText - Custom button text
 * @param {Function} options.onConfirm - Callback when confirmed
 * @param {Function} options.onCancel - Callback when cancelled
 * @returns {Promise<boolean>} - Resolves to true if confirmed, false if cancelled
 */
function showDeleteConfirmation(options) {
    return new Promise((resolve) => {
        const actionConfig = DELETE_ACTIONS[options.action] || {};

        // Set modal content
        const titleEl = document.getElementById('deleteModalTitle');
        const messageEl = document.getElementById('deleteModalMessage');
        const detailsEl = document.getElementById('deleteModalDetails');
        const detailsListEl = document.getElementById('deleteDetailsList');
        const warningEl = document.getElementById('deleteModalWarning');
        const warningTextEl = document.getElementById('deleteWarningText');
        const confirmBtnTextEl = document.getElementById('deleteConfirmBtnText');

        // Set title
        if (titleEl) {
            titleEl.textContent = options.title || actionConfig.title || 'Confirm Delete';
        }

        // Set message
        if (messageEl) {
            messageEl.textContent = options.message || actionConfig.message || 'Are you sure you want to delete this item?';
        }

        // Set details
        if (detailsEl && detailsListEl) {
            if (options.details && options.details.length > 0) {
                detailsListEl.innerHTML = options.details.map(d => `<li>${escapeHtml(d)}</li>`).join('');
                detailsEl.style.display = 'block';
            } else {
                detailsEl.style.display = 'none';
            }
        }

        // Set warning
        if (warningEl && warningTextEl) {
            const showWarning = options.warning || actionConfig.dangerous;
            if (showWarning) {
                warningTextEl.textContent = options.warning || 'This action cannot be undone.';
                warningEl.style.display = 'block';
            } else {
                warningEl.style.display = 'none';
            }
        }

        // Set button text
        if (confirmBtnTextEl) {
            confirmBtnTextEl.textContent = options.buttonText || actionConfig.buttonText || 'Delete';
        }

        // Set callbacks
        currentConfirmCallback = () => {
            if (options.onConfirm) options.onConfirm();
            resolve(true);
        };
        currentCancelCallback = () => {
            if (options.onCancel) options.onCancel();
            resolve(false);
        };

        // Show modal
        const modal = document.getElementById('deleteConfirmModal');
        if (modal) {
            const bsModal = new bootstrap.Modal(modal);
            bsModal.show();
        } else {
            // Fallback to native confirm
            const confirmed = confirm(
                (options.title || actionConfig.title || 'Confirm Delete') + '\n\n' +
                (options.message || actionConfig.message || 'Are you sure?')
            );
            if (confirmed) {
                if (options.onConfirm) options.onConfirm();
                resolve(true);
            } else {
                if (options.onCancel) options.onCancel();
                resolve(false);
            }
        }
    });
}

/**
 * Hide the delete confirmation modal
 */
function hideDeleteModal() {
    const modal = document.getElementById('deleteConfirmModal');
    if (modal) {
        const bsModal = bootstrap.Modal.getInstance(modal);
        if (bsModal) {
            bsModal.hide();
        }
    }
}

/**
 * Escape HTML to prevent XSS
 */
function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

/**
 * Get tooltip text for an action
 *
 * @param {string} action - The action type
 * @returns {string} - Tooltip text
 */
function getDeleteTooltip(action) {
    const config = DELETE_ACTIONS[action];
    return config ? config.tooltip : 'Delete this item';
}

/**
 * Check if confirmations are enabled
 *
 * @returns {Promise<boolean>} - True if confirmations are enabled
 */
async function areConfirmationsEnabled() {
    try {
        const response = await fetch('/settings/api/research_library.confirm_deletions');
        if (response.ok) {
            const data = await response.json();
            return data.value !== false;
        }
    } catch (e) {
        console.warn('Could not check confirmation setting:', e);
    }
    return true; // Default to showing confirmations
}

/**
 * Show confirmation if enabled, otherwise run directly
 *
 * @param {Object} options - Options for showDeleteConfirmation
 * @param {Function} action - The action to perform
 */
async function confirmAndRun(options, action) {
    const confirmationsEnabled = await areConfirmationsEnabled();

    if (confirmationsEnabled) {
        const confirmed = await showDeleteConfirmation(options);
        if (confirmed) {
            action();
        }
    } else {
        action();
    }
}

// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', initDeleteConfirmModal);

// Export for use in other modules
if (typeof window !== 'undefined') {
    window.DeleteConfirmation = {
        show: showDeleteConfirmation,
        hide: hideDeleteModal,
        getTooltip: getDeleteTooltip,
        confirmAndRun: confirmAndRun,
        ACTIONS: DELETE_ACTIONS
    };
}
