{{-- Deal Box Modal — weekly deals from configurable category --}}
@php
    $ts = themeService()->boot();
    $dealSettings = $ts->settings('deal_box') ?? [];
    $dealCategoryId = $dealSettings['category_id'] ?? null;
    $dealItemsCount = $dealSettings['items_count'] ?? 8;

    $dealProducts = collect();
    if ($dealCategoryId) {
        $dealCategory = \ViartasShop\ProductCategories\Models\Accounting\ProductCategory::find($dealCategoryId);
        if ($dealCategory) {
            $categoryIds = $dealCategory->descendantsAndSelf()->pluck('id')->toArray();
            $dealProducts = \ViartasShop\Products\Models\Accounting\Product::where('is_visible', true)
                ->where(function ($q) use ($categoryIds) {
                    $q->whereIn('product_category_id', $categoryIds)
                      ->orWhereHas('additionalCategories', function ($q2) use ($categoryIds) {
                          $q2->whereIn('product_categories.id', $categoryIds);
                      });
                })
                ->with(['media'])
                ->orderByDesc('created_at')
                ->limit($dealItemsCount)
                ->get();
        }
    }
@endphp

<div class="modal fade theme-modal deal-modal" id="deal-box" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-fullscreen-sm-down">
        <div class="modal-content">
            <div class="modal-header">
                <div>
                    <h5 class="modal-title w-100" id="deal_today">{{ __('checkout.week_deals') }}</h5>
                    <p class="mt-1 text-content">{{ __('checkout.recommended_deals') }}</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal">
                    <i class="fa-solid fa-xmark"></i>
                </button>
            </div>
            <div class="modal-body">
                <div class="deal-offer-box">
                    <ul class="deal-offer-list">
                        @forelse($dealProducts as $product)
                            <li class="list-{{ ($loop->index % 3) + 1 }}">
                                <div class="deal-offer-contain">
                                    <a href="{{ $product->getRoute() ?? '#' }}" class="deal-image">
                                        @if($product->mainImageUrl('card-3x4'))
                                            <img src="{{ $product->mainImageUrl('card-3x4') }}"
                                                 class="blur-up lazyload" alt="{{ $product->title }}">
                                        @else
                                            <img src="{{ theme()->publicPath('images/placeholder.png') }}"
                                                 class="blur-up lazyload" alt="{{ $product->title }}">
                                        @endif
                                    </a>

                                    <a href="{{ $product->getRoute() ?? '#' }}" class="deal-contain">
                                        <h5>{{ $product->title }}</h5>
                                        <h6>{{ $product->price->formatAsPrice() }}</h6>
                                    </a>
                                </div>
                            </li>
                        @empty
                            <li>
                                <div class="deal-offer-contain">
                                    <p class="text-content text-center w-100 py-3">{{ __('globals.no_products') }}</p>
                                </div>
                            </li>
                        @endforelse
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
