@page "/hosts"
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.Admin.Services
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
@inject IServiceScopeFactory ScopeFactory
@implements IDisposable

<h1 class="mb-4">Driver host status</h1>

<div class="d-flex align-items-center mb-3 gap-2">
    <button class="btn btn-sm btn-outline-primary" @onclick="RefreshAsync" disabled="@_refreshing">
        @if (_refreshing) { <span class="spinner-border spinner-border-sm me-1" /> }
        Refresh
    </button>
    <span class="text-muted small">
        Auto-refresh every @RefreshIntervalSeconds s. Last updated: @(_lastRefreshUtc?.ToString("HH:mm:ss 'UTC'") ?? "—")
    </span>
</div>

<div class="alert alert-info small mb-4">
    Each row is one host reported by a driver instance on a server node. Galaxy drivers report
    per-Platform / per-AppEngine entries; Modbus drivers report the PLC endpoint. Rows age out
    of the Server's publisher on every 10-second heartbeat — rows whose LastSeen is older than
    30s are flagged Stale, which usually means the owning Server process has crashed or lost
    its DB connection.
</div>

@if (_rows is null)
{
    <p>Loading…</p>
}
else if (_rows.Count == 0)
{
    <div class="alert alert-secondary">
        No host-status rows yet. The Server publishes its first tick 2s after startup; if this list stays empty, check that the Server is running and the driver implements <code>IHostConnectivityProbe</code>.
    </div>
}
else
{
    <div class="row g-3 mb-4">
        <div class="col-md-3"><div class="card"><div class="card-body">
            <h6 class="text-muted mb-1">Hosts</h6>
            <div class="fs-3">@_rows.Count</div>
        </div></div></div>
        <div class="col-md-3"><div class="card border-success"><div class="card-body">
            <h6 class="text-muted mb-1">Running</h6>
            <div class="fs-3 text-success">@_rows.Count(r => r.State == DriverHostState.Running && !HostStatusService.IsStale(r))</div>
        </div></div></div>
        <div class="col-md-3"><div class="card border-warning"><div class="card-body">
            <h6 class="text-muted mb-1">Stale</h6>
            <div class="fs-3 text-warning">@_rows.Count(HostStatusService.IsStale)</div>
        </div></div></div>
        <div class="col-md-3"><div class="card border-danger"><div class="card-body">
            <h6 class="text-muted mb-1">Faulted</h6>
            <div class="fs-3 text-danger">@_rows.Count(r => r.State == DriverHostState.Faulted)</div>
        </div></div></div>
    </div>

    @if (_rows.Any(HostStatusService.IsFlagged))
    {
        var flaggedCount = _rows.Count(HostStatusService.IsFlagged);
        <div class="alert alert-danger small mb-3">
            <strong>@flaggedCount host@(flaggedCount == 1 ? "" : "s")</strong>
            reporting ≥ @HostStatusService.FailureFlagThreshold consecutive failures — circuit breaker
            may trip soon. Inspect the resilience columns below to locate.
        </div>
    }

    @foreach (var cluster in _rows.GroupBy(r => r.ClusterId ?? "(unassigned)").OrderBy(g => g.Key))
    {
        <h2 class="h5 mt-4">Cluster: <code>@cluster.Key</code></h2>
        <table class="table table-sm table-hover align-middle">
            <thead>
                <tr>
                    <th>Node</th>
                    <th>Driver</th>
                    <th>Host</th>
                    <th>State</th>
                    <th class="text-end" title="Consecutive failures — resets when a call succeeds or the breaker closes">Fail#</th>
                    <th class="text-end" title="In-flight capability calls (bulkhead-depth proxy)">In-flight</th>
                    <th>Breaker opened</th>
                    <th>Last transition</th>
                    <th>Last seen</th>
                    <th>Detail</th>
                </tr>
            </thead>
            <tbody>
            @foreach (var r in cluster)
            {
                <tr class="@RowClass(r)">
                    <td><code>@r.NodeId</code></td>
                    <td><code>@r.DriverInstanceId</code></td>
                    <td>@r.HostName</td>
                    <td>
                        <span class="badge @StateBadge(r.State)">@r.State</span>
                        @if (HostStatusService.IsStale(r))
                        {
                            <span class="badge bg-warning text-dark ms-1">Stale</span>
                        }
                        @if (HostStatusService.IsFlagged(r))
                        {
                            <span class="badge bg-danger ms-1" title="≥ @HostStatusService.FailureFlagThreshold consecutive failures">Flagged</span>
                        }
                    </td>
                    <td class="text-end small @(HostStatusService.IsFlagged(r) ? "text-danger fw-bold" : "")">
                        @r.ConsecutiveFailures
                    </td>
                    <td class="text-end small">@r.CurrentBulkheadDepth</td>
                    <td class="small">
                        @(r.LastCircuitBreakerOpenUtc is null ? "—" : FormatAge(r.LastCircuitBreakerOpenUtc.Value))
                    </td>
                    <td class="small">@FormatAge(r.StateChangedUtc)</td>
                    <td class="small @(HostStatusService.IsStale(r) ? "text-warning" : "")">@FormatAge(r.LastSeenUtc)</td>
                    <td class="text-truncate small" style="max-width: 240px;" title="@r.Detail">@r.Detail</td>
                </tr>
            }
            </tbody>
        </table>
    }
}

@code {
    // Mirrors HostStatusPublisher.HeartbeatInterval — polling ahead of the broadcaster
    // produces stale-looking rows mid-cycle.
    private const int RefreshIntervalSeconds = 10;

    private List<HostStatusRow>? _rows;
    private bool _refreshing;
    private DateTime? _lastRefreshUtc;
    private Timer? _timer;

    protected override async Task OnInitializedAsync()
    {
        await RefreshAsync();
        _timer = new Timer(async _ => await InvokeAsync(RefreshAsync),
            state: null,
            dueTime: TimeSpan.FromSeconds(RefreshIntervalSeconds),
            period: TimeSpan.FromSeconds(RefreshIntervalSeconds));
    }

    private async Task RefreshAsync()
    {
        if (_refreshing) return;
        _refreshing = true;
        try
        {
            using var scope = ScopeFactory.CreateScope();
            var svc = scope.ServiceProvider.GetRequiredService<HostStatusService>();
            _rows = (await svc.ListAsync()).ToList();
            _lastRefreshUtc = DateTime.UtcNow;
        }
        finally
        {
            _refreshing = false;
            StateHasChanged();
        }
    }

    private static string RowClass(HostStatusRow r) => r.State switch
    {
        DriverHostState.Faulted => "table-danger",
        _ when HostStatusService.IsStale(r) => "table-warning",
        _ => "",
    };

    private static string StateBadge(DriverHostState s) => s switch
    {
        DriverHostState.Running => "bg-success",
        DriverHostState.Stopped => "bg-secondary",
        DriverHostState.Faulted => "bg-danger",
        _ => "bg-secondary",
    };

    private static string FormatAge(DateTime t)
    {
        var age = DateTime.UtcNow - t;
        if (age.TotalSeconds < 60) return $"{(int)age.TotalSeconds}s ago";
        if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m ago";
        if (age.TotalHours < 24) return $"{(int)age.TotalHours}h ago";
        return t.ToString("yyyy-MM-dd HH:mm 'UTC'");
    }

    public void Dispose() => _timer?.Dispose();
}
