<%
  const entities = (data && Array.isArray(data.entities)) ? data.entities : [];
  const entityCount = (data && typeof data.entityCount === 'number') ? data.entityCount : entities.length;

  function relationArrow(kind) {
    return (kind || '').replace(/-/g, ' ');
  }
%>

<% if (entityCount === 0) { %>
  <div class="py-8 text-center bg-surface-muted border border-line rounded-lg space-y-3 px-6">
    <p class="text-foreground-muted text-sm font-medium">No registered entities to display.</p>
    <p class="text-foreground-faint text-xs leading-relaxed">
      This panel introspects the ORM connection at startup. It appears once at least one entity or
      model is registered on the profiled connection.
    </p>
  </div>
<% } else { %>
  <div class="flex items-center gap-2 mb-3">
    <h3 class="text-foreground-muted text-2xs uppercase tracking-widest font-semibold">Registered entities</h3>
    <span class="text-2xs px-1.5 py-0.5 rounded-full bg-surface-muted border border-line text-foreground-faint tabular-nums"><%= entityCount %> entities</span>
  </div>

  <!-- ── ER diagram ─────────────────────────────────────────────────
       Closed by default: the client behaviour (src/client/behaviors/mermaid-diagram.ts)
       loads Mermaid from jsDelivr and renders the source only once it is opened. -->
  <% if (typeof data.mermaid === 'string' && data.mermaid.length > 0) { %>
    <details class="group mb-3 rounded-lg border border-line overflow-hidden" data-mermaid-diagram>
      <summary class="flex items-center justify-between gap-3 px-4 py-2.5 cursor-pointer select-none bg-surface-muted hover:bg-surface-active transition-colors list-none">
        <div class="flex items-center gap-2 min-w-0">
          <svg class="w-3.5 h-3.5 text-foreground-muted transition-transform group-open:rotate-90 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
          </svg>
          <span class="text-xs font-semibold text-foreground">Entity relationship diagram</span>
        </div>
        <span class="text-2xs text-foreground-faint shrink-0">Mermaid</span>
      </summary>
      <div class="border-t border-line p-4 space-y-3">
        <div class="flex items-center justify-between gap-3">
          <p class="text-2xs text-foreground-faint">Rendered with Mermaid, loaded from cdn.jsdelivr.net when this section is opened.</p>
          <%- copyBtn(data.mermaid, 'Copy Mermaid') %>
        </div>
        <div data-mermaid-status>
          <p class="text-xs text-foreground-faint">Loading diagram…</p>
        </div>
        <!-- Pan / zoom viewport, wired by src/client/behaviors/diagram-viewport.ts. Its inline
             height follows the diagram (--diagram-height), capped to the window; expanded, it
             covers the whole window. -->
        <div class="hidden group/diagram relative h-(--diagram-height,60vh) min-h-60 max-h-[70vh] overflow-hidden rounded-lg border border-line bg-surface select-none touch-none cursor-grab data-dragging:cursor-grabbing focus:outline-none focus-visible:ring-2 focus-visible:ring-nest/40 data-expanded:fixed data-expanded:inset-0 data-expanded:z-[1000] data-expanded:h-auto data-expanded:max-h-none data-expanded:rounded-none data-expanded:border-0"
             tabindex="0" role="application" aria-label="Entity relationship diagram — drag to pan, Ctrl or ⌘ + scroll to zoom, + and - keys to zoom, 0 to fit"
             data-diagram-viewport>
          <div class="h-full w-full" data-mermaid-output></div>
          <div class="hidden absolute top-2 left-2 cursor-default" data-diagram-controls>
            <select class="max-w-56 rounded-md border border-line bg-surface/90 px-2 py-1 text-2xs text-foreground-muted shadow-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-nest/40" aria-label="Go to entity" data-mermaid-entities>
              <option value="">Go to entity…</option>
            </select>
          </div>
          <div class="absolute top-2 right-2 flex items-center gap-0.5 rounded-md border border-line bg-surface/90 p-0.5 shadow-sm cursor-default" data-diagram-controls>
            <button type="button" class="p-1.5 rounded text-foreground-muted hover:bg-surface-muted hover:text-foreground transition-colors" title="Zoom out (-)" aria-label="Zoom out" data-diagram-action="zoom-out">
              <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-width="2" d="M5 12h14"/></svg>
            </button>
            <button type="button" class="min-w-12 px-1.5 py-1 rounded text-2xs font-medium tabular-nums text-foreground-muted hover:bg-surface-muted hover:text-foreground transition-colors" title="Actual size (1)" aria-label="Zoom to actual size" data-diagram-action="actual" data-diagram-zoom>100%</button>
            <button type="button" class="p-1.5 rounded text-foreground-muted hover:bg-surface-muted hover:text-foreground transition-colors" title="Zoom in (+)" aria-label="Zoom in" data-diagram-action="zoom-in">
              <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-width="2" d="M12 5v14M5 12h14"/></svg>
            </button>
            <span class="w-px h-4 mx-0.5 bg-line"></span>
            <button type="button" class="p-1.5 rounded text-foreground-muted hover:bg-surface-muted hover:text-foreground transition-colors" title="Fit to view (0)" aria-label="Fit to view" data-diagram-action="fit">
              <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 9V5a1 1 0 011-1h4M15 4h4a1 1 0 011 1v4M20 15v4a1 1 0 01-1 1h-4M9 20H5a1 1 0 01-1-1v-4"/></svg>
            </button>
            <button type="button" class="p-1.5 rounded text-foreground-muted hover:bg-surface-muted hover:text-foreground transition-colors" title="Toggle full window (Esc to leave)" aria-label="Toggle full window" aria-pressed="false" data-diagram-action="expand">
              <svg class="w-3.5 h-3.5 group-data-expanded/diagram:hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 9V4h5M15 4h5v5M20 15v5h-5M9 20H4v-5M4 4l6 6M20 4l-6 6M20 20l-6-6M4 20l6-6"/></svg>
              <svg class="w-3.5 h-3.5 hidden group-data-expanded/diagram:block" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 5v5H5M14 5v5h5M14 19v-5h5M10 19v-5H5"/></svg>
            </button>
          </div>
          <p class="pointer-events-none absolute bottom-2 left-2 rounded bg-surface/90 px-1.5 py-0.5 text-2xs text-foreground-faint">Drag to pan · Ctrl/⌘ + scroll or double-click to zoom<span class="hidden group-data-expanded/diagram:inline"> · Esc to leave</span></p>
        </div>
        <pre class="hidden text-xs font-mono text-foreground-secondary bg-surface-muted border border-line rounded-lg p-3 overflow-x-auto" data-mermaid-source><%= data.mermaid %></pre>
      </div>
    </details>
  <% } %>

  <!-- One collapsible section per entity: columns, relations and indexes. -->
  <% for (const entity of entities) { %>
    <% const columns = Array.isArray(entity.columns) ? entity.columns : []; %>
    <% const relations = Array.isArray(entity.relations) ? entity.relations : []; %>
    <% const indexes = Array.isArray(entity.indexes) ? entity.indexes : []; %>
    <details class="group mb-3 rounded-lg border border-line overflow-hidden" open>
      <summary class="flex items-center justify-between gap-3 px-4 py-2.5 cursor-pointer select-none bg-surface-muted hover:bg-surface-active transition-colors list-none">
        <div class="flex items-center gap-2 min-w-0">
          <svg class="w-3.5 h-3.5 text-foreground-muted transition-transform group-open:rotate-90 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
          </svg>
          <span class="text-xs font-semibold text-foreground font-mono truncate"><%= entity.name %></span>
          <% if (entity.tableName && entity.tableName !== entity.name) { %>
            <span class="text-2xs text-foreground-faint font-mono truncate">→ <%= entity.tableName %></span>
          <% } %>
        </div>
        <span class="text-2xs px-1.5 py-0.5 rounded-full bg-surface border border-line text-foreground-faint tabular-nums shrink-0"><%= columns.length %> columns</span>
      </summary>
      <div class="border-t border-line p-4 space-y-4">

        <!-- ── Columns ─────────────────────────────────────────────── -->
        <div>
          <h4 class="text-foreground-muted text-2xs uppercase tracking-widest font-semibold mb-2">Columns</h4>
          <div class="rounded-lg border border-line overflow-x-auto">
            <table class="w-full min-w-[640px]">
              <thead>
                <tr class="border-b border-line">
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Name</th>
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Type</th>
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Nullable</th>
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">PK</th>
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Generated</th>
                  <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Default</th>
                </tr>
              </thead>
              <tbody class="divide-y divide-line-subtle">
                <% for (const column of columns) { %>
                  <tr class="hover:bg-surface-muted transition-colors">
                    <td class="py-2 px-4 text-xs font-mono align-top">
                      <span class="text-foreground"><%= column.name %></span>
                      <% if (column.databaseName && column.databaseName !== column.name) { %>
                        <span class="text-foreground-faint">(<%= column.databaseName %>)</span>
                      <% } %>
                    </td>
                    <td class="py-2 px-4 text-xs font-mono align-top text-foreground-secondary">
                      <%= column.type %><% if (column.length) { %><span class="text-foreground-faint">(<%= column.length %>)</span><% } %>
                    </td>
                    <td class="py-2 px-4 text-xs align-top">
                      <% if (column.nullable) { %><span class="text-foreground-faint font-mono">yes</span><% } else { %><span class="text-foreground-muted font-mono">no</span><% } %>
                    </td>
                    <td class="py-2 px-4 text-xs align-top">
                      <% if (column.primary) { %><span class="text-success font-mono font-bold">PK</span><% } else { %><span class="text-foreground-faint">—</span><% } %>
                    </td>
                    <td class="py-2 px-4 text-xs align-top">
                      <% if (column.generated) { %><span class="text-foreground-muted font-mono">auto</span><% } else { %><span class="text-foreground-faint">—</span><% } %>
                    </td>
                    <td class="py-2 px-4 text-xs font-mono align-top">
                      <% if (column.default === undefined || column.default === null) { %>
                        <span class="text-foreground-faint">—</span>
                      <% } else if (column.default === '[REDACTED]') { %>
                        <span class="text-foreground-faint"><%= column.default %></span>
                      <% } else { %>
                        <span class="text-foreground break-all"><%= column.default %></span>
                      <% } %>
                    </td>
                  </tr>
                <% } %>
              </tbody>
            </table>
          </div>
        </div>

        <!-- ── Relations ───────────────────────────────────────────── -->
        <% if (relations.length > 0) { %>
          <div>
            <h4 class="text-foreground-muted text-2xs uppercase tracking-widest font-semibold mb-2">Relations</h4>
            <div class="rounded-lg border border-line overflow-x-auto">
              <table class="w-full min-w-[480px]">
                <thead>
                  <tr class="border-b border-line">
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Property</th>
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Kind</th>
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Target</th>
                  </tr>
                </thead>
                <tbody class="divide-y divide-line-subtle">
                  <% for (const relation of relations) { %>
                    <tr class="hover:bg-surface-muted transition-colors">
                      <td class="py-2 px-4 text-xs font-mono align-top text-foreground"><%= relation.property %></td>
                      <td class="py-2 px-4 text-xs align-top">
                        <span class="text-2xs px-1.5 py-0.5 rounded-full bg-surface-muted border border-line text-foreground-faint"><%= relationArrow(relation.kind) %></span>
                      </td>
                      <td class="py-2 px-4 text-xs font-mono align-top text-foreground-secondary break-all"><%= relation.target %></td>
                    </tr>
                  <% } %>
                </tbody>
              </table>
            </div>
          </div>
        <% } %>

        <!-- ── Indexes ─────────────────────────────────────────────── -->
        <% if (indexes.length > 0) { %>
          <div>
            <h4 class="text-foreground-muted text-2xs uppercase tracking-widest font-semibold mb-2">Indexes</h4>
            <div class="rounded-lg border border-line overflow-hidden">
              <table class="w-full">
                <thead>
                  <tr class="border-b border-line">
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Name</th>
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Columns</th>
                    <th class="text-left py-2.5 px-4 text-foreground-muted font-medium text-2xs uppercase tracking-widest">Unique</th>
                  </tr>
                </thead>
                <tbody class="divide-y divide-line-subtle">
                  <% for (const index of indexes) { %>
                    <tr class="hover:bg-surface-muted transition-colors">
                      <td class="py-2 px-4 text-xs font-mono align-top">
                        <% if (index.name) { %><span class="text-foreground"><%= index.name %></span><% } else { %><span class="text-foreground-faint">—</span><% } %>
                      </td>
                      <td class="py-2 px-4 text-xs font-mono align-top text-foreground-secondary break-all"><%= (index.columns || []).join(', ') %></td>
                      <td class="py-2 px-4 text-xs align-top">
                        <% if (index.unique) { %><span class="text-success font-mono font-bold">yes</span><% } else { %><span class="text-foreground-faint">—</span><% } %>
                      </td>
                    </tr>
                  <% } %>
                </tbody>
              </table>
            </div>
          </div>
        <% } %>

      </div>
    </details>
  <% } %>
<% } %>
