{#
================================================================================
Autodoc Members Partial (Kida-Native)
================================================================================

Renders collapsible member cards for methods, attributes, etc.
With status dots, counts, return types, and internal method separation.

TEMPLATE VARIABLES:
- members: list of DocElements (required)
- title: str (default: 'Members')
- member_type: str (default: 'method')
- first_open: bool (default: true) - auto-open first member

Uses the `member_view` filter to normalize each member's properties,
providing a consistent API regardless of how data is stored in DocElements.

MemberView properties:
- name, signature, description, return_type, return_description
- params (tuple of ParamView with name, type, default, description)
- is_async, is_property, is_classmethod, is_staticmethod, is_abstract
- is_deprecated, is_private, href, decorators

================================================================================
#}

{# Template configuration with defaults #}
{% let
section_title = title ?? 'Members',
section_member_type = member_type ?? 'method',
auto_open_first = first_open ?? true,
all_members = members ?? []
%}

{# Separate public and internal members using is_private from MemberView #}
{# NOTE: Must use {% export %} to promote from loop scope to template scope #}
{% let public_members = [] %}
{% let internal_members = [] %}
{% for el in all_members %}
{% let m = el | member_view %}
{% if m.is_private %}
{% export internal_members = internal_members + [m] %}
{% else %}
{% export public_members = public_members + [m] %}
{% end %}
{% end %}

{# HELPER: Render a single member detailed card using MemberView #}
{% def render_member(m, section_member_type, is_open=false) %}
<details class="autodoc-member" data-member="{{ section_member_type }}" {{ 'open' if is_open else '' }}>
  <summary class="autodoc-member-header">
    {# Status dot #}
    <span class="autodoc-member-dot"></span>

    {# Content wrapper #}
    <div class="autodoc-member-content">
      <div class="autodoc-member-name-row">
        <code class="autodoc-member-name">{{ m.name }}</code>
        {# Parameter count badge #}
        {% let param_count = m.params | length %}
        <span class="autodoc-member-count" title="{{ param_count }} parameter{{ 's' if param_count != 1 else '' }}">{{
          param_count }}</span>
        {# Return type with arrow #}
        {% if m.return_type and m.return_type != 'None' %}
        <span class="autodoc-member-return" title="Returns {{ m.return_type }}">
          <code>{{ m.return_type | truncate(25, True, '…') }}</code>
        </span>
        {% end %}
        {# Toggle indicator #}
        <span class="autodoc-member-toggle">▼</span>
      </div>
      {# Description preview (for scanning) #}
      {% if m.description %}
      <span class="autodoc-member-desc-preview">{{ m.description | striptags | truncate(80, True, '…') }}</span>
      {% end %}
    </div>

    {# Badges - using MemberView properties directly #}
    {% let member_badges = [
    'async' if m.is_async,
    'classmethod' if m.is_classmethod,
    'staticmethod' if m.is_staticmethod,
    'property' if m.is_property,
    'abstract' if m.is_abstract,
    'deprecated' if m.is_deprecated,
    ] | compact %}
    {% if member_badges | length > 0 %}
    <span class="autodoc-member-badges">
      {% for badge in member_badges %}
      <span class="autodoc-badge" data-badge="{{ badge }}">{{ badge }}</span>
      {% end %}
    </span>
    {% end %}
  </summary>

  <div class="autodoc-member-body">
    {# Signature (copy-paste ready) #}
    {% if m.signature %}
    <div class="autodoc-signature-section">
      <code class="autodoc-member-sig">{{ m.signature }}</code>
    </div>
    {% end %}

    {# Full description - only show if truncated in header #}
    {% let desc_stripped = (m.description | striptags | trim) if m.description else '' %}
    {% if m.description and desc_stripped | length > 77 %}
    <div class="autodoc-member-desc prose">
      {{ m.description | markdownify | safe }}
    </div>
    {% end %}

    {# Member parameters - using ParamView from MemberView.params #}
    {% if m.params | length > 0 %}
    <div class="autodoc-params-section">
      <h5 class="autodoc-params-title">Parameters</h5>
      <table class="autodoc-table autodoc-table--compact" data-table="parameters">
        <thead>
          <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Description</th>
          </tr>
        </thead>
        <tbody>
          {% for p in m.params %}
          <tr>
            <td class="autodoc-cell-name"><code>{{ p.name }}</code></td>
            <td class="autodoc-cell-type"><code>{{ p.type if p.type else '—' }}</code></td>
            <td class="autodoc-cell-desc">
              {{ (p.description | markdownify | safe) if p.description else '' }}
              {% if p.default is not none and p.default != '' %}
              <span class="autodoc-param-default">Default: <code>{{ p.default }}</code></span>
              {% end %}
            </td>
          </tr>
          {% end %}
        </tbody>
      </table>
    </div>
    {% end %}

    {# Member return type (detailed) - only show if has description OR non-trivial type #}
    {% let is_nontrivial_return = m.return_type and m.return_type not in ('None', 'none', 'void', '') %}
    {% if m.return_description or is_nontrivial_return %}
    <div class="autodoc-returns">
      <h5 class="autodoc-returns-title">Returns</h5>
      <code class="autodoc-returns-type">{{ m.return_type }}</code>
      {% if m.return_description %}
      <span class="autodoc-returns-desc">{{ m.return_description }}</span>
      {% end %}
    </div>
    {% end %}
  </div>
</details>
{% end %}

{% if all_members | length > 0 %}
<section class="autodoc-section" data-section="{{ section_member_type }}s">
  {% if section_title %}
  <h2 class="autodoc-section-title">{{ section_title }}</h2>
  {% end %}

  {# Public members #}
  {% if public_members | length > 0 %}
  <div class="autodoc-members-list">
    {% for m in public_members %}
    {{ render_member(m, section_member_type, is_open=(auto_open_first and loop.first)) }}
    {% end %}
  </div>
  {% end %}

  {# Internal members (collapsed by default) #}
  {% if internal_members | length > 0 %}
  <details class="autodoc-internal-methods">
    <summary class="autodoc-internal-methods-header">
      <span class="autodoc-internal-methods-label">
        <span>Internal Methods</span>
        <span class="autodoc-member-count">{{ internal_members | length }}</span>
      </span>
      <span class="autodoc-member-toggle">▼</span>
    </summary>
    <div class="autodoc-internal-methods-content">
      {% for m in internal_members %}
      {{ render_member(m, section_member_type) }}
      {% end %}
    </div>
  </details>
  {% end %}
</section>
{% end %}
