{% extends "base.html" %}

{#
================================================================================
Author List Template (Kida-Native)
================================================================================

Displays all authors with their profiles and post counts.

KIDA FEATURES USED:
- {% let %} for template-scoped variables
- Optional chaining (?.) for null-safe access
- Null coalescing (??) for smart defaults
- {% def %} for reusable card component
- Pipeline operator (|>) for filter chains

URL Pattern: /authors/

Context variables:
- page: Current list page (_index.md)
- site.data.authors: Author registry (from data/authors.yaml)
- site.indexes.author: Author index for post counts
================================================================================
#}

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

{# =============================================================================
AUTHOR CARD COMPONENT
============================================================================= #}
{% def author_card(author) %}
{% let author_name = author?.name ?? 'Unknown Author' %}
{% let author_href = author?.href ?? '#' %}
{% let author_avatar = author?.avatar ?? '' %}
{% let author_company = author?.company ?? '' %}
{% let author_location = author?.location ?? '' %}
{% let author_bio = author?.bio ?? '' %}
{% let author_github = author?.github ?? '' %}
{% let post_count = author?.post_count ?? 0 %}

<article class="author-card">
  <a href="{{ author_href }}" class="author-card-link">
    {% if author_avatar %}
    <img src="{{ author_avatar }}" alt="{{ author_name }}" class="author-card-avatar">
    {% else %}
    <div class="author-card-avatar-placeholder">
      {{ author_name[0] | upper }}
    </div>
    {% end %}

    <div class="author-card-content">
      <h2 class="author-card-name">{{ author_name }}</h2>

      {% if author_company or author_location %}
      <p class="author-card-meta">
        {% if author_company %}{{ author_company }}{% end %}
        {% if author_company and author_location %} · {% end %}
        {% if author_location %}{{ author_location }}{% end %}
      </p>
      {% end %}

      {% if author_bio %}
      <p class="author-card-bio">{{ author_bio | truncate(120) }}</p>
      {% end %}

      <div class="author-card-stats">
        <span class="stat">{{ post_count }} post{{ 's' if post_count != 1 else '' }}</span>
        {% if author_github %}
        <span class="stat">{{ author_github }}</span>
        {% end %}
      </div>
    </div>
  </a>
</article>
{% end %}

{# =============================================================================
MAIN TEMPLATE
============================================================================= #}

{% block content %}
{# Template-scoped variables #}
{% let page_title = page?.title ?? 'Authors' %}
{% let page_desc = page?.description ?? '' %}
{% let authors_data = site?.data?.authors ?? {} %}
{% let author_index = site?.indexes?.author ?? {} %}
{% let page_children = page?.children ?? [] %}
{% let home_url = '/' | absolute_url %}

{{ breadcrumbs(page) }}

<div class="authors-page">
  <header class="page-header">
    <h1>{{ page_title }}</h1>
    {% if page_desc %}
    <p class="page-description">{{ page_desc }}</p>
    {% end %}
  </header>

  {# Main content from markdown #}
  {% if content %}
  <div class="prose authors-page__content">
    {{ content | safe }}
  </div>
  {% end %}

  {# Build author list from data registry and/or child pages #}
  {% let authors_list = [] %}

  {# Add authors from data/authors.yaml #}
  {% for author_key, author_data in authors_data | items %}
  {% let author_posts = author_index.get(author_key) ?? [] %}
  {% let post_count = author_posts | length %}
  {% let _name = author_data?.name ?? author_key %}
  {% set _ = authors_list.append({
  'key': author_key,
  'name': _name,
  'bio': author_data?.bio ?? '',
  'avatar': author_data?.avatar ?? '',
  'company': author_data?.company ?? '',
  'location': author_data?.location ?? '',
  'github': author_data?.github ?? '',
  'post_count': post_count,
  'href': '/authors/' ~ author_key ~ '/'
  }) %}
  {% end %}

  {# Add authors from child pages (if not already in registry) #}
  {% for child in page_children %}
  {% let child_title = child?.title ?? 'unknown' %}
  {% let author_key = child?.params?.author_name ?? child_title %}
  {% let existing = authors_list |> selectattr('key', 'eq', author_key) |> list %}
  {% if existing | length == 0 %}
  {% let author_posts = author_index.get(author_key) ?? [] %}
  {% let post_count = author_posts | length %}
  {% let child_params = child?.params ?? {} %}
  {% let child_social = child_params?.social ?? {} %}
  {% set _ = authors_list.append({
  'key': author_key,
  'name': child_title,
  'bio': child_params?.bio ?? '',
  'avatar': child_params?.avatar ?? '',
  'company': child_params?.company ?? '',
  'location': child_params?.location ?? '',
  'github': child_social?.github ?? '',
  'post_count': post_count,
  'href': child?.href ?? '#'
  }) %}
  {% end %}
  {% end %}

  {% if authors_list | length > 0 %}
  <div class="authors-grid">
    {% for author in authors_list | sort(attribute='name') %}
    {{ author_card(author) }}
    {% end %}
  </div>
  {% else %}
  {% from "partials/empty-state.html" import empty_state %}
  {{ empty_state(
      title="No authors found",
      message="Add authors to data/authors.yaml or create pages in content/authors/.",
      icon_name="users",
      actions=[{'href': home_url, 'label': 'Go Home', 'icon': 'house'}]
  ) }}
  {% end %}
</div>
{% end %}
