jQuery(document).ready(function($) {
    // ---------- 1. Star logic (unchanged) ----------
    $('.post-rating').each(function() {
        var $container 	= $(this);
        var postId     	= $container.data('post-id');
        var $stars     	= $container.find('.star');
        var $average	= $container.find('.rating-average');
        var average		= parseFloat($container.find('.rating-stars').data('average'));
        var t_sukses	= $container.data('text-succes');
        var t_error		= $container.data('text-error');
        var t_votes		= $container.data('text-1');

        function highlightAvg() {
            $stars.removeClass('active');
            $stars.slice(0, Math.floor(average)).addClass('active');
            if (average % 1 >= 0.5) $stars.eq(Math.floor(average)).addClass('active');
        }
        highlightAvg();

        $stars.hover(
            function() { $stars.slice(0, $(this).data('rating')).addClass('active'); },
            function() { highlightAvg(); }
        );

        // ---------- 2. Click → AJAX ----------
        $stars.on('click', function(e) {
            e.preventDefault();
            var rating = $(this).data('rating');

            $.ajax({
                url: ajax_object.ajax_url,
                type: 'POST',
                data: {
                    action: 'submit_rating',
                    post_id: postId,
                    rating: rating,
                    nonce: ajax_object.nonce
                },
                success: function(response) {
                    if (response.success) {
                        average = response.data.average;
                        $average.html('' + average + '/5 ( ' + response.data.count + ' ' + t_votes + ' )');
                        highlightAvg();
                        $stars.off('click');

                        // ---- SUCCESS POPUP ----
                        showRatingPopup({
                            title: t_sukses,
                            message: response.data.message,
                            stats: '',
                            type: 'success'
                        });
                    } else {
                        // ---- ERROR POPUP ----
                        showRatingPopup({
                            title: t_error,
                            message: response.data,
                            type: 'error'
                        });
                    }
                },
                error: function() {
                    showRatingPopup({
                        title: 'Error',
                        message: 'Failed to submit rating. Please try again.',
                        type: 'error'
                    });
                }
            });
        });
    });

    // ---------- 3. Popup Functions ----------
    var popupTimer;
    function showRatingPopup(opts) {
        var $overlay = $('#rating-popup-overlay');
        var $popup   = $('#rating-popup');
        var $title   = $popup.find('.rating-popup-title');
        var $msg     = $popup.find('.rating-popup-message');
        var $stats   = $popup.find('.rating-popup-stats');

        // Set content
        $title.text(opts.title || '');
        $msg.text(opts.message || '');
        $stats.text(opts.stats || '');
        
        // Apply type
        $popup.removeClass('success error').addClass(opts.type || 'success');
        if (opts.type === 'error') $stats.hide(); else $stats.show();

        // Show
        $overlay.addClass('show');
        $popup.addClass('show');

        clearTimeout(popupTimer);
		// Auto hide after 4 seconds
        popupTimer = setTimeout(hideRatingPopup, 5000);
		
        // Auto-close after 5 sec (optional) 
        if (opts.type !== 'error') {
            popupTimer = setTimeout(hideRatingPopup, 3000);
        }
    }
	
	// === HIDE POPUP ===
    function hideAutoPopup() {
        $('#rating-popup-overlay, #rating-popup').removeClass('show');
    }

    function hideRatingPopup() {
        $('#rating-popup-overlay, #rating-popup').removeClass('show');
    }

    // Close events
    $(document).on('click', '.rating-popup-close, .rating-popup-ok, #rating-popup-overlay', hideRatingPopup);
    $(document).on('keyup', function(e) {
        if (e.key === 'Escape') hideRatingPopup();
    });
});