// /srv/http/eos-modern/public/assets/js/main.js

document.addEventListener('DOMContentLoaded', () => {
    // 1. Tmavý režim
    const themeToggle = document.getElementById('themeToggle');
    const html = document.documentElement;
    const icon = themeToggle ? themeToggle.querySelector('i') : null;
    
    if (themeToggle && icon) {
        // Načíst uložené téma
        const savedTheme = localStorage.getItem('theme') || 'dark';
        html.setAttribute('data-theme', savedTheme);
        updateIcon(savedTheme);

        themeToggle.addEventListener('click', () => {
            const currentTheme = html.getAttribute('data-theme');
            const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
            html.setAttribute('data-theme', newTheme);
            localStorage.setItem('theme', newTheme);
            updateIcon(newTheme);
        });

        function updateIcon(theme) {
            if (theme === 'dark') {
                icon.className = 'fas fa-sun';
            } else {
                icon.className = 'fas fa-moon';
            }
        }
    }

    // 2. Vyhledávání (Fuse.js)
    const searchInput = document.getElementById('searchInput');
    const searchResults = document.getElementById('searchResults');
    let fuse = null;

    if (searchInput && searchResults) {
        // Cesta k JSON (musí odpovídat WEB_ROOT v config.php)
        // Pokud je web na /eos-modern/public/, cesta je /eos-modern/public/data/articles.json
        const jsonUrl = '/eos-modern/public/data/articles.json';

        fetch(jsonUrl)
            .then(res => res.json())
            .then(data => {
                fuse = new Fuse(data, {
                    keys: ['title', 'excerpt', 'slug'],
                    threshold: 0.3,
                    ignoreLocation: true
                });
            })
            .catch(err => console.error('Chyba při načítání JSON:', err));

        searchInput.addEventListener('input', (e) => {
            const query = e.target.value.trim();
            if (query.length < 2) {
                searchResults.style.display = 'none';
                return;
            }

            if (!fuse) return;

            const results = fuse.search(query);
            searchResults.innerHTML = '';
            
            if (results.length > 0) {
                searchResults.style.display = 'block';
                results.forEach(result => {
                    const item = result.item;
                    const link = document.createElement('a');
                    link.href = `/eos-modern/public/article.php?slug=${item.slug}`;
                    link.className = 'search-item';
                    link.innerHTML = `<strong>${item.title}</strong><br><small>${item.excerpt}</small>`;
                    searchResults.appendChild(link);
                });
            } else {
                searchResults.style.display = 'none';
            }
        });

        // Skrytí výsledků při kliknutí mimo
        document.addEventListener('click', (e) => {
            if (!searchInput.contains(e.target) && !searchResults.contains(e.target)) {
                searchResults.style.display = 'none';
            }
        });
    }
});