/**
 * Kavehboard Notifications JavaScript
 * Handles notification interactions and AJAX requests
 */

(function($) {
    'use strict';
    
    // Initialize notifications functionality
    $(document).ready(function() {
        initNotifications();
    });
    
    function initNotifications() {
        // Mark single notification as read
        $(document).on('click', '.mark-read-btn', function(e) {
            e.preventDefault();
            
            var $button = $(this);
            var notificationId = $button.data('notification-id');
            var $notificationItem = $button.closest('.notification-item');
            
            if (!notificationId) {
                return;
            }
            
            // Disable button during request
            $button.prop('disabled', true).text('Processing...');
            
            $.ajax({
                url: kavehboard_ajax.ajax_url,
                type: 'POST',
                data: {
                    action: 'kaveh_mark_notification_read',
                    notification_id: notificationId,
                    nonce: kavehboard_ajax.nonce
                },
                success: function(response) {
                    if (response.success) {
                        $notificationItem.removeClass('unread').addClass('read');
                        $button.fadeOut(300, function() {
                            $(this).remove();
                        });
                        
                        // Update unread count
                        updateUnreadCount(-1);
                        
                        // Show success message
                        showNotificationMessage('Notification marked as read', 'success');
                    } else {
                        showNotificationMessage('Failed to mark notification as read', 'error');
                        $button.prop('disabled', false).text('Mark as Read');
                    }
                },
                error: function() {
                    showNotificationMessage('An error occurred', 'error');
                    $button.prop('disabled', false).text('Mark as Read');
                }
            });
        });
        
        // Mark all notifications as read
        $(document).on('click', '#mark-all-read-btn', function(e) {
            e.preventDefault();
            
            var $button = $(this);
            
            // Confirm action
            if (!confirm('Are you sure you want to mark all notifications as read?')) {
                return;
            }
            
            // Disable button during request
            $button.prop('disabled', true).text('Processing...');
            
            $.ajax({
                url: kavehboard_ajax.ajax_url,
                type: 'POST',
                data: {
                    action: 'kaveh_mark_all_notifications_read',
                    nonce: kavehboard_ajax.nonce
                },
                success: function(response) {
                    if (response.success) {
                        // Mark all notification items as read
                        $('.notification-item.unread').removeClass('unread').addClass('read');
                        $('.mark-read-btn').fadeOut(300, function() {
                            $(this).remove();
                        });
                        
                        // Hide the mark all read button
                        $button.fadeOut(300);
                        
                        // Reset unread count
                        updateUnreadCount(0, true);
                        
                        // Show success message
                        showNotificationMessage('All notifications marked as read', 'success');
                    } else {
                        showNotificationMessage('Failed to mark all notifications as read', 'error');
                        $button.prop('disabled', false).text('Mark All as Read');
                    }
                },
                error: function() {
                    showNotificationMessage('An error occurred', 'error');
                    $button.prop('disabled', false).text('Mark All as Read');
                }
            });
        });
    }
    
    // Update unread count in badges
    function updateUnreadCount(change, reset = false) {
        $('.badge').each(function() {
            var $badge = $(this);
            var currentCount = parseInt($badge.text()) || 0;
            var newCount = reset ? 0 : Math.max(0, currentCount + change);
            
            if (newCount === 0) {
                $badge.fadeOut(300);
            } else {
                $badge.text(newCount);
            }
        });
    }
    
    // Show notification message
    function showNotificationMessage(message, type) {
        var alertClass = type === 'success' ? 'alert-success' : 'alert-danger';
        var $alert = $('<div class="alert ' + alertClass + ' alert-dismissible fade show" role="alert">' +
                      message +
                      '<button type="button" class="btn-close" data-bs-dismiss="alert"></button>' +
                      '</div>');
        
        // Insert at top of notifications container
        var $container = $('.kavehboard-notifications-page, .dashboard-kaveh-notifications');
        if ($container.length) {
            $container.prepend($alert);
            
            // Auto-hide after 3 seconds
            setTimeout(function() {
                $alert.fadeOut(300, function() {
                    $(this).remove();
                });
            }, 3000);
        }
    }
    
})(jQuery);