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

// Store collections data
let collections = [];

// safeFetch is now provided by utils/safe-fetch.js loaded in base.html
// escapeHtml is the canonical window.escapeHtml from security/xss-protection.js
// (loaded first via base.html). Do NOT reintroduce a local fallback — it would
// be weaker (e.g. not escape "/") and risk a duplicate-const SyntaxError (#3706).

/**
 * 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);
    }

});

/**
 * 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 success message
 */
function showSuccess(message) {
    alert('Success: ' + message);
}

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