@page "/"
@inherits DashboardPageBase

<PageTitle>MXAccess Gateway Dashboard</PageTitle>

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

    <section class="metric-grid">
        <MetricCard Label="Uptime" Value="@DashboardDisplay.Duration(Snapshot.GatewayUptime)" Detail="@Snapshot.GatewayVersion" />
        <MetricCard Label="Open Sessions" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.sessions.open"))" />
        <MetricCard Label="Workers Running" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.workers.running"))" />
        <MetricCard Label="Event Queue Depth" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.events.worker_queue.depth"))" />
        <MetricCard Label="Commands Failed" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.commands.failed"))" />
        <MetricCard Label="Events Received" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.events.received"))" />
        <MetricCard Label="Faults" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.faults"))" />
        <MetricCard Label="Queue Overflows" Value="@DashboardDisplay.Count(DashboardDisplay.MetricValue(Snapshot, "mxgateway.queues.overflows"))" />
    </section>

    <section class="dashboard-section">
        <div class="section-heading d-flex align-items-center gap-2">
            <h2>Galaxy Repository</h2>
            <StatusBadge Text="@Snapshot.Galaxy.Status.ToString()" />
            <NavLink class="ms-auto small" href="galaxy">View browse details &rarr;</NavLink>
        </div>
        <div class="metric-grid compact">
            <MetricCard Label="Last Deploy" Value="@DashboardDisplay.DateTime(Snapshot.Galaxy.LastDeployTime)" />
            <MetricCard Label="Objects" Value="@DashboardDisplay.Count(Snapshot.Galaxy.ObjectCount)" Detail="@($"{Snapshot.Galaxy.AreaCount:N0} areas")" />
            <MetricCard Label="Attributes" Value="@DashboardDisplay.Count(Snapshot.Galaxy.AttributeCount)" />
            <MetricCard Label="Historized" Value="@DashboardDisplay.Count(Snapshot.Galaxy.HistorizedAttributeCount)" />
            <MetricCard Label="Alarms" Value="@DashboardDisplay.Count(Snapshot.Galaxy.AlarmAttributeCount)" />
            <MetricCard Label="Last Refresh" Value="@DashboardDisplay.DateTime(Snapshot.Galaxy.LastSuccessAt)" Detail="@GalaxyRefreshDetail()" />
        </div>
        @if (!string.IsNullOrWhiteSpace(Snapshot.Galaxy.LastError))
        {
            @* Overview stays compact; the Galaxy page renders the error in full. *@
            <div class="empty-state mt-2" title="@Snapshot.Galaxy.LastError">@DashboardDisplay.Abbreviate(Snapshot.Galaxy.LastError, 160)</div>
        }
    </section>

    <section class="dashboard-section">
        <div class="section-heading">
            <h2>Recent Faults</h2>
        </div>
        <FaultList Faults="@Snapshot.Faults" />
    </section>
}

@code {
    private string? GalaxyRefreshDetail()
    {
        DashboardGalaxySummary galaxy = Snapshot!.Galaxy;
        if (galaxy.LastQueriedAt is null)
        {
            return "never queried";
        }

        if (galaxy.LastSuccessAt is null)
        {
            return "no successful refresh yet";
        }

        return galaxy.LastQueriedAt > galaxy.LastSuccessAt
            ? $"last attempt {DashboardDisplay.DateTime(galaxy.LastQueriedAt)}"
            : null;
    }
}
