/**
 * globals.js
 * Contains global UI utilities and helpers used across the CuraFlow application.
 */

// Initialize Toast Container on load
document.addEventListener('DOMContentLoaded', () => {
    if (!document.getElementById('global-toast-container')) {
        const container = document.createElement('div');
        container.id = 'global-toast-container';
        container.className = 'toast-container';
        document.body.appendChild(container);
    }
});

/**
 * Displays a premium sliding toast notification
 * @param {string} message - The text to display
 * @param {string} type - 'success', 'error', 'warning', 'info' (default: 'info')
 */
window.showToast = function(message, type = 'info') {
    const container = document.getElementById('global-toast-container');
    if (!container) return;

    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    
    // Icon based on type
    let icon = 'ℹ️';
    if (type === 'success') icon = '✅';
    if (type === 'error') icon = '❌';
    if (type === 'warning') icon = '⚠️';

    toast.innerHTML = `<span>${icon}</span> <span>${message}</span>`;
    container.appendChild(toast);

    // Trigger animation slightly after append
    requestAnimationFrame(() => {
        requestAnimationFrame(() => {
            toast.classList.add('toast-show');
        });
    });

    // Remove after 3 seconds
    setTimeout(() => {
        toast.classList.remove('toast-show');
        toast.addEventListener('transitionend', () => toast.remove());
    }, 3000);
};

/**
 * Universal dummy handler for unimplemented features
 */
window.handleComingSoon = function(featureName = 'This feature') {
    window.showToast(`${featureName} is coming soon!`, 'info');
};

/**
 * Global Logout Interceptor
 * Captures clicks on any logout button across the app, clears session, and redirects securely.
 */
document.addEventListener('click', (e) => {
    const logoutBtn = e.target.closest('.logout-btn') || e.target.closest('.logout-link') || e.target.closest('.sa-logout');
    if (logoutBtn) {
        e.preventDefault();
        localStorage.clear();
        window.location.href = '/';
    }
});
