{# --- social/meta_post.html --- #}
{# Context expected:
   site, post, request
#}
{% if post is not defined %}
  {% include 'social/meta_base.html' %}
{% else %}

{# Visibility → robots #}
{% set is_public = (post.visibility == 'public') %}
{% set is_published = (post.status == 'published') %}
{% set robots_here = 'index,follow' if (is_public and is_published and not post.email_only) else 'noindex,nofollow' %}

{# Compute canonical early so both this file and base can use it #}
{% set _site_url = site().url.rstrip('/') if site and site().url else '' %}
{% set _post_path = request.path if request else ('/posts/' ~ (post.slug or post.uuid)) %}
{% set canonical = post.canonical_url or (_site_url ~ _post_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 og_title   = post.og_title or base_title %}
{% set tw_title   = post.twitter_title or base_title %}

{# Description best-effort, trimmed #}
{% set desc_source = post.meta_description
                     or post.og_description
                     or post.twitter_description
                     or post.custom_excerpt
                     or post.excerpt
                     or (post.plaintext if post.plaintext else (post.html|striptags if post.html else '')) %}
{% set description = (desc_source|trim|replace('\n',' ')|replace('\r',' ')|striptags)|truncate(160, True, '…') %}

{# Image priority #}
{% set image_url = post.og_image
                   or post.twitter_image
                   or post.feature_image
                   or (site().default_image if site and site().default_image else None) %}

{# Dates #}
{% set published_iso = post.published_at.isoformat() if post.published_at else None %}
{% set updated_iso   = post.updated_at.isoformat() if post.updated_at
                       else (post.created_at.isoformat() if post.created_at else None) %}

{# Authors / tags #}
{% set primary_author = post.primary_author %}
{% set authors = post.authors or ([primary_author] if primary_author else []) %}
{% set tag_names = (post.tags or []) | map(attribute='name') | list %}
{% set is_article = not post.is_page %}

<title>{{ base_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="{{ 'article' if is_article else 'website' }}">
<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 %}
{% if is_article and published_iso %}<meta property="article:published_time" content="{{ published_iso }}">{% endif %}
{% if is_article and updated_iso %}
  <meta property="article:modified_time" content="{{ updated_iso }}">
  <meta property="og:updatd_time" content="{{ updated_iso }}">
{% endif %}
{% if is_article and post.primary_tag and post.primary_tag.name %}
  <meta property="article:section" content="{{ post.primary_tag.name }}">
{% endif %}
{% if is_article %}
  {% for t in tag_names %}
    <meta property="article:tag" content="{{ t }}">
  {% endfor %}
{% 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 %}
{% if primary_author and primary_author.twitter %}
  <meta name="twitter:creator" content="@{{ primary_author.twitter | replace('@','') }}">
{% 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 author value (no list comprehensions) ---- #}
{% if authors and authors|length == 1 %}
  {% set author_value = {"@type": "Person", "name": authors[0].name} %}
{% elif authors %}
  {% set ns = namespace(arr=[]) %}
  {% for a in authors %}
    {% set _ = ns.arr.append({"@type": "Person", "name": a.name}) %}
  {% endfor %}
  {% set author_value = ns.arr %}
{% else %}
  {% set author_value = none %}
{% endif %}

{# ---- JSON-LD using combine for optionals ---- #}
{% set jsonld = {
  "@context": "https://schema.org",
  "@type": "BlogPosting" if is_article else "WebPage",
  "mainEntityOfPage": canonical,
  "headline": base_title,
  "description": description,
  "image": image_url,
  "datePublished": published_iso,
  "author": author_value,
  "publisher": {
    "@type": "Organization",
    "name": site().title if site and site().title else "",
    "logo": {"@type": "ImageObject", "url": site().logo if site and site().logo else image_url}
  }
} %}

{% if updated_iso %}
  {% set jsonld = jsonld | combine({"dateModified": updated_iso}) %}
{% endif %}
{% if tag_names %}
  {% set jsonld = jsonld | combine({"keywords": tag_names | join(", ")}) %}
{% endif %}

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