{% extends 'public/base.html' %}

{% block title %}{{ '#' if not is_category|default(false) else '' }}{{ tag_name }} - {{ site_name }}{% endblock %}
{% block meta_description %}{{ tag_name }} — {{ site_name }}{% endblock %}

{% block schema %}
<script type="application/ld+json">{{ page_schema | tojson | safe }}</script>
{% endblock %}

{% block hreflang %}
{#- Hub-style hreflang: every active language is an equal version of this
    listing. Default language lives on the clean URL, others carry ?lang=.
    Emitted on page 1 only — page counts differ per language, so cross-
    linking /tag/X?page=3 to /tag/X?lang=de&page=3 could point at a 404.
    No x-default: the engine treats all languages as equals (same policy
    as the sitemap). -#}
{%- if not request.args.get('page') %}
{%- for version in all_versions %}
{%- if version.language == (default_language or 'en') %}
<link rel="alternate" hreflang="{{ version.language }}" href="{{ request.base_url }}" />
{%- else %}
<link rel="alternate" hreflang="{{ version.language }}" href="{{ request.base_url }}?lang={{ version.language }}" />
{%- endif %}
{%- endfor %}
{%- endif %}
{% endblock %}

{% block content %}
{% set _is_cat = is_category|default(false) %}
{# The category route is /category/<category_name>; passing tag_name instead
   leaves the rule variable unfilled and url_for() raises BuildError -> 500.
   Pick the endpoint and its keyword name here, once. #}
{% set _page_endpoint = 'view_category' if _is_cat else 'view_tag' %}
{% set _page_key = {'category_name': category_name} if _is_cat else {'tag_name': tag_name} %}
{# ФИКС 3 (отделяемый) — сохранить язык на 2-й и далее страницах. #}
{% set _page_lang = request.args.get('lang') %}

<div class="ap-page__header ap-page__header--top">
    <p class="ap-page__meta ap-page__meta--spaced">
        {{ pt('in_category') if _is_cat else pt('tagged_with') }}
    </p>
    <h1 class="ap-page__title">{{ tag_name }}</h1>
</div>

{% if posts.items or tagged_pages %}
<div class="ap-post-list--narrow">
    {% for post in posts.items %}
    <article class="ap-post-card">
        <h2 class="ap-post-card__title">
            <a href="{{ url_for('view_content', slug=post.slug) }}">{{ post.title }}</a>
        </h2>
        <div class="ap-post-card__meta">
            <time datetime="{{ post.published_at.isoformat() if post.published_at else '' }}">
                {{ (post.published_at|site_tz).strftime('%B %d, %Y') if post.published_at else '' }}
            </time>
            {% if post.category %}
            <span>· {{ post.category }}</span>
            {% endif %}
        </div>
        {% if post.excerpt %}
        <p class="ap-post-card__excerpt">{{ post.excerpt }}</p>
        {% endif %}
    </article>
    {% endfor %}
    
    {% for page in tagged_pages %}
    <article class="ap-post-card">
        <h2 class="ap-post-card__title">
            <a href="{{ url_for('view_content', slug=page.slug) }}">{{ page.title }}</a>
        </h2>
        <div class="ap-post-card__meta">
            <time datetime="{{ page.updated_at.isoformat() }}">
                {{ (page.updated_at|site_tz).strftime('%B %d, %Y') }}
            </time>
        </div>
        {% if page.excerpt %}
        <p class="ap-post-card__excerpt">{{ page.excerpt }}</p>
        {% endif %}
    </article>
    {% endfor %}
</div>

{% if posts.pages > 1 %}
<nav class="ap-pagination" aria-label="Pagination">
    {% if posts.has_prev %}
    <a href="{{ url_for(_page_endpoint, lang=_page_lang, page=(posts.prev_num if posts.prev_num > 1 else none), **_page_key) }}"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m15 18-6-6 6-6"/></svg></a>
    {% endif %}
    
    {% for page_num in posts.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
        {% if page_num %}
            {% if page_num == posts.page %}
            <span class="is-current">{{ page_num }}</span>
            {% else %}
            <a href="{{ url_for(_page_endpoint, lang=_page_lang, page=(page_num if page_num > 1 else none), **_page_key) }}">{{ page_num }}</a>
            {% endif %}
        {% else %}
            <span>…</span>
        {% endif %}
    {% endfor %}
    
    {% if posts.has_next %}
    <a href="{{ url_for(_page_endpoint, lang=_page_lang, page=posts.next_num, **_page_key) }}"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 18 6-6-6-6"/></svg></a>
    {% endif %}
</nav>
{% endif %}

{% else %}
<div class="ap-empty-state">
    <p>{{ pt('no_content') }}</p>
</div>
{% endif %}
{% endblock %}
