/**
 * View Script for Book Page Reviews Block
 * Handles Star Rating Interaction in the Form
 */

document.addEventListener('DOMContentLoaded', function () {
    const reviewForm = document.getElementById('rswpbs-review-form');
    if (!reviewForm) return;

    const starContainer = reviewForm.querySelector('.awt-star-rating-select');
    const hiddenInput = reviewForm.querySelector('input[name="rating"]');

    if (starContainer && hiddenInput) {
        const stars = starContainer.querySelectorAll('.awt-star-icon');

        stars.forEach(star => {
            star.addEventListener('click', function () {
                const rating = this.getAttribute('data-rating');
                hiddenInput.value = rating;

                // Update visual state
                stars.forEach(s => {
                    if (parseInt(s.getAttribute('data-rating')) <= parseInt(rating)) {
                        s.classList.remove('text-gray-300');
                        s.classList.add('text-yellow-400');
                    } else {
                        s.classList.remove('text-yellow-400');
                        s.classList.add('text-gray-300');
                    }
                });
            });

            // Optional: Hover effect
            star.addEventListener('mouseenter', function () {
                const rating = this.getAttribute('data-rating');
                stars.forEach(s => {
                    if (parseInt(s.getAttribute('data-rating')) <= parseInt(rating)) {
                        s.classList.remove('text-gray-300');
                        s.classList.add('text-yellow-400', 'opacity-70');
                    }
                });
            });

            star.addEventListener('mouseleave', function () {
                // Reset to selected state
                const currentRating = hiddenInput.value || 0;
                stars.forEach(s => {
                    s.classList.remove('opacity-70');
                    if (parseInt(s.getAttribute('data-rating')) <= parseInt(currentRating)) {
                        s.classList.remove('text-gray-300');
                        s.classList.add('text-yellow-400');
                    } else {
                        s.classList.remove('text-yellow-400');
                        s.classList.add('text-gray-300');
                    }
                });
            });
        });
    }
});
