/**
 * Save to Collection Component
 *
 * Allows users to save research (report + sources) to user collections
 * for semantic search.
 */
(function() {
    // URL security: This component uses centralized URLS.LIBRARY_API constants for API endpoints.
    // External URL validation is available via URLValidator.isSafeUrl if needed.

    let modal = null;
    let researchId = null;

    /**
     * Initialize the save to collection component
     */
    function initSaveToCollection() {
        const saveBtn = document.getElementById('save-to-collection-btn');
        if (!saveBtn) return;

        // Get research ID from URL
        researchId = getResearchIdFromUrl();
        if (!researchId) {
            saveBtn.disabled = true;
            saveBtn.title = 'Research ID not found';
            return;
        }

        // Set up click handler
        saveBtn.addEventListener('click', showCollectionModal);
    }

    /**
     * Get research ID from URL
     */
    function getResearchIdFromUrl() {
        return URLBuilder.extractResearchIdFromPattern('results');
    }

    /**
     * Show the collection selection modal
     */
    async function showCollectionModal() {
        // Initialize modal if not already
        const modalElement = document.getElementById('saveToCollectionModal');
        if (!modalElement) {
            SafeLogger.error('Save to collection modal not found');
            return;
        }

        if (typeof bootstrap === 'undefined' || !bootstrap.Modal) {
            SafeLogger.error('Bootstrap Modal not available');
            return;
        }
        modal = bootstrap.Modal.getOrCreateInstance(modalElement);
        modal.show();

        // Reset UI state
        document.getElementById('collection-list-loading').style.display = 'block';
        document.getElementById('collection-list').style.display = 'none';
        document.getElementById('collection-error').style.display = 'none';
        document.getElementById('collection-success').style.display = 'none';

        // Load collections
        await loadCollections();
    }

    /**
     * Load available collections
     */
    async function loadCollections() {
        try {
            const response = await fetch(URLS.LIBRARY_API.COLLECTIONS);
            if (!response.ok) {
                throw new Error(`Server returned ${response.status}`);
            }
            const data = await response.json();

            if (!data.success) {
                throw new Error(data.error || 'Failed to load collections');
            }

            renderCollections(data.collections);

        } catch (error) {
            SafeLogger.error('Error loading collections:', error);
            document.getElementById('collection-list-loading').style.display = 'none';
            const errorDiv = document.getElementById('collection-error');
            errorDiv.textContent = 'Failed to load collections: ' + error.message;
            errorDiv.style.display = 'block';
        }
    }

    /**
     * Render the collection list
     */
    function renderCollections(collections) {
        const loadingDiv = document.getElementById('collection-list-loading');
        const listDiv = document.getElementById('collection-list');
        const itemsDiv = document.getElementById('collection-items');

        loadingDiv.style.display = 'none';

        if (!collections || collections.length === 0) {
            // bearer:disable javascript_lang_dangerous_insert_html — static HTML, no user data
            itemsDiv.innerHTML = `
                <div class="text-center text-muted py-3">
                    <i class="fas fa-folder-open"></i>
                    <p class="mt-2">No collections found. Create a collection in the Library first.</p>
                </div>
            `;
            listDiv.style.display = 'block';
            return;
        }

        // Build collection items
        itemsDiv.textContent = '';
        collections.forEach(col => {
            const button = document.createElement('button');
            button.type = 'button';
            button.className = 'list-group-item list-group-item-action d-flex justify-content-between align-items-center';
            button.dataset.collectionId = col.id;
            button.dataset.collectionName = col.name;

            const div = document.createElement('div');
            const folderIcon = document.createElement('i');
            folderIcon.className = 'fas fa-folder me-2';
            div.appendChild(folderIcon);
            const strong = document.createElement('strong');
            strong.textContent = col.name;
            div.appendChild(strong);
            if (col.description) {
                div.appendChild(document.createElement('br'));
                const small = document.createElement('small');
                small.className = 'text-muted';
                small.textContent = col.description;
                div.appendChild(small);
            }
            button.appendChild(div);

            const chevron = document.createElement('i');
            chevron.className = 'fas fa-chevron-right text-muted';
            button.appendChild(chevron);

            itemsDiv.appendChild(button);
        });

        // Attach click listeners via event delegation (avoids XSS from inline handlers)
        itemsDiv.querySelectorAll('button[data-collection-id]').forEach(btn => {
            btn.addEventListener('click', () => {
                saveToCollection(btn.dataset.collectionId, btn.dataset.collectionName);
            });
        });

        listDiv.style.display = 'block';
    }

    /**
     * Save research to the selected collection
     */
    async function saveToCollection(collectionId, collectionName) {
        const itemsDiv = document.getElementById('collection-items');
        const errorDiv = document.getElementById('collection-error');
        const successDiv = document.getElementById('collection-success');

        // Find and disable the clicked button (save original children for error recovery)
        const buttons = itemsDiv.querySelectorAll('button');
        let originalBtnChildren = null;
        buttons.forEach(btn => {
            btn.disabled = true;
            if (btn.dataset.collectionId === collectionId) {
                originalBtnChildren = Array.from(btn.childNodes).map(n => n.cloneNode(true));
                btn.textContent = '';
                const spinDiv = document.createElement('div');
                const spinner = document.createElement('i');
                spinner.className = 'fas fa-spinner fa-spin me-2';
                spinDiv.appendChild(spinner);
                const savingText = document.createElement('strong');
                savingText.textContent = 'Saving...';
                spinDiv.appendChild(savingText);
                btn.appendChild(spinDiv);
            }
        });

        errorDiv.style.display = 'none';
        successDiv.style.display = 'none';

        try {
            // Get CSRF token
            const csrfToken = (window.api && window.api.getCsrfToken)
                ? window.api.getCsrfToken()
                : (document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '');

            const response = await fetch(URLBuilder.build(URLS.LIBRARY_API.RESEARCH_ADD_TO_COLLECTION, researchId), {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify({ collection_id: collectionId })
            });

            if (!response.ok) {
                throw new Error(`Server returned ${response.status}`);
            }
            const data = await response.json();

            if (!data.success) {
                throw new Error(data.error || 'Failed to save to collection');
            }

            // Show success message
            successDiv.textContent = '';
            const checkIcon = document.createElement('i');
            checkIcon.className = 'fas fa-check-circle';
            successDiv.appendChild(checkIcon);
            successDiv.appendChild(document.createTextNode(' Saved to '));
            const nameStrong = document.createElement('strong');
            nameStrong.textContent = collectionName;
            successDiv.appendChild(nameStrong);
            successDiv.appendChild(document.createTextNode('!'));
            successDiv.appendChild(document.createElement('br'));
            const docsSmall = document.createElement('small');
            docsSmall.textContent = String(data.documents_added || 0) + ' documents added.';
            successDiv.appendChild(docsSmall);
            successDiv.style.display = 'block';

            // Re-enable buttons after a delay
            setTimeout(() => {
                loadCollections();
            }, 2000);

        } catch (error) {
            SafeLogger.error('Error saving to collection:', error);
            errorDiv.textContent = error.message;
            errorDiv.style.display = 'block';

            // Re-enable buttons and restore original content
            buttons.forEach(btn => {
                btn.disabled = false;
                if (btn.dataset.collectionId === collectionId && originalBtnChildren) {
                    btn.textContent = '';
                    originalBtnChildren.forEach(node => btn.appendChild(node));
                }
            });
        }
    }

    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initSaveToCollection);
    } else {
        initSaveToCollection();
    }
})();
