{#
Bengal OpenAPI Schema Page
==========================

Standalone schema/model documentation page.
Displays JSON Schema object structure with examples.

Context:
- element: DocElement with OpenAPISchemaMetadata
- page: Page object
- section: Section for navigation
- site: Site instance
- config: Config instance

Kida Features:
- {% with %} for nil-resilient optional sections
- {% cache %} for expensive schema rendering
- Optional chaining (?.) and null coalescing (??)
#}
{% extends 'autodoc/openapi/layouts/reference.html' %}

{% let meta = element?.typed_metadata ?? element?.metadata ?? {} %}
{% let schema_name = element?.name ?? page?.title ?? 'Schema' %}
{% let schema_type = meta?.schema_type ?? 'object' %}
{% let properties = meta?.properties ?? {} %}
{% let required_props = meta?.required ?? () %}
{% let enum_vals = meta?.enum %}
{% let example_val = meta?.example %}
{% let description = element?.description ?? '' %}

{% block header %}
<h1 class="api-schema__title">
  <code>{{ schema_name }}</code>
  <span class="badge badge--outline">{{ schema_type }}</span>
</h1>

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

{% block content_main %}
<div class="api-schema" id="schema-{{ schema_name | slugify }}" data-autodoc="openapi" data-element="schema">

  {# Schema Properties #}
  {% if properties | length > 0 %}
  <section class="api-schema__section autodoc-section" data-section="properties">
    <h2 class="api-schema__section-title">Properties</h2>

    {% cache 'openapi-schema-' ~ schema_name %}
    {% let schema = meta %}
    {% let name = schema_name %}
    {% let depth = 0 %}
    {% include 'autodoc/openapi/partials/schema-viewer.html' %}
    {% end %}
  </section>
  {% end %}

  {# Enum Values (for enum types) #}
  {% with enum_vals as enums %}
  {% if enums | length > 0 %}
  <section class="api-schema__section autodoc-section" data-section="enum">
    <h2 class="api-schema__section-title">Allowed Values</h2>
    <div class="api-schema__enum-list">
      {% for val in enums %}
      <code class="api-schema__enum-value">{{ val }}</code>
      {% end %}
    </div>
  </section>
  {% end %}
  {% end %}

  {# Example #}
  {% with example_val as example %}
  <section class="api-schema__section autodoc-section" data-section="example">
    <h2 class="api-schema__section-title">Example</h2>
    <div class="api-schema__example">
      <pre><code class="language-json">{{ example | tojson(indent=2) }}</code></pre>
    </div>
  </section>
  {% end %}

  {# Usage Information #}
  {% with element?.see_also as refs %}
  {% if refs | length > 0 %}
  <section class="api-schema__section autodoc-section" data-section="usage">
    <h2 class="api-schema__section-title">Used In</h2>
    <ul class="api-schema__usage-list">
      {% for ref in refs %}
      <li><a href="{{ ref }}">{{ ref }}</a></li>
      {% end %}
    </ul>
  </section>
  {% end %}
  {% end %}
</div>
{% end %}
