{% macro render_scalar_table(obj) -%}
  <div class="w-full overflow-x-auto sm:overflow-visible">
    <table class="w-full table-fixed text-sm border border-neutral-200 dark:border-neutral-800 rounded-xl overflow-hidden">
      <thead class="bg-neutral-50/70 dark:bg-neutral-900/60">
        <tr>
          <th class="px-3 py-2 text-left font-medium w-40 sm:w-56">Field</th>
          <th class="px-3 py-2 text-left font-medium">Value</th>
        </tr>
      </thead>
      <tbody>
        {% for col in obj.__mapper__.columns %}
          {% set key = col.key %}
          {% set val = obj|attr(key) %}
          {% if key != "_sa_instance_state" %}
            <tr class="border-t border-neutral-200 dark:border-neutral-800 align-top">
              <td class="px-3 py-2 whitespace-nowrap text-neutral-600 dark:text-neutral-400 align-top">{{ key }}</td>
              <td class="px-3 py-2 align-top">
                {% if val is none %}
                  <span class="text-neutral-400">—</span>
                {% elif val.__class__.__name__ in ["datetime", "date"] and val.isoformat is defined %}
                  <pre class="whitespace-pre-wrap break-words break-all text-xs"><code>{{ val.isoformat() }}</code></pre>
                {% elif val is string %}
                  <pre class="whitespace-pre-wrap break-words break-all text-xs">{{ val }}</pre>
                {% else %}
                  <pre class="whitespace-pre-wrap break-words break-all text-xs"><code>{{ val }}</code></pre>
                {% endif %}
              </td>
            </tr>
          {% endif %}
        {% endfor %}
      </tbody>
    </table>
  </div>
{%- endmacro %}

{% macro render_model(obj, depth=0, max_depth=2) -%}
  {% if obj is none %}
    <span class="text-neutral-400">—</span>
  {% else %}
    <div class="space-y-4">
      {{ render_scalar_table(obj) }}

      <div class="space-y-3">
        {% for rel in obj.__mapper__.relationships %}
          {% set rel_name = rel.key %}
          {% set loaded = rel.key in obj.__dict__ %}
          {% if loaded %}
            {% set value = obj|attr(rel_name) %}
          {% else %}
            {% set value = none %}
          {% endif %}

          <div class="rounded-xl border border-neutral-200 dark:border-neutral-800">
            <div class="px-3 py-2 bg-neutral-50/70 dark:bg-neutral-900/60 text-sm font-medium">
              Relationship: <span class="font-semibold">{{ rel_name }}</span>
              <span class="ml-2 text-xs text-neutral-500">
                {{ 'many' if rel.uselist else 'one' }} → {{ rel.mapper.class_.__name__ }}
                {% if not loaded %} • <em>not loaded</em>{% endif %}
              </span>
            </div>

            <div class="p-3 text-sm">
              {% if value is none %}
                <span class="text-neutral-400">—</span>

              {% elif rel.uselist %}
                {% set items = value or [] %}
                <div class="text-neutral-500 mb-2">{{ items|length }} item{{ '' if items|length == 1 else 's' }}</div>

                {% if items %}
                  <div class="w-full overflow-x-auto sm:overflow-visible">
                    <table class="w-full table-fixed text-sm border border-neutral-200 dark:border-neutral-800 rounded-lg overflow-hidden">
                      <thead class="bg-neutral-50/70 dark:bg-neutral-900/60">
                        <tr>
                          <th class="px-2 py-1 text-left w-10">#</th>
                          <th class="px-2 py-1 text-left">Summary</th>
                        </tr>
                      </thead>
                      <tbody>
                        {% for it in items %}
                          <tr class="border-t border-neutral-200 dark:border-neutral-800 align-top">
                            <td class="px-2 py-1 whitespace-nowrap align-top">{{ loop.index }}</td>
                            <td class="px-2 py-1 align-top">
                              {% set ident = [] %}
                              {% for k in ['id','ghost_id','uuid','slug','name','title'] if k in it.__mapper__.c %}
                                {% set v = (it|attr(k))|default('', true) %}
                                {% do ident.append(k ~ '=' ~ v) %}
                              {% endfor %}
                              <pre class="whitespace-pre-wrap break-words break-all text-xs"><code>{{ (ident|join(' • ')) or it|string }}</code></pre>

                              {% if depth < max_depth %}
                                <div class="mt-2 pl-3 border-l border-neutral-200 dark:border-neutral-800">
                                  {{ render_model(it, depth+1, max_depth) }}
                                </div>
                              {% else %}
                                <div class="mt-1 text-xs text-neutral-500">…max depth reached…</div>
                              {% endif %}
                            </td>
                          </tr>
                        {% endfor %}
                      </tbody>
                    </table>
                  </div>
                {% endif %}

              {% else %}
                {% set child = value %}
                {% set ident = [] %}
                {% for k in ['id','ghost_id','uuid','slug','name','title'] if k in child.__mapper__.c %}
                  {% set v = (child|attr(k))|default('', true) %}
                  {% do ident.append(k ~ '=' ~ v) %}
                {% endfor %}
                <pre class="whitespace-pre-wrap break-words break-all text-xs mb-2"><code>{{ (ident|join(' • ')) or child|string }}</code></pre>

                {% if depth < max_depth %}
                  <div class="pl-3 border-l border-neutral-200 dark:border-neutral-800">
                    {{ render_model(child, depth+1, max_depth) }}
                  </div>
                {% else %}
                  <div class="text-xs text-neutral-500">…max depth reached…</div>
                {% endif %}
              {% endif %}
            </div>
          </div>
        {% endfor %}
      </div>
    </div>
  {% endif %}
{%- endmacro %}

<div class="px-4 py-8">
  <div class="mb-6 text-sm text-neutral-500">
    Model: <code>Post</code> • Table: <code>{{ original_post.__tablename__ }}</code>
  </div>
  {{ render_model(original_post, 0, 2) }}
</div>

