// Task Manager JavaScript

document.addEventListener('DOMContentLoaded', function() {
    console.log("JavaScript loaded!"); // Check if JS is loading
    
    // ========== DARK MODE TOGGLE ==========
    const darkModeToggle = document.getElementById('darkModeToggle');
    
    if (darkModeToggle) {
        console.log("Dark mode button found!"); // Check if button exists
        
        // Check for saved preference
        if (localStorage.getItem('darkMode') === 'enabled') {
            document.body.classList.add('dark-mode');
            darkModeToggle.innerHTML = '<i class="bi bi-sun"></i> Light Mode';
            console.log("Dark mode enabled from storage");
        }
        
        darkModeToggle.addEventListener('click', function() {
            console.log("Dark mode button clicked!"); // Check if click works
            document.body.classList.toggle('dark-mode');
            
            if (document.body.classList.contains('dark-mode')) {
                localStorage.setItem('darkMode', 'enabled');
                this.innerHTML = '<i class="bi bi-sun"></i> Light Mode';
                console.log("Switched to dark mode");
            } else {
                localStorage.setItem('darkMode', 'disabled');
                this.innerHTML = '<i class="bi bi-moon"></i> Dark Mode';
                console.log("Switched to light mode");
            }
        });
    } else {
        console.log("Dark mode button NOT found!");
    }
    
    // ========== SEARCH FUNCTIONALITY ==========
    const searchInput = document.getElementById('searchInput');
    if (searchInput) {
        searchInput.addEventListener('keyup', function() {
            const searchTerm = this.value.toLowerCase();
            const taskCards = document.querySelectorAll('.task-card');
            
            taskCards.forEach(card => {
                const title = card.querySelector('h5').innerText.toLowerCase();
                const description = card.querySelector('.task-description');
                const descText = description ? description.innerText.toLowerCase() : '';
                
                if (title.includes(searchTerm) || descText.includes(searchTerm)) {
                    card.style.display = '';
                } else {
                    card.style.display = 'none';
                }
            });
        });
    }
    
    // ========== CATEGORY FILTERING ==========
    const filterBtns = document.querySelectorAll('.filter-btn');
    if (filterBtns.length > 0) {
        filterBtns.forEach(btn => {
            btn.addEventListener('click', function() {
                const category = this.getAttribute('data-category');
                
                filterBtns.forEach(b => b.classList.remove('active'));
                this.classList.add('active');
                
                const taskCards = document.querySelectorAll('.task-card');
                taskCards.forEach(card => {
                    if (category === 'all') {
                        card.style.display = '';
                    } else {
                        const cardCategory = card.getAttribute('data-category');
                        if (cardCategory === category) {
                            card.style.display = '';
                        } else {
                            card.style.display = 'none';
                        }
                    }
                });
            });
        });
    }
    
    // ========== CONFIRM DELETE ==========
    const deleteButtons = document.querySelectorAll('.delete-task');
    deleteButtons.forEach(btn => {
        btn.addEventListener('click', function(e) {
            if (!confirm('⚠️ Are you sure you want to delete this task?')) {
                e.preventDefault();
            }
        });
    });
    
    // ========== AUTO-HIDE MESSAGES ==========
    const alerts = document.querySelectorAll('.alert');
    alerts.forEach(alert => {
        if (!alert.classList.contains('alert-danger')) {
            setTimeout(() => {
                alert.style.opacity = '0';
                setTimeout(() => {
                    if (alert.parentNode) alert.remove();
                }, 500);
            }, 3000);
        }
    });
});