document.addEventListener('DOMContentLoaded', function () {
    // Confirm delete actions
    const deleteButtons = document.querySelectorAll('.btn-delete');
    deleteButtons.forEach(button => {
        button.addEventListener('click', function (e) {
            const href = this.getAttribute('href');
            // Use a data attribute for the confirmation message for better i18n and flexibility
            let message = this.dataset.confirm || 'Are you sure you want to delete this item? This action cannot be undone.';

            // Fallback for older implementation if data-confirm is not set
            if (!this.dataset.confirm) {
                if (href.includes('delete_user.php')) {
                    message = 'هل أنت متأكد من رغبتك في حذف هذا المستخدم؟';
                } else if (href.includes('delete_service.php')) {
                    message = 'هل أنت متأكد من رغبتك في حذف هذه الخدمة؟';
                } else if (href.includes('delete_certification.php') || href.includes('certifications.php?delete=')) {
                    message = 'هل أنت متأكد من رغبتك في حذف هذه الشهادة؟';
                } else if (href.includes('delete_slider.php')) {
                    message = 'هل أنت متأكد من حذف هذا السلايدر؟';
                }
            }
            
            if (!confirm(message)) {
                e.preventDefault();
            }
        });
    });

    // Sidebar active state highlight based on URL slug
    const currentPath = window.location.pathname;
    const sidebarLinks = document.querySelectorAll('.sidebar ul li a');
    
    sidebarLinks.forEach(link => {
        const href = link.getAttribute('href');
        // Check if the current URL ends with the link href
        if (currentPath.endsWith('/' + href) || (href === 'dashboard.php' && currentPath.endsWith('/admin/'))) {
            link.classList.add('active');
        } else {
            link.classList.remove('active');
        }
    });

    // Theme Toggle Logic for both public and admin
    const themeToggleButton = document.getElementById('theme-toggle') || document.getElementById('admin-theme-toggle');
    if (themeToggleButton) {
        const themeIcon = themeToggleButton.querySelector('.theme-icon');
        const themeStorageKey = document.body.classList.contains('admin-body') ? 'admin_theme' : 'theme';
        const currentTheme = localStorage.getItem(themeStorageKey) || 'light';

        // Function to apply theme
        const applyTheme = (theme) => {
            document.documentElement.setAttribute('data-theme', theme);
            if (themeIcon) {
                themeIcon.textContent = theme === 'dark' ? '☀️' : '🌙';
            }
            localStorage.setItem(themeStorageKey, theme);
        };

        // Apply initial theme
        applyTheme(currentTheme);

        themeToggleButton.addEventListener('click', () => {
            let newTheme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
            applyTheme(newTheme);
        });
    }
});
