(function() {
    'use strict';

    const NOTIFICATION_URL = 'https://ishakeducation.vercel.app/notification.json';
    let currentNotification = null;
    let checkInterval = null;
    let lastMessageHash = null;

    function showNotification(message) {
        if (currentNotification) {
            currentNotification.remove();
            currentNotification = null;
        }

        const notification = document.createElement('div');
        notification.id = 'ishak-notification';
        notification.style.cssText = `
            position: fixed;
            bottom: 100px;
            right: 20px;
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(5px);
            color: #333;
            padding: 15px 20px;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            z-index: 10002;
            font-family: Arial, sans-serif;
            max-width: 300px;
            border-left: 4px solid #FF9800;
            pointer-events: none;
        `;
        notification.textContent = message;
        document.body.appendChild(notification);
        currentNotification = notification;
    }

    async function checkNotification() {
        try {
            const response = await fetch(NOTIFICATION_URL + '?t=' + Date.now());
            if (!response.ok) throw new Error('Ошибка загрузки');
            const data = await response.json();
            const message = data.message || '';
            const newHash = message ? message : '';

            if (newHash !== lastMessageHash) {
                lastMessageHash = newHash;
                if (message) {
                    showNotification(message);
                } else {
                    if (currentNotification) {
                        currentNotification.remove();
                        currentNotification = null;
                    }
                }
            }
        } catch (e) {
            console.log('Не удалось проверить уведомления:', e);
        }
    }

    function initNotifications() {
        checkNotification();
        if (checkInterval) clearInterval(checkInterval);
        checkInterval = setInterval(checkNotification, 300000); // 5 минут
    }

    window.initNotifications = initNotifications;
})();