@using EntKube.Web.Data
@using EntKube.Web.Services
@inject IncidentService IncidentService

@if (rows.Count == 0 && !isLoading)
{
    <div class="text-muted small text-center py-2">No uptime data yet — health snapshots are collected every 5 minutes.</div>
}
else if (isLoading)
{
    <div class="text-muted small text-center py-2"><span class="bi bi-hourglass-split me-1"></span> Loading…</div>
}
else
{
    <div class="table-responsive">
        <table class="table table-sm align-middle mb-0">
            <thead class="table-light">
                <tr>
                    <th>Deployment</th>
                    <th>App</th>
                    <th class="text-end">7-day uptime</th>
                    <th class="text-end">30-day uptime</th>
                    <th class="text-end">SLA target</th>
                    <th class="text-end">Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach (SlaRow row in rows)
                {
                    bool met = !row.Uptime30d.HasValue || !row.SlaTarget.HasValue || row.Uptime30d.Value >= row.SlaTarget.Value;
                    <tr>
                        <td class="fw-semibold small">@row.DeploymentName</td>
                        <td class="text-muted small">@row.AppName</td>
                        <td class="text-end">
                            @if (row.Uptime7d.HasValue)
                            {
                                <span class="@UptimeColor(row.Uptime7d.Value, row.SlaTarget)">@row.Uptime7d.Value.ToString("F2")%</span>
                            }
                            else { <span class="text-muted">—</span> }
                        </td>
                        <td class="text-end">
                            @if (row.Uptime30d.HasValue)
                            {
                                <span class="@UptimeColor(row.Uptime30d.Value, row.SlaTarget)">@row.Uptime30d.Value.ToString("F2")%</span>
                            }
                            else { <span class="text-muted">—</span> }
                        </td>
                        <td class="text-end text-muted small">
                            @(row.SlaTarget.HasValue ? $"{row.SlaTarget.Value:F1}%" : "—")
                        </td>
                        <td class="text-end">
                            @if (!row.Uptime30d.HasValue)
                            {
                                <span class="text-muted small">No data</span>
                            }
                            else if (!row.SlaTarget.HasValue)
                            {
                                <span class="text-muted small">No target</span>
                            }
                            else if (met)
                            {
                                <span class="badge bg-success"><span class="bi bi-check me-1"></span>Meeting SLA</span>
                            }
                            else
                            {
                                <span class="badge bg-danger"><span class="bi bi-exclamation-triangle me-1"></span>Below SLA</span>
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@code {
    [Parameter] public required List<AppDeployment> Deployments { get; set; }
    [Parameter] public Guid CustomerId { get; set; }
    [Parameter] public Guid TenantId { get; set; }

    private List<SlaRow> rows = [];
    private bool isLoading = true;

    protected override async Task OnParametersSetAsync() => await Load();

    private async Task Load()
    {
        isLoading = true;
        StateHasChanged();

        List<SlaRow> result = [];
        foreach (AppDeployment dep in Deployments)
        {
            UptimeResult u7 = await IncidentService.GetUptimeAsync(dep.Id, 7);
            UptimeResult u30 = await IncidentService.GetUptimeAsync(dep.Id, 30);
            SlaTarget? sla = await IncidentService.GetSlaTargetAsync(TenantId, CustomerId, dep.AppId);

            result.Add(new SlaRow(
                dep.Name,
                dep.App?.Name ?? dep.AppId.ToString(),
                u7.Percent,
                u30.Percent,
                sla?.TargetPercent));
        }
        rows = result.OrderBy(r => r.AppName).ThenBy(r => r.DeploymentName).ToList();
        isLoading = false;
    }

    private static string UptimeColor(double pct, double? target)
    {
        double threshold = target ?? 99.0;
        if (pct >= threshold) return "text-success fw-semibold";
        if (pct >= threshold - 2) return "text-warning fw-semibold";
        return "text-danger fw-semibold";
    }

    private record SlaRow(
        string DeploymentName,
        string AppName,
        double? Uptime7d,
        double? Uptime30d,
        double? SlaTarget);
}
