@page "/events"
@inherits DashboardPageBase

<PageTitle>Dashboard Events</PageTitle>

@if (Snapshot is null)
{
    <div class="empty-state">Loading event diagnostics.</div>
}
else
{
    <div class="dashboard-page-header">
        <div>
            <h1>Events</h1>
            <div class="text-secondary">Generated @DashboardDisplay.DateTime(Snapshot.GeneratedAt)</div>
        </div>
    </div>

    <section class="metric-grid compact">
        <MetricCard Label="Events Received" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.events.received"))" />
        <MetricCard Label="Worker Event Queue Depth" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.events.worker_queue.depth"))" />
        <MetricCard Label="Stream Queue Depth" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.events.grpc_stream_queue.depth"))" />
        <MetricCard Label="Queue Overflows" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.queues.overflows"))" />
        <MetricCard Label="Stream Disconnects" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.grpc.streams.disconnected"))" />
    </section>

    <section class="dashboard-section">
        <div class="section-heading">
            <h2>Event Families</h2>
        </div>
        @if (EventFamilyMetrics.Count == 0)
        {
            <div class="empty-state">No event family counters recorded.</div>
        }
        else
        {
            <div class="table-responsive">
                <table class="table table-sm dashboard-table">
                    <thead>
                        <tr>
                            <th scope="col">Family</th>
                            <th scope="col">Count</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (DashboardMetricSummary metric in EventFamilyMetrics)
                        {
                            <tr>
                                <td>@metric.Dimension</td>
                                <td>@DashboardDisplay.Count(metric.Value)</td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>
}

@code {
    private IReadOnlyList<DashboardMetricSummary> EventFamilyMetrics => Snapshot?.Metrics
        .Where(metric => metric.Name == "mxgateway.events.received" && metric.Dimension is not null)
        .OrderBy(metric => metric.Dimension, StringComparer.OrdinalIgnoreCase)
        .ToArray() ?? [];
}
