@extends('layouts.panel')

@section('title', 'System Health')

@section('content')
    <section class="metric-grid">
        @foreach ($breakdown as $label => $value)
            <article class="metric-card">
                <p class="eyebrow">{{ str($label)->headline() }}</p>
                <strong>{{ $value }}</strong>
                <p class="muted">Websites in this health state</p>
            </article>
        @endforeach
    </section>

    <section class="hero">
        <p class="eyebrow">Fleet Telemetry</p>
        <h1>Availability, response time, database footprint, and error trends.</h1>
        <div class="stat-strip">
            <div class="stat-pill">
                <p class="eyebrow">Average Latency</p>
                <strong>{{ $headlineStats['average_latency'] }} ms</strong>
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Tracked Tables</p>
                <strong>{{ number_format($headlineStats['tracked_tables']) }}</strong>
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Error Rate</p>
                <strong>{{ $headlineStats['error_rate'] }}%</strong>
            </div>
        </div>
    </section>

    <section class="card">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Website</th>
                        <th>Status</th>
                        <th>Health</th>
                        <th>Response</th>
                        <th>Last Seen</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($sites as $site)
                        <tr>
                            <td>{{ $site->name }}</td>
                            <td><x-status-badge :value="$site->status" /></td>
                            <td><x-status-badge :value="$site->health_status" /></td>
                            <td>{{ $site->response_time_ms ? $site->response_time_ms.' ms' : 'N/A' }}</td>
                            <td>{{ $site->last_seen_at?->diffForHumans() ?? 'Never' }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="muted">No health data available.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{ $sites->links() }}
    </section>
@endsection
