jQuery(document).ready(function($) {
    
    // Initialize post views display
    function initPostViews() {
        $('.bbai-post-views-counter').each(function() {
            var $counter = $(this);
            var postId = $counter.data('post-id');
            
            if (postId) {
                loadPostViews(postId, $counter);
            }
        });
    }
    
    // Load post views via AJAX
    function loadPostViews(postId, $element) {
        $.ajax({
            url: boostify_blog_ai_post_views.ajax_url,
            type: 'POST',
            data: {
                action: 'boostify_blog_ai_get_post_views',
                post_id: postId,
                nonce: boostify_blog_ai_post_views.nonce
            },
            success: function(response) {
                if (response.success) {
                    updateViewDisplay($element, response.data);
                }
            },
            error: function() {
                $element.html('<span class="bbai-error">Error loading views</span>');
            }
        });
    }
    
    // Update the view display
    function updateViewDisplay($element, data) {
        var displayType = $element.data('display-type') || 'both';
        var format = $element.data('format') || 'default';
        var html = '';
        
        switch (displayType) {
            case 'views-only':
                html = formatViews(data.total_views, format);
                break;
            case 'unique-only':
                html = formatUniqueVisitors(data.unique_visitors, format);
                break;
            case 'both':
            default:
                html = formatViews(data.total_views, format) + ' | ' + formatUniqueVisitors(data.unique_visitors, format);
                break;
        }
        
        $element.html(html);
        $element.addClass('bbai-loaded');
    }
    
    // Format views count
    function formatViews(count, format) {
        switch (format) {
            case 'icon':
                return '<span class="bbai-views-icon">👁</span> <span class="bbai-views-count">' + count + '</span>';
            case 'label':
                return '<span class="bbai-views-label">Views:</span> <span class="bbai-views-count">' + count + '</span>';
            case 'short':
                return '<span class="bbai-views-count">' + count + '</span>';
            case 'default':
            default:
                return '<span class="bbai-views-text">' + count + ' view' + (count !== 1 ? 's' : '') + '</span>';
        }
    }
    
    // Format unique visitors count
    function formatUniqueVisitors(count, format) {
        switch (format) {
            case 'icon':
                return '<span class="bbai-unique-icon">👥</span> <span class="bbai-unique-count">' + count + '</span>';
            case 'label':
                return '<span class="bbai-unique-label">Unique:</span> <span class="bbai-unique-count">' + count + '</span>';
            case 'short':
                return '<span class="bbai-unique-count">' + count + '</span>';
            case 'default':
            default:
                return '<span class="bbai-unique-text">' + count + ' unique visitor' + (count !== 1 ? 's' : '') + '</span>';
        }
    }
    
    // Auto-refresh functionality
    function setupAutoRefresh() {
        var refreshInterval = boostify_blog_ai_post_views.refresh_interval || 0;
        
        if (refreshInterval > 0) {
            setInterval(function() {
                initPostViews();
            }, refreshInterval * 1000);
        }
    }
    
    // Initialize everything
    initPostViews();
    setupAutoRefresh();
    
    // Handle shortcode refresh button
    $(document).on('click', '.bbai-refresh-views', function(e) {
        e.preventDefault();
        var $button = $(this);
        var $counter = $button.siblings('.bbai-post-views-counter');
        var postId = $counter.data('post-id');
        
        $button.addClass('bbai-refreshing');
        loadPostViews(postId, $counter);
        
        setTimeout(function() {
            $button.removeClass('bbai-refreshing');
        }, 1000);
    });
    
}); 