@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('admin.title')],
        ['label' => __('admin.system_health.title')],
    ];
    $c = config('theme.colors');
@endphp

<x-ui.layout.page
    :title="__('admin.system_health.title')"
    :subtitle="__('admin.system_health.subtitle')"
    page-icon="fe fe-activity"
    :breadcrumbs="$breadcrumbs"
>
    <div class="row row-cards">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">Deployment readiness</h3>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center gap-3">
                        <span class="badge {{ $readiness['ok'] ? 'bg-success' : 'bg-danger' }}">
                            {{ $readiness['ok'] ? 'OK' : 'BLOCKED' }}
                        </span>
                        <span class="text-muted">Queue backlog: {{ (int) ($queue_backlog['total'] ?? 0) }}</span>
                    </div>
                    <div class="mt-3">
                        <ul class="mb-0">
                            @foreach ($readiness['checks'] as $chk)
                                <li>
                                    <strong>{{ $chk['key'] }}</strong>:
                                    <span class="{{ $chk['ok'] ? 'text-success' : 'text-danger' }}">{{ $chk['ok'] ? 'ok' : 'fail' }}</span>
                                    @if (!empty($chk['detail']))
                                        <span class="text-muted">— {{ $chk['detail'] }}</span>
                                    @endif
                                </li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">Backup health</h3>
                </div>
                <div class="card-body">
                    @foreach (['db' => 'Database', 'files' => 'Files'] as $k => $label)
                        @php($h = $backup_health[$k] ?? null)
                        <div class="mb-2">
                            <strong>{{ $label }}</strong>:
                            <span class="{{ ($h['ok'] ?? false) ? 'text-success' : 'text-danger' }}">
                                {{ ($h['ok'] ?? false) ? 'ok' : 'fail' }}
                            </span>
                            <span class="text-muted">
                                — last {{ ($h['last_success_at'] ?? null)?->toDateTimeString() ?? '—' }}
                                @if (!empty($h['reason']))
                                    ({{ $h['reason'] }})
                                @endif
                            </span>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">Queue backlog & retry spikes</h3>
                </div>
                <div class="card-body">
                    <div class="text-muted mb-2">Total queued: {{ (int) ($queue_backlog['total'] ?? 0) }}</div>
                    <ul class="mb-3">
                        @foreach ($queue_backlog as $k => $v)
                            @if ($k !== 'total')
                                <li><strong>{{ $k }}</strong>: {{ (int) $v }}</li>
                            @endif
                        @endforeach
                    </ul>
                    <div class="text-muted mb-2">Retry threshold: {{ (int) ($retry_spikes['threshold'] ?? 0) }}</div>
                    <ul class="mb-0">
                        <li><strong>exports</strong>: {{ (int) ($retry_spikes['exports'] ?? 0) }}</li>
                        <li><strong>payroll</strong>: {{ (int) ($retry_spikes['payroll'] ?? 0) }}</li>
                        <li><strong>attendance</strong>: {{ (int) ($retry_spikes['attendance'] ?? 0) }}</li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.failed_exports') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_tenant') }}</th>
                                    <th>{{ __('admin.system_health.col_status') }}</th>
                                    <th>{{ __('admin.system_health.col_failed_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($failed_exports as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ $row->tenant_id }}</td>
                                        <td>{{ $row->status }}</td>
                                        <td>{{ $row->failed_at?->toDateTimeString() ?? '—' }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.stuck_exports') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_tenant') }}</th>
                                    <th>{{ __('admin.system_health.col_started_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($stuck_exports as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ $row->tenant_id }}</td>
                                        <td>{{ $row->started_at?->toDateTimeString() ?? '—' }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="3" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.stuck_payroll') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_tenant') }}</th>
                                    <th>{{ __('admin.system_health.col_updated_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($stuck_payroll_periods as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ $row->tenant_id }}</td>
                                        <td>{{ $row->updated_at?->toDateTimeString() ?? '—' }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="3" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.failed_jobs_payroll') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_failed_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($failed_snapshot_jobs as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ \Illuminate\Support\Carbon::parse($row->failed_at)->toDateTimeString() }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="2" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.failed_jobs_biometric') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_failed_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($failed_biometric_jobs as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ \Illuminate\Support\Carbon::parse($row->failed_at)->toDateTimeString() }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="2" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.system_health.failed_jobs_attendance') }}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-vcenter card-table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.system_health.col_failed_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($failed_attendance_detection_jobs as $row)
                                    <tr>
                                        <td>{{ $row->id }}</td>
                                        <td>{{ \Illuminate\Support\Carbon::parse($row->failed_at)->toDateTimeString() }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="2" class="text-muted">{{ __('admin.system_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-ui.layout.page>
