{#
Autodoc Header Partial

Renders the header section: badges, title, description, stats, source link.
Works for all autodoc types (Python, CLI, OpenAPI).

Required: element (DocElement)
Optional: show_stats (bool, default true), show_source (bool, default true)
#}

{% let
show_stats = show_stats ?? true,
show_source = show_source ?? true,
meta = element?.metadata ?? {},
children = element?.children ?? []
%}

<header class="autodoc-header">
  {# Badges #}
  {% let badges = [] %}
  {% if meta?.is_deprecated %}{% set _ = badges.append('deprecated') %}{% end %}
  {% if meta?.is_async %}{% set _ = badges.append('async') %}{% end %}
  {% if meta?.is_abstract %}{% set _ = badges.append('abstract') %}{% end %}
  {% if meta?.is_dataclass %}{% set _ = badges.append('dataclass') %}{% end %}
  {% if meta?.is_classmethod %}{% set _ = badges.append('classmethod') %}{% end %}
  {% if meta?.is_staticmethod %}{% set _ = badges.append('staticmethod') %}{% end %}
  {% if meta?.is_property %}{% set _ = badges.append('property') %}{% end %}

  {% if badges | length > 0 %}
  <div class="autodoc-badges">
    {% for badge in badges %}
    <span class="autodoc-badge" data-badge="{{ badge }}">{{ badge | title }}</span>
    {% end %}
  </div>
  {% end %}

  {# Title #}
  <h1 class="autodoc-title">
    {% with meta?.method as method %}
    {% if element?.element_type == 'endpoint' and method %}
    <span class="autodoc-method" data-method="{{ method | lower }}">{{ method | upper }}</span>
    {% end %}
    {% end %}
    <code>{{ element?.qualified_name ?? element?.name }}</code>
  </h1>

  {# Description #}
  {% if element?.description %}
  <div class="autodoc-description prose">
    {{ element.description | markdownify | safe }}
  </div>
  {% end %}

  {# Stats row #}
  {% if show_stats %}
  {% let stat_config = [
  {'type': 'option', 'label': 'Options'},
  {'type': 'argument', 'label': 'Arguments'},
  {'type': 'method', 'label': 'Methods'},
  {'type': 'function', 'label': 'Functions'},
  {'type': 'class', 'label': 'Classes'},
  {'type': 'attribute', 'label': 'Attributes'}
  ] %}

  {% let stats = [] %}
  {% for item in stat_config %}
  {% let count = children |> selectattr('element_type', 'eq', item.type) |> list |> length %}
  {% if count > 0 %}
  {% set _ = stats.append({'value': count, 'label': item.label}) %}
  {% end %}
  {% end %}

  {# Subcommands (command or command-group) #}
  {% let subcommands_count = children |> selectattr('element_type', 'match', '^command(-group)?$') |> list |> length %}
  {% if subcommands_count > 0 %}
  {% set _ = stats.append({'value': subcommands_count, 'label': 'Subcommands'}) %}
  {% end %}

  {# Parameters #}
  {% let parameters = meta?.args ?? meta?.parameters ?? [] %}
  {% if parameters | length > 0 %}
  {% set _ = stats.append({'value': parameters | length, 'label': 'Parameters'}) %}
  {% end %}

  {% if stats | length > 0 %}
  <div class="autodoc-stats">
    {% for stat in stats %}
    <span class="autodoc-stat">
      <span class="autodoc-stat-value">{{ stat.value }}</span>
      <span class="autodoc-stat-label">{{ stat.label }}</span>
    </span>
    {% end %}
  </div>
  {% end %}
  {% end %}

  {# Source link #}
  {% with meta?.source_file as source_file %}
  {% if show_source and source_file %}
  <a href="{{ meta?.source_url ?? '#' }}" class="autodoc-source" target="_blank" rel="noopener">
    View source
  </a>
  {% end %}
  {% end %}
</header>
