{% extends 'base.html.twig' %}

{% set heroPath = product.backgroundImage and product.backgroundImage.path ? product.backgroundImage.path : 'images/header/shop.jpg' %}
{% set heroWidth = product.backgroundImage and product.backgroundImage.width ? product.backgroundImage.width : 1800 %}
{% set heroHeight = product.backgroundImage and product.backgroundImage.height ? product.backgroundImage.height : 1177 %}
{% set seoTitle = (product.fullName ?: product.name) ~ ' - ' ~ ('shop.title'|trans({}, 'meta')) %}
{% set seoDescription = (product.description|default('')|striptags|trim) ?: ('shop.description'|trans({}, 'meta')) %}
{% set localizedRoutes = localized_page_routes() %}
{% set productUrl = localizedRoutes.current.absoluteUrl|default(app.request.uri) %}
{% set productImagePath = product.secondImage and product.secondImage.path
    ? product.secondImage.path
    : (product.image and product.image.path ? product.image.path : 'images/logo-meta.jpg')
%}
{% set showPrices = config.shouldShowPrices(app.session.get('partnerCode')) %}
{% set businessName = contact_business_name() %}
{% set schemaOffers = [] %}
{% set hasInStockOffer = false %}
{% for variant in product.variants %}
    {% set variantPrice = activePriceGroupId ? variant.getPriceByGroupId(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 = {
    '@context': 'https://schema.org',
    '@type': 'Product',
    name: product.fullName ?: product.name,
    description: seoDescription,
    category: category.name,
    url: productUrl,
    image: absolute_url(asset(productImagePath)),
    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 %}
{% set schemaAggregateRating = product_review_aggregate_rating(product) %}
{% if schemaAggregateRating %}
    {% set schemaProduct = schemaProduct|merge({ aggregateRating: schemaAggregateRating }) %}
{% endif %}

{% block title %}{{ seoTitle }}{% endblock %}

{% block meta %}
    {{ include('partials/seo.html.twig', {
        title_key: 'shop.title',
        description_key: 'shop.description',
        title: seoTitle,
        description: seoDescription,
        robots: 'index,follow',
        og_type: 'product'
    }) }}
    <script type="application/ld+json">{{ schemaProduct|json_encode|raw }}</script>
{% endblock meta %}

{% block header_top %}
    <twig:Image
        mapping="public"
        class="peer dark fixed -z-20 h-screen max-h-svh w-full overflow-hidden bg-transparent"
        picture:class="h-full w-full"
        img:class="min-safe-h-screen min-h-screen h-screen w-full object-cover"
        lazy="{{ false }}"
        quality="{{ 60 }}"
        responsive="{{ true }}"
        path="{{ heroPath }}"
        imageWidth="{{ heroWidth }}"
        imageHeight="{{ heroHeight }}" />
{% endblock %}

{% block main %}
    <twig:shop:ProductDetail :product="product" :category="category" :config="config" />
    <twig:marketing:ProductReviews :product="product" />
{% endblock %}
