{% set category = this.category %}
{% set schemaHasVisibleCategory = this.showCategoryMenu and category|default(null) and this.currentCategoryVisible %}
{% set schemaPageName = schemaHasVisibleCategory ? category.name : 'shop.title'|trans %}
{% set schemaRouteParameters = schemaHasVisibleCategory ? { slug: category.slug } : {} %}
{% set localizedRoutes = localized_page_routes('shop', schemaRouteParameters) %}
{% set schemaPageUrlFallback = schemaHasVisibleCategory ? absolute_url(path('shop', { slug: category.slug })) : absolute_url(path('shop')) %}
{% set schemaPageUrl = localizedRoutes.current.absoluteUrl|default(schemaPageUrlFallback) %}
{% set schemaShopProducts = [] %}
{% set showPrices = this.shouldShowPrices %}
{% set businessName = contact_business_name() %}

{% for product in this.schemaProducts %}
    {% set productPosition = loop.index %}
    {% set productUrl = this.productPageEnabled and product.category and product.category.slug and product.slug
        ? absolute_url(path('shop_product', { categorySlug: product.category.slug, slug: product.slug }))
        : schemaPageUrl ~ '#' ~ product.slug
    %}
    {% set productImagePath = product.secondImage and product.secondImage.path
        ? product.secondImage.path
        : (product.image and product.image.path ? product.image.path : null)
    %}
    {% set productDescription = (product.description|default('')|striptags|trim) ?: (product.quality|default('')|striptags|trim) %}
    {% set schemaOffers = [] %}
    {% set hasInStockOffer = false %}

    {% for variant in product.variants %}
        {% set variantPrice = this.activePriceGroupId ? variant.getPriceByGroupId(this.activePriceGroupId) : null %}
        {% if showPrices and not variant.unPublished and variantPrice %}
            {% set isOfferOutOfStock = product.soldOut or variant.soldOut or not variant.available %}
            {% set schemaOffer = {
                '@type': 'Offer',
                url: productUrl,
                priceCurrency: 'EUR',
                price: (variantPrice.finalPrice / 100)|number_format(2, '.', ''),
                availability: 'https://schema.org/' ~ (isOfferOutOfStock ? 'OutOfStock' : 'InStock'),
                itemCondition: 'https://schema.org/NewCondition',
                name: variant.fullName,
                description: variant.label
            } %}
            {% if businessName %}
                {% set schemaOffer = schemaOffer|merge({ seller: {
                    '@type': 'Organization',
                    name: businessName
                } }) %}
            {% endif %}
            {% if product.sku %}
                {% set schemaOffer = schemaOffer|merge({ sku: product.sku }) %}
            {% endif %}
            {% if variant.stock is not null %}
                {% set schemaOffer = schemaOffer|merge({
                    inventoryLevel: {
                        '@type': 'QuantitativeValue',
                        value: variant.stock
                    }
                }) %}
            {% endif %}
            {% set schemaOffers = schemaOffers|merge([schemaOffer]) %}
            {% if not isOfferOutOfStock %}
                {% set hasInStockOffer = true %}
            {% endif %}
        {% endif %}
    {% endfor %}

    {% set schemaProduct = {
        '@type': 'Product',
        name: product.fullName ?: product.name,
        description: productDescription,
        category: product.category ? product.category.name : (category ? category.name : null),
        url: productUrl,
        availability: 'https://schema.org/' ~ (hasInStockOffer ? 'InStock' : 'OutOfStock'),
        sku: product.sku,
        offers: showPrices ? schemaOffers : []
    } %}
    {% if businessName %}
        {% set schemaProduct = schemaProduct|merge({ brand: {
            '@type': 'Brand',
            name: businessName
        } }) %}
    {% endif %}
    {% if productImagePath %}
        {% set schemaProduct = schemaProduct|merge({ image: asset_glide(productImagePath, 'image', {
            fm: 'jpg',
            q: 80,
            fit: 'contain',
            bg: 'ffffff',
            w: 1200,
            h: 1200
        }) }) %}
    {% endif %}
    {% set schemaShopProducts = schemaShopProducts|merge([{
        '@type': 'ListItem',
        position: productPosition,
        url: productUrl,
        item: schemaProduct
    }]) %}
{% endfor %}

{% if schemaShopProducts|length > 0 %}
    <script type="application/ld+json">{{ {
        '@context': 'https://schema.org',
        '@type': 'CollectionPage',
        name: schemaPageName,
        url: schemaPageUrl,
        mainEntity: {
            '@type': 'ItemList',
            name: schemaPageName,
            itemListElement: schemaShopProducts
        }
    }|json_encode|raw }}</script>
{% endif %}
