/**
 * Manages the display of toast-style notifications.
 */
class NotificationManager {
    constructor(containerId = 'notification-container') {
        this.container = document.getElementById(containerId);
        if (!this.container) {
            console.error(`Notification container with id #${containerId} not found.`);
        }
    }

    /**
     * Shows a notification message.
     * @param {string} message The message to display.
     * @param {string} [type='info'] The type of notification ('success', 'error', 'warning', 'info').
     * @param {number} [duration=5000] The duration in milliseconds to show the notification.
     */
    show(message, type = 'info', duration = 5000) {
        if (!this.container) return;

        const notification = document.createElement('div');
        notification.className = `toast-notification ${type}`;

        const iconClass = this.getIcon(type);
        notification.innerHTML = `
            <i class="fas ${iconClass}"></i>
            <p>${message}</p>
            <button class="close-btn">&times;</button>
        `;

        this.container.appendChild(notification);

        // Show with animation
        setTimeout(() => notification.classList.add('show'), 10);

        const close = () => {
            notification.classList.remove('show');
            // Remove from DOM after animation
            notification.addEventListener('transitionend', () => notification.remove());
        };

        const timer = setTimeout(close, duration);

        notification.querySelector('.close-btn').addEventListener('click', () => {
            clearTimeout(timer);
            close();
        });
    }

    /**
     * Gets an appropriate Font Awesome icon class based on the notification type.
     * @param {string} type The notification type.
     * @returns {string} The icon class.
     */
    getIcon(type) {
        switch (type) {
            case 'success': return 'fa-check-circle';
            case 'error': return 'fa-times-circle';
            case 'warning': return 'fa-exclamation-triangle';
            default: return 'fa-info-circle';
        }
    }
}