@php
    $showDiscount = $showDiscount ?? false;
    $inWishlist = false;
    $effectiveShippingDays = $product->shipping_days ?? \App\Models\Setting::get('default_shipping_days');
    if (auth()->check()) {
        // Use cached wishlist product IDs to avoid N+1 queries
        $wishlistProductIds = \App\Helpers\ProductHelper::getWishlistProductIds();
        $inWishlist = $wishlistProductIds->contains($product->id);
    }
@endphp
<div class="product-card">
    <div class="product-card-inner">
        @if($product->discount_percentage && $showDiscount)
            <span class="discount-badge">-{{ round($product->discount_percentage) }}%</span>
        @endif
        <div class="product-image-wrapper">
            <a href="{{ route('product.show', $product->slug) }}" class="product-image-link">
                @if($product->image_path || $product->image)
                    <x-product-image :product="$product" context="listing" class="product-image" onerror="this.onerror=null; this.style.display='none'; this.nextElementSibling.style.display='flex';" />
                    <div class="product-placeholder" style="display: none;">
                        <i class="bi bi-image"></i>
                    </div>
                @else
                    <div class="product-placeholder">
                        <i class="bi bi-image"></i>
                    </div>
                @endif
            </a>
            @auth
                <form action="{{ route('wishlist.toggle', $product->id) }}" method="POST" class="d-inline wishlist-form" data-product-id="{{ $product->id }}">
                    @csrf
                    <button type="submit" class="action-btn wishlist-btn {{ $inWishlist ? 'in-wishlist' : '' }}"
                            title="{{ $inWishlist ? 'Remove from Wishlist' : 'Add to Wishlist' }}">
                        <i class="bi bi-heart{{ $inWishlist ? '-fill' : '' }}"></i>
                    </button>
                </form>
            @else
                <a href="{{ route('login') }}" class="btn action-btn wishlist-form wishlist-btn" title="Login to Add to Wishlist">
                    <i class="bi bi-heart"></i>
                </a>
            @endauth
            @if($product->is_out_of_stock)
                <div class="stockout-badge-overlay" role="status" aria-label="{{ __('Out of stock') }}">
                    <span class="stockout-text">Stockout</span>
                </div>
            @endif
        </div>
        <div class="product-info">
            <h6 class="product-title">
                <a href="{{ route('product.show', $product->slug) }}">{{ strtoupper($product->name) }}</a>
            </h6>
            @if($product->is_out_of_stock)
                <div class="product-stockout-label text-danger fw-semibold small mb-1" role="status">{{ __('Stockout') }}</div>
            @endif
            
            <!-- Rating Display -->
            <div class="product-rating mb-2">
                <div class="d-flex align-items-center gap-1">
                    @if($product->rating_count > 0)
                        <x-star-rating :rating="$product->avg_rating" size="sm" />
                        <span class="rating-text">
                            <strong class="rating-value">{{ number_format($product->avg_rating, 1) }}</strong>
                            <span class="rating-count">({{ $product->rating_count }})</span>
                        </span>
                    @else
                        <div class="star-rating text-sm">
                            @for($i = 1; $i <= 5; $i++)
                                <i class="bi bi-star" style="font-size: 0.875rem; color: #ffc107; opacity: 0.4;"></i>
                            @endfor
                        </div>
                    @endif
                </div>
            </div>
            
            <div class="product-price">
                @if($product->product_type === 'variant' && $product->variants && $product->variants->isNotEmpty())
                    @php
                        $activeVariants = $product->variants->where('is_active', true);
                        $variantPrices = $activeVariants->pluck('price')->filter();
                        $minPrice = $variantPrices->min();
                        $maxPrice = $variantPrices->max();
                        $variantMrps = $activeVariants->pluck('mrp')->filter();
                        $minMrp = $variantMrps->min();
                        $maxMrp = $variantMrps->max();
                    @endphp
                    @if($minPrice && $maxPrice && $maxPrice === $minPrice)
                        <div class="current-price">₹{{ number_format($minPrice, 0)}}</div>
                    @else
                        <div class="current-price">₹{{ number_format($minPrice, 0) }} - ₹{{ number_format($maxPrice, 0) }}</div>
                    @endif
                    @if($minMrp && $maxMrp && $minMrp > $minPrice)
                        <!-- <div class="original-price">₹{{ number_format($minMrp, 0) }} - ₹{{ number_format($maxMrp, 0) }}</div> -->
                    @endif
                @else
                    <span class="current-price">₹{{ number_format($product->price, 0) }}</span>
                    @if($product->mrp && $product->mrp > $product->price)
                        <span class="original-price">₹{{ number_format($product->mrp, 0) }}</span>
                    @endif
                @endif
            </div>
            @if(!is_null($effectiveShippingDays) && $effectiveShippingDays !== '')
                <div class="mt-1 text-muted" style="font-size: 12px;">
                    <i class="bi bi-truck"></i> Ships in {{ $effectiveShippingDays }} day{{ (int) $effectiveShippingDays === 1 ? '' : 's' }}
                </div>
            @endif
            @if($product->product_type === 'variant')
               <a href="{{ route('product.show', $product->slug) }}" 
                  class="add-to-cart-btn mt-2">
                  Select Options
                 </a>
            @else
           <form action="{{ route('cart.store') }}" method="POST" class="add-to-cart-form">
           @csrf
             <input type="hidden" name="product_id" value="{{ $product->id }}">
            <input type="hidden" name="qty" value="1">
            <button type="submit" class="add-to-cart-btn mt-2">
             <i class="bi bi-bag"></i> Add to Cart
           </button>
           </form>
       @endif
        </div>
    </div>
</div>

