{#
    Renders the <head> meta block for a web page, sourced from the
    `pages.<page>.*` translation catalogue.

    Usage (in a page template, inside <head>):
        {% include 'partials/page-meta.twig' with { page: 'login' } only %}

    Expected keys in `resources/lang/<locale>/pages/<page>.php`:
        - meta.title        (string, falls back to pages.<page>.title)
        - meta.description  (string)
        - meta.robots       (string, e.g. "index, follow" or "noindex, nofollow")
        - og.title          (string, falls back to meta.title)
        - og.description    (string, falls back to meta.description)
        - og.image          (string — absolute URL OR `/`-prefixed path; empty = no tag)
        - og.imageAlt       (string)
        - twitter.card      (string, "summary" or "summary_large_image")
        - twitter.title     (string, falls back to og.title)
        - twitter.description (string, falls back to og.description)
        - twitter.image     (string, falls back to og.image)

    `app_url` is exposed as a Twig global (see config/container.php). Relative
    OG/Twitter image paths are prefixed with it; absolute URLs (http://…) are
    passed through unchanged.
#}
{% set ns = 'pages.' ~ page ~ '.' %}

{# --- <title> + standard meta --- #}
{% set page_title = trans(ns ~ 'meta.title') %}
{% if page_title == ns ~ 'meta.title' %}
    {% set page_title = trans(ns ~ 'title') %}
{% endif %}
<title>{{ page_title }}</title>

{% set page_description = trans(ns ~ 'meta.description') %}
{% if page_description != ns ~ 'meta.description' %}
    <meta name="description" content="{{ page_description }}">
{% endif %}

{% set page_robots = trans(ns ~ 'meta.robots') %}
{% if page_robots != ns ~ 'meta.robots' %}
    <meta name="robots" content="{{ page_robots }}">
{% endif %}

{# --- Open Graph --- #}
{% set og_title = trans(ns ~ 'og.title') %}
{% if og_title == ns ~ 'og.title' %}{% set og_title = page_title %}{% endif %}
<meta property="og:title" content="{{ og_title }}">

{% set og_description = trans(ns ~ 'og.description') %}
{% if og_description == ns ~ 'og.description' %}{% set og_description = page_description %}{% endif %}
{% if og_description %}
    <meta property="og:description" content="{{ og_description }}">
{% endif %}

<meta property="og:type" content="website">

{% set og_image = trans(ns ~ 'og.image') %}
{% if og_image != ns ~ 'og.image' and og_image is not empty %}
    {% set og_image_url = og_image starts with 'http' ? og_image : app_url ~ og_image %}
    <meta property="og:image" content="{{ og_image_url }}">
    {% set og_image_alt = trans(ns ~ 'og.imageAlt') %}
    {% if og_image_alt != ns ~ 'og.imageAlt' %}
        <meta property="og:image:alt" content="{{ og_image_alt }}">
    {% endif %}
{% endif %}

{# --- Twitter Card --- #}
{% set tw_card = trans(ns ~ 'twitter.card') %}
{% if tw_card == ns ~ 'twitter.card' %}{% set tw_card = 'summary' %}{% endif %}
<meta name="twitter:card" content="{{ tw_card }}">

{% set tw_title = trans(ns ~ 'twitter.title') %}
{% if tw_title == ns ~ 'twitter.title' %}{% set tw_title = og_title %}{% endif %}
<meta name="twitter:title" content="{{ tw_title }}">

{% set tw_description = trans(ns ~ 'twitter.description') %}
{% if tw_description == ns ~ 'twitter.description' %}{% set tw_description = og_description %}{% endif %}
{% if tw_description %}
    <meta name="twitter:description" content="{{ tw_description }}">
{% endif %}

{% set tw_image = trans(ns ~ 'twitter.image') %}
{% if tw_image == ns ~ 'twitter.image' or tw_image is empty %}{% set tw_image = og_image %}{% endif %}
{% if tw_image is not empty and tw_image != ns ~ 'og.image' %}
    {% set tw_image_url = tw_image starts with 'http' ? tw_image : app_url ~ tw_image %}
    <meta name="twitter:image" content="{{ tw_image_url }}">
{% endif %}
