/**
 * Help System Service
 * Manages collapsible help panels and dismissal preferences
 *
 * URL Security Note: This service only uses internal relative URLs (/settings/api/*)
 * for the SettingsManager API. No external URLs are handled.
 * URLValidator is not required as all URLs are hardcoded internal API paths.
 *
 * Dismissal preferences are stored in the backend via SettingsManager API
 * for consistency with other UI dismissals and encrypted storage.
 * Collapsed state uses localStorage for session persistence only.
 */

const HelpService = (function() {
    'use strict';

    // Storage key prefix for dismissed panels (backend setting key)
    const STORAGE_PREFIX = 'app.ui.help_dismissed_';

    // Internal API base path (relative URL - no external URLs)
    const SETTINGS_API_BASE = '/settings/api/';

    // Cache for dismissed panel states
    let dismissedCache = {};
    let initialized = false;

    /**
     * Toggle a help panel's collapsed state
     * @param {string} panelId - The panel ID to toggle
     */
    function togglePanel(panelId) {
        const panel = document.getElementById('help-panel-' + panelId);
        if (!panel) {
            SafeLogger.warn('Help panel not found:', panelId);
            return;
        }

        const isCollapsed = panel.classList.toggle('collapsed');
        const header = panel.querySelector('.ldr-help-panel-header');

        if (header) {
            header.setAttribute('aria-expanded', !isCollapsed);
        }

        // Store collapsed preference in localStorage for session persistence
        // (this is UI state, not a user preference, so localStorage is appropriate)
        try {
            localStorage.setItem('ldr_panel_collapsed_' + panelId, isCollapsed ? 'true' : 'false');
        } catch (e) {
            SafeLogger.warn('Failed to save panel state:', e);
        }
    }

    /**
     * Dismiss a help panel permanently using the SettingsManager API
     * @param {string} panelId - The panel ID to dismiss
     */
    async function dismissPanel(panelId) {
        const settingKey = STORAGE_PREFIX + panelId;

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

            if (!csrfToken) {
                SafeLogger.warn('CSRF token not found, dismissal may fail');
            }

            // Save to settings via internal API (relative URL only)
            const apiUrl = SETTINGS_API_BASE + encodeURIComponent(settingKey);
            const response = await fetch(apiUrl, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify({ value: true })
            });

            if (response.ok) {
                // Update cache
                dismissedCache[panelId] = true;

                // Hide the panel
                const panel = document.getElementById('help-panel-' + panelId);
                if (panel) {
                    panel.style.display = 'none';
                }

                // Show confirmation
                if (window.ui && window.ui.showMessage) {
                    window.ui.showMessage('Help panel dismissed', 'info');
                }
            } else {
                SafeLogger.error('Failed to dismiss panel:', response.status);
                if (window.ui && window.ui.showMessage) {
                    window.ui.showMessage('Failed to save preference', 'error');
                }
            }
        } catch (error) {
            SafeLogger.error('Error dismissing panel:', error);
            if (window.ui && window.ui.showMessage) {
                window.ui.showMessage('Failed to save preference', 'error');
            }
        }
    }

    /**
     * Check if a panel has been dismissed (from cache)
     * @param {string} panelId - The panel ID to check
     * @returns {boolean} - True if dismissed
     */
    function isPanelDismissed(panelId) {
        // Check cache
        if (dismissedCache.hasOwnProperty(panelId)) {
            return dismissedCache[panelId];
        }

        return false;
    }

    /**
     * Load dismissed states from backend
     * @param {Array<string>} panelIds - List of panel IDs to check
     */
    async function loadDismissedStates(panelIds) {
        for (const panelId of panelIds) {
            const settingKey = STORAGE_PREFIX + panelId;
            try {
                const apiUrl = SETTINGS_API_BASE + encodeURIComponent(settingKey);
                const response = await fetch(apiUrl);
                if (response.ok) {
                    const data = await response.json();
                    if (data.value === true) {
                        dismissedCache[panelId] = true;
                    }
                }
            } catch (e) {
                // Ignore fetch errors for individual settings
            }
        }
    }

    /**
     * Initialize panel states from backend and localStorage
     */
    async function initPanelStates() {
        const panels = document.querySelectorAll('.ldr-help-panel');
        const panelIds = [];

        panels.forEach(panel => {
            const panelId = panel.getAttribute('data-panel-id');
            if (panelId) {
                panelIds.push(panelId);
            }
        });

        // Load dismissed states from backend
        await loadDismissedStates(panelIds);

        panels.forEach(panel => {
            const panelId = panel.getAttribute('data-panel-id');
            if (!panelId) return;

            // Check if panel was dismissed
            if (isPanelDismissed(panelId)) {
                panel.style.display = 'none';
                return;
            }

            // Restore collapsed state from localStorage (UI state only)
            try {
                const collapsed = localStorage.getItem('ldr_panel_collapsed_' + panelId);
                if (collapsed === 'true') {
                    panel.classList.add('collapsed');
                    const header = panel.querySelector('.ldr-help-panel-header');
                    if (header) {
                        header.setAttribute('aria-expanded', 'false');
                    }
                } else if (collapsed === 'false') {
                    panel.classList.remove('collapsed');
                    const header = panel.querySelector('.ldr-help-panel-header');
                    if (header) {
                        header.setAttribute('aria-expanded', 'true');
                    }
                }
            } catch (e) {
                // Ignore localStorage errors
            }
        });
    }

    /**
     * Reset all dismissed panels (for testing or user request)
     */
    async function resetDismissedPanels() {
        const panels = document.querySelectorAll('.ldr-help-panel');

        // Get CSRF token
        const csrfMeta = document.querySelector('meta[name="csrf-token"]');
        const csrfToken = csrfMeta ? csrfMeta.getAttribute('content') : '';

        for (const panel of panels) {
            const panelId = panel.getAttribute('data-panel-id');
            if (!panelId) continue;

            const settingKey = STORAGE_PREFIX + panelId;

            try {
                // Delete from backend
                const apiUrl = SETTINGS_API_BASE + encodeURIComponent(settingKey);
                await fetch(apiUrl, {
                    method: 'DELETE',
                    headers: {
                        'X-CSRFToken': csrfToken
                    }
                });
            } catch (e) {
                SafeLogger.warn('Failed to reset panel dismissal:', panelId, e);
            }
        }

        // Clear cache
        dismissedCache = {};

        // Show all hidden panels
        panels.forEach(panel => {
            panel.style.display = '';
        });

        if (window.ui && window.ui.showMessage) {
            window.ui.showMessage('Help panels reset', 'success');
        }
    }

    /**
     * Expand all help panels on the page
     */
    function expandAll() {
        document.querySelectorAll('.ldr-help-panel.collapsed').forEach(panel => {
            panel.classList.remove('collapsed');
            const header = panel.querySelector('.ldr-help-panel-header');
            if (header) {
                header.setAttribute('aria-expanded', 'true');
            }
        });
    }

    /**
     * Collapse all help panels on the page
     */
    function collapseAll() {
        document.querySelectorAll('.ldr-help-panel:not(.collapsed)').forEach(panel => {
            panel.classList.add('collapsed');
            const header = panel.querySelector('.ldr-help-panel-header');
            if (header) {
                header.setAttribute('aria-expanded', 'false');
            }
        });
    }

    /**
     * Initialize the help system
     */
    async function init() {
        if (initialized) return;

        // Initialize panel states (async to load from backend)
        await initPanelStates();

        // Add keyboard navigation for tooltips
        document.querySelectorAll('.ldr-help-tooltip').forEach(tooltip => {
            tooltip.addEventListener('keydown', function(e) {
                // Show tooltip on Enter or Space
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    // Toggle aria-expanded for screen readers
                    const expanded = this.getAttribute('aria-expanded') === 'true';
                    this.setAttribute('aria-expanded', !expanded);
                }
            });
        });

        initialized = true;
    }

    // Auto-initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        // DOM is already ready
        init();
    }

    // Public API
    return {
        init: init,
        togglePanel: togglePanel,
        dismissPanel: dismissPanel,
        isPanelDismissed: isPanelDismissed,
        resetDismissedPanels: resetDismissedPanels,
        expandAll: expandAll,
        collapseAll: collapseAll
    };
})();

// Export for global access
window.HelpService = HelpService;
