{% extends "base.html" %}

{#
Year Archive Template

Displays all posts from a specific year, grouped by month.
Uses query indexes for O(1) lookups instead of O(n) filtering.

URL Pattern: /archive/2024/

Required page metadata:
- year: "2024"

Optional metadata:
- section: "blog" (which section to show archives for)
- show_stats: true (show statistics for the year)

Usage:
Create content/archive/2024.md:
---
title: "2024 Archive"
year: "2024"
template: archive-year.html
---

Context variables:
- page: Current page with year in metadata
- page.metadata.year: The year to show
- site.indexes.date_range: Date range index
#}

{% from 'partials/navigation-components.html' import breadcrumbs %}
{% from 'partials/content-components.html' import tag_list %}

{% set year = page.metadata.year | string %}
{% set section_filter = page.metadata.get('section') %}

{% block content %}
{{ breadcrumbs(page) }}

<div class="archive-year">
    <header class="page-header page-header--centered">
        <h1>📅 {{ year }} Archive</h1>

        {# O(1) lookup for the entire year #}
        {% set year_posts = site.indexes.date_range.get(year) | resolve_pages %}

        {# Filter by section if specified #}
        {% if section_filter %}
        {% set year_posts = year_posts | selectattr('_section.name', 'equalto', section_filter) | list %}
        {% endif %}

        {% if year_posts %}
        <p class="archive-count">{{ year_posts | length }} post{{ 's' if year_posts | length != 1 }} in {{ year }}</p>

        {# Optional statistics #}
        {% if page.metadata.get('show_stats') %}
        <div class="archive-stats">
            <div class="stat-grid">
                <div class="stat">
                    <span class="stat-value">{{ year_posts | length }}</span>
                    <span class="stat-label">Posts</span>
                </div>
                <div class="stat">
                    <span class="stat-value">{{ year_posts | map(attribute='content') | join('') | wordcount |
                        format_number }}</span>
                    <span class="stat-label">Words</span>
                </div>
                <div class="stat">
                    <span class="stat-value">{{ year_posts | map(attribute='tags') | flatten | unique | length }}</span>
                    <span class="stat-label">Topics</span>
                </div>
                {% set authors = year_posts | map(attribute='metadata.author') | select('defined') | unique | list %}
                <div class="stat">
                    <span class="stat-value">{{ authors | length }}</span>
                    <span class="stat-label">Author{{ 's' if authors | length != 1 }}</span>
                </div>
            </div>
        </div>
        {% endif %}
        {% endif %}
    </header>

    {% if year_posts %}
    {# Group posts by month #}
    {% for month_num in range(1, 13) %}
    {% set month_key = year ~ '-' ~ '%02d' | format(month_num) %}
    {% set month_posts = site.indexes.date_range.get(month_key) | resolve_pages %}

    {# Filter by section if specified #}
    {% if section_filter %}
    {% set month_posts = month_posts | selectattr('_section.name', 'equalto', section_filter) | list %}
    {% endif %}

    {% if month_posts %}
    <section class="month-group">
        <h2 class="month-title">
            {{ (year ~ '-' ~ '%02d'|format(month_num) ~ '-01') | dateformat('%B') }}
            <span class="month-count">({{ month_posts | length }})</span>
        </h2>

        <div class="month-posts">
            {% for post in month_posts | sort_by('date', reverse=true) %}
            <article class="archive-post-card">
                <time class="post-date" datetime="{{ post.date | date_iso }}">
                    {{ post.date | dateformat('%d') }}
                </time>

                <div class="post-content">
                    <h3 class="post-title">
                        <a href="{{ post.url }}">{{ post.title }}</a>
                    </h3>

                    {% if post.metadata.get('description') %}
                    <p class="post-excerpt">{{ post.metadata.get('description') }}</p>
                    {% endif %}

                    <div class="post-meta">
                        {% if post.metadata.get('author') %}
                        <span class="post-author">{{ post.metadata.get('author') }}</span>
                        {% endif %}

                        {% if post.content %}
                        <span class="reading-time">
                            {{ (post.content | wordcount / 200) | round }} min read
                        </span>
                        {% endif %}

                        {% if post.tags %}
                        <span class="post-tags">
                            {{ tag_list(post.tags | limit(3)) }}
                        </span>
                        {% endif %}
                    </div>
                </div>
            </article>
            {% endfor %}
        </div>
    </section>
    {% endif %}
    {% endfor %}

    {# Navigation to other years #}
    <nav class="year-navigation" aria-label="Other years">
        <h3>Browse Other Years</h3>
        <ul class="year-list">
            {% set all_years = site.indexes.date_range.keys()
            | select('match', '^\\d{4}$')
            | sort(reverse=true)
            | list %}

            {% for other_year in all_years %}
            {% set other_year_posts = site.indexes.date_range.get(other_year) | resolve_pages %}

            {# Filter by section if specified #}
            {% if section_filter %}
            {% set other_year_posts = other_year_posts | selectattr('_section.name', 'equalto', section_filter) | list
            %}
            {% endif %}

            {% if other_year_posts %}
            <li {% if other_year==year %}class="current-year" {% endif %}>
                <a href="{{ ('/archive/' ~ other_year ~ '/') | absolute_url }}">
                    {{ other_year }}
                    <span class="year-count">({{ other_year_posts | length }})</span>
                </a>
            </li>
            {% endif %}
            {% endfor %}
        </ul>
    </nav>

    {% else %}
    {# Empty state #}
    <div class="archive-empty">
        <div class="empty-state">
            <p class="empty-message">No posts found for {{ year }}.</p>
            <a href="{{ '/archive/' | absolute_url }}" class="button">Browse All Archives</a>
        </div>
    </div>
    {% endif %}
</div>

{% endblock %}
