{% set topic_count = widget.topics | length %}
{% set ns_count = widget.namespaces | length %}
{% set declared_count = widget.declared | length %}

{# ===== Stats Row ===== #}
<div class="row g-3 mb-3">
    <div class="col-6 col-lg-3">
        <div class="fp-stat-tile fp-stat-tile--green">
            <div class="fp-stat-tile__value">{{ declared_count }}</div>
            <div class="fp-stat-tile__label">Published</div>
            <i class="ti ti-book fp-stat-tile__icon"></i>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="fp-stat-tile fp-stat-tile--blue">
            <div class="fp-stat-tile__value">{{ topic_count }}</div>
            <div class="fp-stat-tile__label">Subscribed Topics</div>
            <i class="ti ti-antenna fp-stat-tile__icon"></i>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="fp-stat-tile fp-stat-tile--teal">
            <div class="fp-stat-tile__value">{{ widget.total_listeners }}</div>
            <div class="fp-stat-tile__label">Total Listeners</div>
            <i class="ti ti-ear fp-stat-tile__icon"></i>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="fp-stat-tile {% if ns_count %}fp-stat-tile--violet{% else %}fp-stat-tile--dim{% endif %}">
            <div class="fp-stat-tile__value">{{ ns_count }}</div>
            <div class="fp-stat-tile__label">Namespaces</div>
            <i class="ti ti-circles fp-stat-tile__icon"></i>
        </div>
    </div>
</div>

{# ===== Published Events (declared by plugins) ===== #}
<div class="card mb-3 fp-card-accent-green">
    <div class="card-header">
        <h4 class="card-title">
            <i class="ti ti-book me-2" style="color: var(--c-green)"></i>Published Events
        </h4>
        <div class="card-options">
            <span class="fp-meta-chip fp-meta-chip--dim">declared via <code>emits</code> field</span>
        </div>
    </div>
    {% if widget.declared %}
    <div class="card-body p-0">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th style="width:28%">Event</th>
                    <th style="width:16%">Plugin</th>
                    <th style="width:32%">Description</th>
                    <th>Payload</th>
                </tr>
            </thead>
            <tbody>
                {% for item in widget.declared %}
                {% set spec = item.spec %}
                {% set subscribed_topics = widget.topics | map(attribute='topic') | list %}
                {% set is_subscribed = spec.name in subscribed_topics %}
                <tr>
                    <td>
                        <code class="fp-path-code">{{ spec.name }}</code>
                        {% if is_subscribed %}
                            <span class="fp-badge--blue mt-1">subscribed</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if item.plugin_url %}
                            <a href="{{ item.plugin_url }}" class="fp-meta-chip fp-meta-chip--dim text-decoration-none">
                                <i class="ti ti-external-link me-1"></i>{{ item.plugin }}
                            </a>
                        {% else %}
                            <span class="fp-meta-chip fp-meta-chip--dim">{{ item.plugin }}</span>
                        {% endif %}
                    </td>
                    <td class="text-muted" style="font-size: 0.8125rem">
                        {{ spec.description or '—' }}
                    </td>
                    <td>
                        {% if spec.payload %}
                            <div class="d-flex flex-wrap gap-1">
                                {% for field_name, field_type in spec.payload.items() %}
                                    <span class="fp-meta-chip fp-meta-chip--mono">
                                        {{ field_name }}<span class="text-muted">:{{ field_type }}</span>
                                    </span>
                                {% endfor %}
                            </div>
                        {% else %}
                            <span class="fp-meta-none">—</span>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="card-body">
        <div class="fp-dep-empty">
            <i class="ti ti-book-off"></i>
            <span>No plugins have declared events yet. Add an <code>emits</code> list to your plugin class using <code>EventSpec</code> objects.</span>
        </div>
    </div>
    {% endif %}
</div>

{# ===== Active Subscriptions ===== #}
<div class="card mb-3 fp-card-accent-blue">
    <div class="card-header">
        <h4 class="card-title">
            <i class="ti ti-antenna text-primary me-2"></i>Active Subscriptions
        </h4>
        <div class="card-options">
            <span class="fp-meta-chip fp-meta-chip--blue">{{ topic_count }} topic{{ 's' if topic_count != 1 else '' }}</span>
        </div>
    </div>
    {% if widget.topics %}
    <div class="card-body p-0">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th style="width:30%">Topic</th>
                    <th style="width:8%">Count</th>
                    <th>Callables</th>
                </tr>
            </thead>
            <tbody>
                {% for row in widget.topics %}
                <tr>
                    <td>
                        <code class="fp-path-code">{{ row.topic }}</code>
                        {% if row.topic.startswith('fp.') %}
                            <span class="fp-badge--blue ms-1">built-in</span>
                        {% endif %}
                    </td>
                    <td>
                        <span class="fp-meta-chip fp-meta-chip--blue">{{ row.count }}</span>
                    </td>
                    <td>
                        <div class="d-flex flex-wrap gap-1">
                            {% for label in row.listeners %}
                                <code class="fp-meta-chip fp-meta-chip--mono">{{ label }}</code>
                            {% endfor %}
                            {% for flag in row.flags %}
                                <span class="fp-badge--amber ms-1">{{ flag }}</span>
                            {% endfor %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="card-body">
        <div class="fp-dep-empty">
            <i class="ti ti-antenna-off"></i>
            <span>No topic listeners registered yet. Plugins subscribe in <code>on_load_complete</code>.</span>
        </div>
    </div>
    {% endif %}
</div>

{# ===== Namespace Listeners ===== #}
{% if widget.namespaces %}
<div class="card mb-3 fp-card-accent-violet">
    <div class="card-header">
        <h4 class="card-title">
            <i class="ti ti-circles me-2" style="color: var(--c-violet)"></i>Namespace Listeners
        </h4>
        <div class="card-options">
            <span class="fp-meta-chip fp-meta-chip--dim">{{ ns_count }} prefix{{ 'es' if ns_count != 1 else '' }}</span>
        </div>
    </div>
    <div class="card-body p-0">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th style="width:30%">Prefix</th>
                    <th style="width:8%">Count</th>
                    <th>Callables</th>
                </tr>
            </thead>
            <tbody>
                {% for row in widget.namespaces %}
                <tr>
                    <td><code class="fp-path-code">{{ row.prefix }}.*</code></td>
                    <td><span class="fp-meta-chip fp-meta-chip--blue">{{ row.count }}</span></td>
                    <td>
                        <div class="d-flex flex-wrap gap-1">
                            {% for label in row.listeners %}
                                <code class="fp-meta-chip fp-meta-chip--mono">{{ label }}</code>
                            {% endfor %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endif %}
