{% 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 : viti_theme_asset('images/logo_600x450.jpg'))
%}
{% set showPrices = config.shouldShowPrices(app.session.get('partnerCode')) %}
{% set schemaOffers = [] %}
{% set hasInStockOffer = false %}
{% if showPrices %}
    {% for variant in product.variants %}
        {% set variantPrice = activePriceGroupId ? variant.getPriceByGroupId(activePriceGroupId) : null %}
        {% if 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,
                seller: {
                    '@type': 'Organization',
                    name: 'company'|trans
                }
            } %}
            {% 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 %}
{% endif %}

{% 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)),
    brand: {
        '@type': 'Brand',
        name: 'company'|trans
    },
    sku: product.sku
} %}
{% if showPrices %}
    {% set schemaProduct = schemaProduct|merge({
        availability: 'https://schema.org/' ~ (hasInStockOffer ? 'InStock' : 'OutOfStock'),
        offers: schemaOffers
    }) %}
{% 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',
        seo_image_url: absolute_url(asset(productImagePath)),
        seo_image_alt: product.fullName ?: product.name|striptags
    }) }}
    <script type="application/ld+json">{{ schemaProduct|json_encode|raw }}</script>
{% endblock meta %}

{% block stylesheets %}
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Libre+Bodoni:opsz,wght@6..96,400..900&family=Libre+Franklin:wght@100..900&family=Material+Symbols+Outlined:wght@100..700&display=swap">
{% endblock %}

{% block header %}{% endblock %}

{% block main %}
    <turbo-frame id="product-cuvee" class="block" data-turbo-action="advance">
        <header class="theme-product-header">
            {% include 'partials/_nav.html.twig' %}
        </header>

        <div class="theme-product-page overflow-hidden bg-background font-body text-base text-primary selection:bg-secondary/20 selection:text-primary">
            <section data-theme-product-navigation class="relative overflow-hidden bg-background pt-8 pb-8 md:pt-10 md:pb-10">
                <div class="pointer-events-none absolute inset-x-0 top-0 h-[32rem] bg-[radial-gradient(circle_at_top,rgba(173,156,118,0.08),transparent_60%)]"></div>

                <div class="relative z-10 mx-auto w-full max-w-[1440px] px-4 md:px-16">
                    {{ component('shop:ProductNavigation', { product: product }) }}
                </div>
            </section>

            <section class="mx-auto max-w-[1440px] px-4 pb-16 md:px-16 md:pb-20">
                {{ component('shop:Vacation', { config: config, class: 'mb-8' }) }}
                {{ component('shop:ProductDetailForm', { product: product }) }}
                {{ component('shop:LegalNotice', { class: 'mt-16' }) }}
                <twig:marketing:ProductReviews :product="product" />
            </section>
        </div>
    </turbo-frame>
{% endblock %}
