@php
    /** @var \App\Models\HomeSection $section */
    $menu = $section->menu;
@endphp

@if($menu)
    @php
        // প্রতি সেকশনে কতগুলো product দেখাবে
        $limit = $section->product_limit ?? 8;

        $products = \App\Models\Product::where('category_id', $menu->id)
            ->latest()
            ->take($limit)
            ->get();
    @endphp

    @if($products->isNotEmpty())
        <section class="home-block">
            <div class="container">
                <div class="home-block-header d-flex justify-content-between align-items-center flex-wrap gap-2">
                    <div>
                        <h2 class="home-block-title">
                            {{ $section->title ?: $menu->name }}
                        </h2>

                        @if($section->subtitle)
                            <p class="home-block-subtitle">
                                {{ $section->subtitle }}
                            </p>
                        @endif

                        {{-- Subcategory list --}}
                        @if($menu->submenus && $menu->submenus->count())
                            <div class="home-block-submenus">
                                @foreach($menu->submenus as $submenu)
                                    <a href="{{ route('menu.products', ['name' => $submenu->name, 'slug' => $submenu->slug]) }}"
                                       class="home-submenu-link">
                                        {{ $submenu->name }}
                                    </a>
                                @endforeach
                            </div>
                        @endif
                    </div>

                    <div class="mt-2 mt-md-0">
                        <a href="{{ route('main-menu-products', $menu->slug) }}"
                           class="btn btn-sm btn-outline-primary home-block-more">
                            সব দেখুন
                        </a>
                    </div>
                </div>

                <div class="row g-3 g-md-4">
                    @foreach($products as $product)
                        @php
                            $hasDiscount = !is_null($product->discount_price) && $product->discount_price > 0;
                            $basePrice   = $product->price ?: 0;
                            $finalPrice  = $hasDiscount ? $product->discount_price : $basePrice;
                            $discountPercent = $hasDiscount && $basePrice > 0
                                ? round((($basePrice - $product->discount_price) / $basePrice) * 100)
                                : null;

                            // Image URL – storage path ধরে
                            $thumb = $product->thumbnail
                                ? asset('storage/'.$product->thumbnail)
                                : asset('assets/img/placeholder.png');
                        @endphp

                        <div class="col-6 col-md-3 col-lg-2">
                            <a href="{{ route('details', $product->slug) }}" class="home-product-card-link">
                                <div class="home-product-card h-100 d-flex flex-column">
                                    <div class="home-product-img-wrap">
                                        <img src="{{ $thumb }}"
                                             alt="{{ $product->alt }}"
                                             class="img-fluid">
                                        @if($discountPercent)
                                            <span class="home-discount-badge">{{ $discountPercent }}% OFF</span>
                                        @endif
                                    </div>
                                    <div class="home-product-body">
                                        <p class="home-product-name mb-1">
                                            {{ Str::limit($product->name, 40) }}
                                        </p>
                                        <div class="home-product-price-wrap">
                                            <span class="home-price-main">
                                                ৳ {{ number_format($finalPrice, 0) }}
                                            </span>
                                            @if($hasDiscount)
                                                <span class="home-price-old">
                                                    ৳ {{ number_format($basePrice, 0) }}
                                                </span>
                                            @endif
                                        </div>
                                        <span class="home-price-tag">In Stock</span>
                                    </div>
                                </div>
                            </a>
                        </div>
                    @endforeach
                </div>
            </div>
        </section>
    @endif
@endif
