{{-- Products section component
     Usage: @include('themes-manager::components.web.sections.products', ['sectionKey' => 'homepage_products_1'])
--}}
@php
    $ts = themeService()->boot();
    $section = $ts->section($sectionKey ?? '');
    if (!$section) return;

    $settings = $ts->settings($sectionKey);
    $sectionTitle = $ts->trans($sectionKey, 'section_title');
    $sectionSubtitle = $ts->trans($sectionKey, 'section_subtitle');
    $viewAllText = $ts->trans($sectionKey, 'view_all_text');
    $viewAllUrl = $ts->config($sectionKey, 'view_all_url', '');

    $source = $settings['source'] ?? 'latest';
    $categoryId = $settings['category_id'] ?? null;
    $itemsCount = $settings['items_count'] ?? 6;
    $columns = $settings['columns'] ?? 6;
    $showPrice = $settings['show_price'] ?? true;
    $showAddToCart = $settings['show_add_to_cart'] ?? true;

    // Fetch products based on source
    $productsQuery = \ViartasShop\Products\Models\Accounting\Product::visible();

    switch ($source) {
        case 'category':
            if ($categoryId) {
                $productsQuery->inCategory($categoryId);
            }
            break;
        case 'featured':
            // If you have a featured flag, use it here
            $productsQuery->orderByDesc('updated_at');
            break;
        case 'bestselling':
            $productsQuery->orderByDesc('updated_at');
            break;
        case 'latest':
        default:
            $productsQuery->orderByDesc('created_at');
            break;
    }

    $products = $productsQuery->limit($itemsCount)->get();
    $colClass = 'col-xxl-' . (12 / $columns) . ' col-lg-3 col-md-4 col-6';
@endphp

@if($products->isNotEmpty())
<section class="product-section">
    <div class="container-fluid-lg">
        @if($sectionTitle)
        <div class="title d-block">
            <div>
                <h2>{{ $sectionTitle }}</h2>
                @if($sectionSubtitle)
                    <span class="title-leaf"><p>{{ $sectionSubtitle }}</p></span>
                @endif
            </div>
            @if($viewAllText && $viewAllUrl)
                <a href="{{ $viewAllUrl }}" class="title-link">{{ $viewAllText }} →</a>
            @endif
        </div>
        @endif

        <div class="row g-sm-4 g-3">
            @foreach($products as $product)
                <div class="{{ $colClass }}">
                    <div class="product-box-3 h-100 wow fadeInUp">
                        <div class="product-header">
                            <div class="product-image">
                                <a href="{{ $product->getRoute() ?? '#' }}">
                                    @if($product->getFirstMediaUrl('default', 'category_preview'))
                                        <img src="{{ $product->getFirstMediaUrl('default', 'category_preview') }}"
                                             class="img-fluid blur-up lazyload" alt="{{ $product->title }}">
                                    @else
                                        <img src="{{ theme()->publicPath('images/placeholder.png') }}"
                                             class="img-fluid blur-up lazyload" alt="{{ $product->title }}">
                                    @endif
                                </a>
                            </div>
                        </div>
                        <div class="product-footer">
                            <div class="product-detail">
                                <a href="{{ $product->getRoute() ?? '#' }}">
                                    <h5 class="name">{{ $product->title }}</h5>
                                </a>
                                @if($showPrice && $product->price)
                                    <h5 class="price">
                                        <span class="theme-color">{{ $product->formatted_price }}</span>
                                    </h5>
                                @endif
                            </div>
                            @if($showAddToCart)
                            <div class="add-to-cart-box">
                                <button class="btn btn-add-cart addcart-button" data-product-id="{{ $product->id }}">
                                    {{ __('globals.add_to_cart') }}
                                    <span class="add-icon"><i class="fa-solid fa-plus"></i></span>
                                </button>
                            </div>
                            @endif
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    </div>
</section>
@endif
