@page "/superadmin/system/health"
@using global::System.Net.Http
@layout WebClientTpos.Client.Layout.SuperAdminLayout
@inherits SuperAdminBase
@inject SuperAdminApiService Api
@using WebClientTpos.Client.Services

<PageTitle>Trạng thái hệ thống — aPOS Super Admin</PageTitle>

<div class="sa-topbar">
    <div class="sa-topbar__left">
        <h1 class="sa-topbar__title">Trạng thái hệ thống</h1>
        <p class="sa-topbar__subtitle">Giám sát real-time tất cả microservices</p>
    </div>
    <div class="sa-topbar__right">
        <button class="sa-btn-primary" @onclick="LoadAsync">
            <i data-lucide="refresh-cw" style="width:14px;height:14px;"></i>
            Kiểm tra lại
        </button>
    </div>
</div>

<div class="sa-content">
    @* Overall Status *@
    <div style="padding:20px;background:var(--sa-bg-elevated);border:1px solid var(--sa-border);border-radius:12px;display:flex;align-items:center;gap:16px;">
        <div style="width:48px;height:48px;border-radius:12px;background:@(_overallColor)20;display:flex;align-items:center;justify-content:center;">
            <i data-lucide="@_overallIcon" style="width:24px;height:24px;color:@_overallColor;"></i>
        </div>
        <div>
            <div style="font-size:18px;font-weight:700;color:var(--sa-text-primary);">@_overallStatus</div>
            <div style="font-size:13px;color:var(--sa-text-tertiary);">
                @_healthyCount/@_services.Count services hoạt động bình thường
                @if (_lastCheck != null)
                {
                    <span> • Cập nhật lúc @_lastCheck.Value.ToLocalTime().ToString("HH:mm:ss")</span>
                }
            </div>
        </div>
    </div>

    @* Service Grid *@
    <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(280px, 1fr));gap:16px;">
        @foreach (var svc in _services)
        {
            <div class="sa-panel">
                <div class="sa-panel__body" style="display:flex;align-items:center;justify-content:space-between;">
                    <div style="display:flex;align-items:center;gap:12px;">
                        <div style="width:10px;height:10px;border-radius:50%;background:@GetColor(svc.Status);"></div>
                        <div>
                            <div style="font-size:14px;font-weight:600;color:var(--sa-text-primary);">@svc.Name</div>
                            @if (svc.ResponseTimeMs >= 0)
                            {
                                <div style="font-size:12px;color:var(--sa-text-tertiary);">@(svc.ResponseTimeMs)ms</div>
                            }
                        </div>
                    </div>
                    <span class="sa-badge @GetBadge(svc.Status)">@GetLabel(svc.Status)</span>
                </div>
            </div>
        }
    </div>

    @if (!_services.Any() && !IsLoading)
    {
        <div class="sa-panel">
            <div class="sa-empty">
                <div class="sa-empty__icon">
                    <i data-lucide="activity" style="width:24px;height:24px;color:var(--sa-text-tertiary);"></i>
                </div>
                <p>Nhấn "Kiểm tra lại" để xem trạng thái</p>
            </div>
        </div>
    }
</div>

@code {
    private List<SuperAdminApiService.ServiceHealthDto> _services = new();
    private DateTime? _lastCheck;

    private int _healthyCount => _services.Count(s => s.Status == "Healthy");
    private string _overallStatus => _healthyCount == _services.Count && _services.Any() ? "Tất cả hệ thống hoạt động bình thường"
        : _healthyCount == 0 && _services.Any() ? "Sự cố nghiêm trọng" : _services.Any() ? "Một số dịch vụ gặp sự cố" : "Đang kiểm tra...";
    private string _overallColor => _healthyCount == _services.Count && _services.Any() ? "#22C55E"
        : _healthyCount == 0 && _services.Any() ? "#EF4444" : "#F59E0B";
    private string _overallIcon => _healthyCount == _services.Count && _services.Any() ? "check-circle" : "alert-triangle";

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await LoadAsync();
    }

    private async Task LoadAsync()
    {
        IsLoading = true;
        StateHasChanged();
        _services = await Api.GetSystemHealthAsync();
        _lastCheck = DateTime.UtcNow;
        IsLoading = false;
        StateHasChanged();
    }

    private static string GetColor(string s) => s switch { "Healthy" => "#22C55E", "Degraded" => "#F59E0B", _ => "#EF4444" };
    private static string GetBadge(string s) => s switch { "Healthy" => "sa-badge--success", "Degraded" => "sa-badge--warning", _ => "sa-badge--danger" };
    private static string GetLabel(string s) => s switch { "Healthy" => "Khỏe mạnh", "Degraded" => "Chậm", _ => "Không khả dụng" };
}
