/**
 * Collections Manager JavaScript
 * Handles the Collections page UI interactions and API calls
 */

// Store collections data
let collections = [];
const currentCollectionId = null;

// safeFetch is now provided by utils/safe-fetch.js loaded in base.html

/**
 * Initialize the page
 */
document.addEventListener('DOMContentLoaded', function() {
    loadCollections();

    // Setup auto-index toggle
    const autoIndexToggle = document.getElementById('auto-index-toggle');
    if (autoIndexToggle) {
        loadAutoIndexSetting();
        autoIndexToggle.addEventListener('change', saveAutoIndexSetting);
    }

    // Setup modal handlers (only if modal form exists)
    const createCollectionModalForm = document.getElementById('create-collection-modal-form');
    if (createCollectionModalForm) {
        createCollectionModalForm.addEventListener('submit', handleCreateCollection);
    }

    const cancelCreateBtn = document.getElementById('cancel-create-btn');
    if (cancelCreateBtn) {
        cancelCreateBtn.addEventListener('click', hideCreateCollectionModal);
    }

    // Setup close buttons
    document.querySelectorAll('.modal .ldr-close').forEach(btn => {
        btn.addEventListener('click', () => {
            hideCreateCollectionModal();
        });
    });

    // Click outside modal to close
    window.addEventListener('click', (event) => {
        if (event.target.classList.contains('modal')) {
            event.target.style.display = 'none';
        }
    });
});

/**
 * Load the auto-index setting and update the toggle
 */
async function loadAutoIndexSetting() {
    try {
        const response = await safeFetch('/settings/api/research_library.auto_index_enabled');
        if (!response.ok) return;
        const data = await response.json();
        const toggle = document.getElementById('auto-index-toggle');
        toggle.checked = data.value === true || data.value === 'true';
    } catch (error) {
        SafeLogger.error('Error loading auto-index setting:', error);
    }
}

/**
 * Save the auto-index setting when toggled
 */
async function saveAutoIndexSetting() {
    const toggle = document.getElementById('auto-index-toggle');
    try {
        const csrfToken = window.api ? window.api.getCsrfToken() : '';
        const response = await safeFetch('/settings/api/research_library.auto_index_enabled', {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify({ value: toggle.checked })
        });
        const data = await response.json();
        if (!response.ok || data.error) {
            SafeLogger.error('Failed to save auto-index setting:', data.error);
            toggle.checked = !toggle.checked;
        }
    } catch (error) {
        SafeLogger.error('Error saving auto-index setting:', error);
        toggle.checked = !toggle.checked;
    }
}

/**
 * Load document collections
 */
async function loadCollections() {
    const container = document.getElementById('collections-container');
    const noCollectionsMessage = document.getElementById('no-collections-message');

    try {
        const response = await safeFetch(URLS.LIBRARY_API.COLLECTIONS);
        const data = await response.json();

        if (data.success) {
            collections = data.collections || [];

            if (collections.length === 0) {
                container.style.display = 'none';
                noCollectionsMessage.style.display = 'flex';
            } else {
                container.style.display = 'grid';
                noCollectionsMessage.style.display = 'none';
                renderCollections();
            }
        } else {
            showError('Failed to load collections: ' + data.error);
        }
    } catch (error) {
        SafeLogger.error('Error loading collections:', error);
        showError('Failed to load collections');
    }
}

/**
 * Render collections grid
 */
function renderCollections() {
    const container = document.getElementById('collections-container');

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-28: all interpolations use escapeHtml/esc, numeric coercion, or hardcoded strings
    container.innerHTML = collections.map(collection => `
        <a href="/library/collections/${encodeURIComponent(collection.id)}" class="ldr-collection-card" data-id="${escapeHtml(collection.id)}" style="text-decoration: none; color: inherit; cursor: pointer;">
            <div class="ldr-collection-header">
                <h3>${escapeHtml(collection.name)}</h3>
                ${collection.description ? `<p class="ldr-collection-description">${escapeHtml(collection.description)}</p>` : ''}
            </div>

            <div class="ldr-collection-stats">
                <div class="ldr-stat-item">
                    <i class="fas fa-file"></i>
                    <span>${collection.document_count || 0} documents</span>
                </div>
                ${collection.created_at ? `
                <div class="ldr-stat-item">
                    <i class="fas fa-clock"></i>
                    <span>${new Date(collection.created_at).toLocaleDateString()}</span>
                </div>
                ` : ''}
                ${collection.embedding ? `
                <div class="ldr-stat-item ldr-embedding-info">
                    <i class="fas fa-microchip"></i>
                    <span title="Embedding: ${escapeHtml(collection.embedding.provider)}/${escapeHtml(collection.embedding.model)}">${escapeHtml(collection.embedding.model)}</span>
                </div>
                ` : `
                <div class="ldr-stat-item ldr-embedding-warning">
                    <i class="fas fa-exclamation-triangle"></i>
                    <span title="Collection not yet indexed">Not indexed</span>
                </div>
                `}
            </div>

            <div class="ldr-collection-view-link">
                <span>View</span>
                <i class="fas fa-arrow-right"></i>
            </div>
        </a>
    `).join('');
}

/**
 * Show create collection modal
 */
function showCreateCollectionModal() {
    document.getElementById('create-collection-modal').style.display = 'flex';
    document.getElementById('modal-collection-name').focus();
}

/**
 * Hide create collection modal
 */
function hideCreateCollectionModal() {
    document.getElementById('create-collection-modal').style.display = 'none';
    document.getElementById('create-collection-modal-form').reset();
}

/**
 * Handle create collection form submission
 */
async function handleCreateCollection(event) {
    event.preventDefault();

    const name = document.getElementById('modal-collection-name').value.trim();
    const description = document.getElementById('modal-collection-description').value.trim();

    if (!name) {
        showError('Collection name is required');
        return;
    }

    try {
        const csrfToken = window.api ? window.api.getCsrfToken() : '';
        const response = await safeFetch(URLS.LIBRARY_API.COLLECTIONS, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify({
                name,
                description,
                type: 'user_uploads'
            })
        });

        const data = await response.json();
        if (data.success) {
            showSuccess(`Collection "${name}" created successfully`);
            hideCreateCollectionModal();
            loadCollections();
        } else {
            showError('Failed to create collection: ' + data.error);
        }
    } catch (error) {
        SafeLogger.error('Error creating collection:', error);
        showError('Failed to create collection');
    }
}

/**
 * Format bytes to human readable
 */
function formatBytes(bytes) {
    if (bytes === 0) return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return Math.round(bytes / k ** i * 100) / 100 + ' ' + sizes[i];
}

// Prefer the full escapeHtml from xss-protection.js; inline fallback if it hasn't loaded yet
// bearer:disable javascript_lang_manual_html_sanitization
const escapeHtml = window.escapeHtml || function(str) {
    return String(str).replace(/[&<>"']/g, function(m) {
        return {'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m];
    });
};


/**
 * Show success message
 */
function showSuccess(message) {
    alert('Success: ' + message);
}

/**
 * Show error message
 */
function showError(message) {
    alert('Error: ' + message);
}

/**
 * Show info message
 */
function showInfo(message) {
    SafeLogger.log('Info: ' + message);
}
