{#
Bengal OpenAPI List Page
========================

Consolidated endpoint list for a tag/category section.
Groups multiple endpoints under a single tag with method badges.

Context:
- element: DocElement (optional, for tag info)
- section: Section containing endpoints
- page: Page object
- site: Site instance
- config: Config instance

The `endpoints` filter normalizes access to endpoint data regardless of
whether the source is DocElements (consolidated mode) or Pages (individual mode).
Each EndpointView provides:
  - method: HTTP method
  - path: URL path
  - summary: Short description
  - deprecated: Deprecation status
  - href: Always valid link (anchor in consolidated mode, page URL otherwise)
  - has_page: Whether individual page exists

Kida Features:
- {% with %} for nil-resilient optional sections
- {% spaceless %} for compact badge output
- Optional chaining (?.) and null coalescing (??)
#}
{% extends 'autodoc/openapi/layouts/reference.html' %}

{% let tag_name = element?.name ?? section?.title ?? page?.title ?? 'Endpoints' %}
{% let tag_desc = element?.description ?? section?.metadata?.description ?? '' %}
{% let eps = section | endpoints %}

{% block header %}
<h1 class="api-list__title">{{ tag_name }}</h1>

{% if tag_desc %}
<div class="api-list__description prose">
  {{ tag_desc | markdownify | safe }}
</div>
{% end %}

<div class="api-list__stats">
  <span class="badge badge--outline">{{ eps | length }} endpoints</span>
</div>
{% end %}

{% block content_main %}
<div class="api-list" data-autodoc="openapi" data-element="list">

  {# Endpoint Table #}
  {% if eps | length > 0 %}
  <section class="api-list__endpoints autodoc-section">
    <table class="autodoc-table" data-table="endpoints">
      <thead>
        <tr>
          <th>Method</th>
          <th>Endpoint</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        {% for ep in eps %}
        <tr class="{% if ep.deprecated %}api-list__row--deprecated{% end %}">
          <td class="autodoc-cell-name" data-label="Method">
            {% spaceless %}
            <span class="api-method api-method--sm api-method--{{ ep.method |> lower }}">{{ ep.method }}</span>
            {% end %}
          </td>
          <td class="autodoc-cell-name" data-label="Endpoint">
            {% if ep.has_page %}
            <a href="{{ ep.href }}">
              <code>{{ ep.path | highlight_path_params | safe }}</code>
            </a>
            {% else %}
            <code>{{ ep.path | highlight_path_params | safe }}</code>
            {% end %}
            {% if ep.deprecated %}
            <span class="autodoc-badge" data-badge="deprecated">Deprecated</span>
            {% end %}
          </td>
          <td class="autodoc-cell-desc" data-label="Description">
            {{ ep.summary | truncate(100) }}
          </td>
        </tr>
        {% end %}
      </tbody>
    </table>
  </section>
  {% else %}
  {# Empty State #}
  <div class="autodoc-empty-state">
    {{ icon('inbox', size=48, class='autodoc-empty-state-icon') }}
    <p class="autodoc-empty-state-text">No endpoints in this category</p>
  </div>
  {% end %}

  {# Cards View (Alternative) #}
  {% if eps | length > 0 %}
  <section class="api-list__cards autodoc-section">
    <h2 class="autodoc-section-title">Quick Reference</h2>
    <div class="autodoc-cards">
      {% for ep in eps %}
      {% if ep.has_page %}
      <a href="{{ ep.href }}" class="autodoc-card{% if ep.deprecated %} autodoc-card--deprecated{% end %}">
        <div class="autodoc-card-header">
          {% spaceless %}
          <span class="api-method api-method--sm api-method--{{ ep.method |> lower }}">{{ ep.method }}</span>
          {% end %}
          <code class="autodoc-card-name">{{ ep.path | highlight_path_params | safe }}</code>
        </div>
        {% if ep.summary %}
        <p class="autodoc-card-desc">{{ ep.summary | truncate(80) }}</p>
        {% end %}
        {% if ep.deprecated %}
        <span class="autodoc-badge" data-badge="deprecated">Deprecated</span>
        {% end %}
      </a>
      {% else %}
      <div class="autodoc-card{% if ep.deprecated %} autodoc-card--deprecated{% end %}">
        <div class="autodoc-card-header">
          {% spaceless %}
          <span class="api-method api-method--sm api-method--{{ ep.method |> lower }}">{{ ep.method }}</span>
          {% end %}
          <code class="autodoc-card-name">{{ ep.path | highlight_path_params | safe }}</code>
        </div>
        {% if ep.summary %}
        <p class="autodoc-card-desc">{{ ep.summary | truncate(80) }}</p>
        {% end %}
        {% if ep.deprecated %}
        <span class="autodoc-badge" data-badge="deprecated">Deprecated</span>
        {% end %}
      </div>
      {% end %}
      {% end %}
    </div>
  </section>
  {% end %}
</div>
{% end %}
