{%- comment -%}
  JSON-LD structured data for SEO / AI crawlers.
  Emits Organization + WebSite on every page; adds Product/Article/BreadcrumbList
  contextually based on template. Shopify strips line breaks in rendered JSON so
  each block is a single flat object — schema.org validates, Google Rich Results
  Test passes.
{%- endcomment -%}

{%- assign _site_name = shop.name | escape -%}
{%- assign _site_url = shop.url -%}
{%- assign _logo_url = '' -%}
{%- if settings.logo != blank -%}
  {%- assign _logo_url = settings.logo | image_url: width: 600 | prepend: 'https:' -%}
{%- endif -%}

{%- assign _social_urls = '' -%}
{%- if settings.social_instagram != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_instagram | append: '|' -%}{%- endif -%}
{%- if settings.social_tiktok != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_tiktok | append: '|' -%}{%- endif -%}
{%- if settings.social_pinterest != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_pinterest | append: '|' -%}{%- endif -%}
{%- if settings.social_facebook != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_facebook | append: '|' -%}{%- endif -%}
{%- if settings.social_twitter != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_twitter | append: '|' -%}{%- endif -%}
{%- if settings.social_youtube != blank -%}{%- assign _social_urls = _social_urls | append: settings.social_youtube | append: '|' -%}{%- endif -%}
{%- assign _social_urls = _social_urls | split: '|' -%}

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Organization",
  "name": "{{ _site_name }}",
  "url": "{{ _site_url }}"
  {%- if _logo_url != blank -%}
  ,"logo": "{{ _logo_url }}"
  {%- endif -%}
  {%- if _social_urls.size > 0 -%}
  ,"sameAs": [
    {%- for _url in _social_urls -%}
    "{{ _url }}"{%- unless forloop.last -%},{%- endunless -%}
    {%- endfor -%}
  ]
  {%- endif -%}
}
</script>

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "WebSite",
  "name": "{{ _site_name }}",
  "url": "{{ _site_url }}",
  "potentialAction": {
    "@type": "SearchAction",
    "target": "{{ _site_url }}/search?q={search_term_string}",
    "query-input": "required name=search_term_string"
  }
}
</script>

{%- if template.name contains 'product' and product -%}
  {%- assign _product_image = product.featured_image | image_url: width: 1200 | prepend: 'https:' -%}
  {%- assign _product_desc = product.description | strip_html | truncate: 500 | escape -%}
  {%- assign _product_url = shop.url | append: product.url -%}
  {%- assign _price_min = product.price_min | divided_by: 100.0 -%}
  {%- assign _price_max = product.price_max | divided_by: 100.0 -%}
  {%- assign _availability = 'https://schema.org/OutOfStock' -%}
  {%- if product.available -%}{%- assign _availability = 'https://schema.org/InStock' -%}{%- endif -%}

  <script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "Product",
    "name": "{{ product.title | escape }}",
    "description": "{{ _product_desc }}",
    "image": "{{ _product_image }}",
    "url": "{{ _product_url }}",
    "sku": "{{ product.selected_or_first_available_variant.sku | default: product.id | escape }}",
    "brand": { "@type": "Brand", "name": "{{ product.vendor | default: _site_name | escape }}" },
    "offers": {
      "@type": "{% if product.price_min == product.price_max %}Offer{% else %}AggregateOffer{% endif %}",
      "priceCurrency": "{{ cart.currency.iso_code | default: 'USD' }}",
      {%- if product.price_min == product.price_max -%}
        "price": "{{ _price_min }}",
      {%- else -%}
        "lowPrice": "{{ _price_min }}",
        "highPrice": "{{ _price_max }}",
        "offerCount": {{ product.variants.size }},
      {%- endif -%}
      "availability": "{{ _availability }}",
      "url": "{{ _product_url }}"
    }
    {%- if product.metafields.reviews.rating and product.metafields.reviews.rating_count -%}
    ,"aggregateRating": {
      "@type": "AggregateRating",
      "ratingValue": "{{ product.metafields.reviews.rating }}",
      "reviewCount": "{{ product.metafields.reviews.rating_count }}"
    }
    {%- endif -%}
  }
  </script>
{%- endif -%}

{%- if template.name contains 'article' and article -%}
  {%- assign _article_image = article.image | image_url: width: 1200 | prepend: 'https:' -%}
  <script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": "{{ article.title | escape }}",
    "image": "{{ _article_image }}",
    "datePublished": "{{ article.published_at | date: '%Y-%m-%dT%H:%M:%S%z' }}",
    "dateModified": "{{ article.updated_at | date: '%Y-%m-%dT%H:%M:%S%z' }}",
    "author": { "@type": "Person", "name": "{{ article.author | escape }}" },
    "publisher": {
      "@type": "Organization",
      "name": "{{ _site_name }}"
      {%- if _logo_url != blank -%},"logo": { "@type": "ImageObject", "url": "{{ _logo_url }}" }{%- endif -%}
    },
    "description": "{{ article.excerpt_or_content | strip_html | truncate: 500 | escape }}",
    "mainEntityOfPage": "{{ shop.url }}{{ article.url }}"
  }
  </script>
{%- endif -%}

{%- comment -%} BreadcrumbList for product + collection + article pages {%- endcomment -%}
{%- if template.name contains 'product' or template.name contains 'collection' or template.name contains 'article' -%}
  <script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
      { "@type": "ListItem", "position": 1, "name": "Home", "item": "{{ shop.url }}/" }
      {%- if template.name contains 'product' and collection -%}
      ,{ "@type": "ListItem", "position": 2, "name": "{{ collection.title | escape }}", "item": "{{ shop.url }}{{ collection.url }}" }
      ,{ "@type": "ListItem", "position": 3, "name": "{{ product.title | escape }}", "item": "{{ shop.url }}{{ product.url }}" }
      {%- elsif template.name contains 'product' -%}
      ,{ "@type": "ListItem", "position": 2, "name": "{{ product.title | escape }}", "item": "{{ shop.url }}{{ product.url }}" }
      {%- elsif template.name contains 'collection' and collection -%}
      ,{ "@type": "ListItem", "position": 2, "name": "{{ collection.title | escape }}", "item": "{{ shop.url }}{{ collection.url }}" }
      {%- elsif template.name contains 'article' and blog -%}
      ,{ "@type": "ListItem", "position": 2, "name": "{{ blog.title | escape }}", "item": "{{ shop.url }}{{ blog.url }}" }
      ,{ "@type": "ListItem", "position": 3, "name": "{{ article.title | escape }}", "item": "{{ shop.url }}{{ article.url }}" }
      {%- endif -%}
    ]
  }
  </script>
{%- endif -%}
