<div class="review-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
    <h3 style="font-family: 'Montserrat', sans-serif;font-weight: 450">Customer Reviews</h3>

      @php
        $user = Auth::user();
        $orderExist = false;

       $allreview = \App\Models\Review::where('product_id', $item->id)
    ->where('status', 1)
    ->get();

$allreviewcount = $allreview->count();
$allreviewstarsum = $allreview->sum('star_level');

// Avoid division by zero
$averageRating = $allreviewcount > 0 ? round($allreviewstarsum / $allreviewcount, 1) : 0;
        if ($user) {
            $orderExist = \App\Models\Order::where('userid', $user->id)->where('product_id', $item->id)->exists();
            $reviewExist = \App\Models\Review::where('user_id', $user->id)->where('product_id', $item->id)->exists();
        }
    @endphp
    <!-- Average Rating Display -->
    <div class="average-rating" style="margin: 15px 0; display: flex; align-items: center;">
        <div style="font-size: 28px; font-weight: bold; margin-right: 10px;">{{ number_format($averageRating ?? 5.0, 1) }}
</div>
        <div style="font-size: 20px; color: #ffc107;">
            ★★★★☆
        </div>
        <div style="margin-left: 10px; color: #666;">({{ $allreviewcount ?? 0 }} reviews)</div>
    </div>

    <!-- Review Form -->

    @if ($orderExist && !$reviewExist)
        <div class="review-form-container"
            style="background: #f9f9f9; padding: 20px; border-radius: 5px; margin-bottom: 30px;">
            <h4>Write a Review</h4>
            <form id="review-form" type="submit" action="{{ route('review.store') }}" method="POST">
                @csrf
                <div class="rating-input" style="margin: 15px 0;">
                    <p>Your Rating:</p>
                    <div class="stars" style="font-size: 24px;">
                        <span class="star" data-value="1" style="cursor: pointer;">☆</span>
                        <span class="star" data-value="2" style="cursor: pointer;">☆</span>
                        <span class="star" data-value="3" style="cursor: pointer;">☆</span>
                        <span class="star" data-value="4" style="cursor: pointer;">☆</span>
                        <span class="star" data-value="5" style="cursor: pointer;">☆</span>
                    </div>
                    <input type="hidden" id="rating-value" name="rating" value="0">
                    <input type="hidden" id="itemid" name="itemid" value="{{ $item->id }}">
                </div>

                <div style="margin-bottom: 15px;">
                    <label for="review-text" style="display: block; margin-bottom: 5px;">Your Review:</label>
                    <textarea id="review-text" rows="4" style="width: 100%; padding: 8px; border: 1px solid #ddd;" name="review"></textarea>
                </div>

                <button type="button" onclick="submitReviewForm()"
                    style="background: #333; color: white; padding: 5px 15px; border: none; cursor: pointer;">
                    Submit Review
                </button>

                <script>
                    // Handle star click
                    document.querySelectorAll('.star').forEach(star => {
                        star.addEventListener('click', function() {
                            const value = this.getAttribute('data-value');
                            document.getElementById('rating-value').value = value;

                            // Highlight stars
                            document.querySelectorAll('.star').forEach(s => {
                                s.textContent = s.getAttribute('data-value') <= value ? '★' : '☆';
                            });
                        });
                    });

                    // Submit with validation
                    function submitReviewForm() {
                        const rating = parseInt(document.getElementById('rating-value').value);
                        if (rating === 0) {

                            toastr.error("Please select a star rating before submitting your review.!", "Info", {
                                timeOut: 3000,
                                positionClass: "toast-top-right",
                                preventDuplicates: true
                            });
                            return;
                        }

                        document.getElementById('review-form').submit();
                    }
                </script>

            </form>
        </div>
    @elseif ($user && $reviewExist)
        <div class="review-submitted-message"
            style="background: #f9f9f9; padding: 20px; border-radius: 5px; margin-bottom: 30px;">
            <p style="color: green; font-weight: bold;">Your review was submitted</p>
        </div>
    @else

    @endif

    @php
        $reviews = \App\Models\Review::where('product_id', $item->id)->where('status',1)->get();
        $reviewCount = $reviews->count();
         $reviewspag = \App\Models\Review::where('product_id', $item->id)->where('status',1)->paginate(3);
    @endphp
    <!-- Reviews List -->
    <div class="reviews-list">
        <h4>Customer Reviews</h4>

        <!-- Review Sorting -->
        <div class="review-sorting"
            style="margin: 15px 0; display: flex; justify-content: space-between; align-items: center;">
            <div>{{ $reviewCount ?? 0 }} reviews</div>

        </div>

        <!-- Review Items -->

      <div class="review-items">
    @if ($reviews->count() > 0)
        @foreach ($reviewspag as $review)
            <div class="review-item" style="padding: 15px 0; border-bottom: 1px solid #eee;">
                <div style="display: flex; justify-content: space-between; margin-bottom: 5px;">
                    <div style="font-weight: bold;">{{ $review->userData->name ?? 'User not found' }}</div>
                    <div style="color: #666; font-size: 14px;">{{ $review->created_at->format('F d, Y') }}</div>
                </div>
                <div style="color: #ffc107; font-size: 16px; margin: 5px 0;">
                    @for ($i = 1; $i <= 5; $i++)
                        @if ($i <= $review->star_level)
                            ★
                        @else
                            ☆
                        @endif
                    @endfor
                </div>
                <p style="margin: 10px 0;">{{ $review->review }}</p>
            </div>
        @endforeach

        <!-- Pagination Links -->
        <div class="pagination" style="margin-top: 20px; display: flex; justify-content: center;">
            {{ $reviewspag->links() }}
        </div>

    @else
        <p>No reviews available for this product.</p>
    @endif
</div>

        <!-- Pagination -->

    </div>
</div>
