{% extends "base.html" %}

{#
================================================================================
Single Tag Page Template (Kida-Native)
================================================================================

Displays all posts with a specific tag.

KIDA FEATURES USED:
- {% let %} for template-scoped variables
- Optional chaining (?.) for null-safe access
- Null coalescing (??) for smart defaults

USAGE:
Auto-generated for each tag in the site
================================================================================
#}

{% from 'partials/components/article.html' import article_card %}
{% from 'partials/navigation-components.html' import pagination %}

{% block content %}
{# Template-scoped variables #}
{% let current_tag = tag ?? '' %}
{% let tag_posts = posts ?? [] %}
{% let total_posts = total_posts ?? (tag_posts | length) %}
{% let total_pages = total_pages ?? 1 %}
{% let current_page = current_page ?? 1 %}
{% let base_url = base_url ?? tag_url(current_tag) %}

{# Two-column layout similar to docs #}
<div class="docs-layout">
  {# Left Sidebar: Tag Navigation #}
  <aside class="docs-sidebar" id="tag-sidebar" role="navigation" aria-label="Tag navigation">
    {% include 'partials/tag-nav.html' %}
  </aside>

  {# Main Content #}
  <div class="docs-main">
    {# Action Bar: Breadcrumbs + Share #}
    {% include 'partials/action-bar.html' %}

    <article class="prose">
      <header class="page-header">
        <h1>
          <span class="tag-icon">#</span>
          {{ current_tag }}
        </h1>

        <p class="page-description">
          {{ total_posts }} post{% if total_posts != 1 %}s{% end %} tagged with "{{ current_tag }}"
        </p>
      </header>

      {% if tag_posts | length > 0 %}
      <div class="archive-list">
        {% for post in tag_posts %}
        {# Skip autodoc pages using efficient function check instead of string manipulation #}
        {% let is_autodoc = is_autodoc_page(post) if is_autodoc_page is defined else false %}
        {% unless is_autodoc %}
        {{ article_card(post, show_excerpt=true) }}
        {% end %}
        {% end %}
      </div>

      {# Pagination #}
      {% if total_pages > 1 %}
      {{ pagination(current_page, total_pages, base_url) }}
      {% end %}
      {% else %}
      <p>No posts found with this tag.</p>
      {% end %}

      <div class="tag-nav">
        <a href="{{ '/tags/' | absolute_url }}" class="button button-outline">← View all tags</a>
      </div>
    </article>
  </div>

  {# Right Sidebar: Empty (hidden by CSS) #}
  <aside class="docs-toc"></aside>
</div>
{% end %}
