@extends('theme::layouts.web')

{{--
    ProductCategory landing — Protera theme override.

    Replaces the standalone Modules/ProductCategories/Resources/views/web/default.blade.php
    fallback. Inherits topbar + site-header + site-footer + Vite-built CSS
    from theme::layouts.web.

    v18.10 (task-18 CatalogBrowsing) — when the `$listing` array is
    present (CatalogBrowsing module installed), renders the full
    listing+filter UX. Falls back to the legacy `$models` grid when
    CatalogBrowsing is missing (no module shipping the listing service,
    or its boot listener crashed and `$listing` is null).

    Vars (passed by ProductCategorySlugResolver::resolve()):
      - $category       ProductCategory
      - $listing        array|null  (task-18: full listing response)
      - $models         Paginator | Collection  (legacy fallback)
      - $currentLocale  string
      - $defaultLocale  string
      - $locales        Collection<Locale>

    Test markers preserved:
      [data-product-category-title], [data-product-list],
      [data-product-list-empty], [data-product-card]

    Microdata wraps the category meta only — individual product cards on
    the listing are NOT wrapped (each product gets its own microdata
    article on its own URL — Schema.org best practice).
--}}

@section('title', $category->getTitle())
@section('description', $category->getTranslation('excerpt', $currentLocale) ?: '')

@section('content')
<main class="container py-5">
    <x-microdata::attributes :for="$category" tag="section" class="cms-product-category">
        <header class="mb-4">
            <h1 class="mb-2" data-product-category-title>
                <x-microdata::property
                    name="name"
                    :for="$category"
                    :value="$category->getTitle()" />
            </h1>

            @if ($category->getTranslation('excerpt', $currentLocale))
                <p class="lead text-muted mb-0">
                    <x-microdata::property
                        name="description"
                        :for="$category"
                        :value="$category->getTranslation('excerpt', $currentLocale)" />
                </p>
            @endif
        </header>

        @if ($category->getTranslation('content', $currentLocale))
            <div class="cms-product-category-content mb-4">
                {!! $category->getTranslation('content', $currentLocale) !!}
            </div>
        @endif
    </x-microdata::attributes>

    @if(!empty($listing) && is_array($listing))
        {{-- Task-18 — full CatalogBrowsing layout (sidebar + grid + chips + pagination) --}}
        @include('catalogbrowsing::web._catalog-listing', [
            'listing' => $listing,
            'currentLocale' => $currentLocale,
        ])
    @else
        {{-- Legacy fallback grid — kept for the rare case where the
             CatalogBrowsing module isn't installed (test env, partial
             deploy) — the category is still navigable. --}}
        <hr>
        @include('catalogbrowsing::web._listing-grid', [
            'items' => $models,
            'currentLocale' => $currentLocale,
        ])
        @if($models instanceof \Illuminate\Contracts\Pagination\Paginator)
            <div class="d-flex justify-content-center mt-4">
                {{ $models->withQueryString()->links() }}
            </div>
        @endif
    @endif

    {{-- Task-17 CategoryRelations: cross-sell grid pointing to related
         ProductCategories. Includes only renders when at least one
         active relation_type=related row exists for this category. --}}
    @includeIf('categoryrelations::web._related-categories-grid', [
        'category' => $category,
        'currentLocale' => $currentLocale,
    ])
</main>
@endsection
