{# --- social/meta_product.html --- #}
{# Context expected:
   site, d (Product), request
#}

{# Visibility → robots: index unless soft-deleted #}
{% set robots_here = 'noindex,nofollow' if d.deleted_at else 'index,follow' %}

{# Compute canonical #}
{% set _site_url = site().url.rstrip('/') if site and site().url else '' %}
{% set _product_path = request.path if request else ('/products/' ~ (d.slug or '')) %}
{% set canonical = _site_url ~ _product_path if _site_url else (request.url if request else None) %}

{# Include common base (charset, viewport, robots default, RSS, Org/WebSite JSON-LD) #}
{% set robots_override = robots_here %}
{% include 'social/meta_base.html' %}

{# ---- Titles / descriptions ---- #}
{% set base_product_title = d.title or base_title %}
{% set og_title = base_product_title %}
{% set tw_title = base_product_title %}

{# Description: prefer short, then HTML stripped #}
{% set desc_source = d.description_short
                     or (d.description_html|striptags if d.description_html else '') %}
{% set description = (desc_source|trim|replace('\n',' ')|replace('\r',' ')|striptags)|truncate(160, True, '…') %}

{# ---- Image priority: product image, then first gallery image, then site default ---- #}
{% set image_url = d.image
                   or ((d.images|first).url if d.images and (d.images|first).url else None)
                   or (site().default_image if site and site().default_image else None) %}

{# ---- Price / offer helpers ---- #}
{% set price = d.special_price or d.regular_price or d.rrp %}
{% set price_currency = d.special_price_currency or d.regular_price_currency or d.rrp_currency %}

{# ---- Basic meta ---- #}
<title>{{ base_product_title }}</title>
<meta name="description" content="{{ description }}">
{% if canonical %}<link rel="canonical" href="{{ canonical }}">{% endif %}

{# ---- Open Graph ---- #}
<meta property="og:site_name" content="{{ site().title if site and site().title else '' }}">
<meta property="og:type" content="product">
<meta property="og:title" content="{{ og_title }}">
<meta property="og:description" content="{{ description }}">
{% if canonical %}<meta property="og:url" content="{{ canonical }}">{% endif %}
{% if image_url %}<meta property="og:image" content="{{ image_url }}">{% endif %}

{# Optional product OG price tags #}
{% if price and price_currency %}
  <meta property="product:price:amount" content="{{ '%.2f'|format(price) }}">
  <meta property="product:price:currency" content="{{ price_currency }}">
{% endif %}
{% if d.brand %}
  <meta property="product:brand" content="{{ d.brand }}">
{% endif %}
{% if d.sku %}
  <meta property="product:retailer_item_id" content="{{ d.sku }}">
{% endif %}

{# ---- Twitter ---- #}
<meta name="twitter:card" content="{{ 'summary_large_image' if image_url else 'summary' }}">
{% if site and site().twitter_site %}<meta name="twitter:site" content="{{ site().twitter_site }}">{% endif %}
<meta name="twitter:title" content="{{ tw_title }}">
<meta name="twitter:description" content="{{ description }}">
{% if image_url %}<meta name="twitter:image" content="{{ image_url }}">{% endif %}

{# ---- JSON-LD Product ---- #}
{% set jsonld = {
  "@context": "https://schema.org",
  "@type": "Product",
  "name": d.title,
  "image": image_url,
  "description": description,
  "sku": d.sku,
  "brand": d.brand,
  "url": canonical
} %}

{# Brand as proper object if present #}
{% if d.brand %}
  {% set jsonld = jsonld | combine({
    "brand": {
      "@type": "Brand",
      "name": d.brand
    }
  }) %}
{% endif %}

{# Offers if price available #}
{% if price and price_currency %}
  {% set jsonld = jsonld | combine({
    "offers": {
      "@type": "Offer",
      "price": price,
      "priceCurrency": price_currency,
      "url": canonical,
      "availability": "https://schema.org/InStock"
    }
  }) %}
{% endif %}

<script type="application/ld+json">
{{ jsonld | tojson }}
</script>
