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

<x-ui.layout.page
    :title="__('admin.integrations_health.title')"
    :subtitle="__('admin.integrations_health.subtitle')"
    page-icon="fe fe-link"
    :breadcrumbs="$breadcrumbs"
>
    <div class="row row-cards">
        <div class="col-12 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.integrations_health.webhooks') }}</h3>
                </div>
                <div class="card-body">
                    <div class="text-muted mb-2">{{ __('admin.integrations_health.window_24h') }}</div>
                    <ul class="mb-0">
                        <li>{{ __('admin.integrations_health.dead_letters') }}: <strong>{{ (int) ($dead_letters_24h ?? 0) }}</strong></li>
                        <li>{{ __('admin.integrations_health.retry_storms') }}: <strong>{{ (int) ($retry_storms_1h ?? 0) }}</strong></li>
                        <li>{{ __('admin.integrations_health.failing_streams') }}:
                            <strong>{{ (int) ($failing_webhook_streams_24h ?? 0) }}</strong></li>
                        <li>{{ __('admin.integrations_health.slow_deliveries') }}:
                            <strong>{{ (int) ($slow_webhooks['slow_count'] ?? 0) }}</strong>
                            @if (!empty($slow_webhooks['avg']))
                                (avg {{ (int) $slow_webhooks['avg'] }} ms)
                            @endif
                        </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.integrations_health.api_security') }}</h3>
                </div>
                <div class="card-body">
                    @php($sec = $api_security ?? [])
                    <ul class="mb-2">
                        @foreach (($sec['security_events'] ?? []) as $type => $count)
                            <li><code>{{ $type }}</code>: {{ (int) $count }}</li>
                        @endforeach
                    </ul>
                    <div class="text-muted">{{ __('admin.integrations_health.rate_limit_audit_429') }}:
                        <strong>{{ (int) ($sec['api_audit_429'] ?? 0) }}</strong></div>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title" style="color: {{ $c['text'] }}">{{ __('admin.integrations_health.dead_letter_table') }}</h3>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('admin.integrations_health.col_tenant') }}</th>
                                    <th>{{ __('admin.integrations_health.col_webhook') }}</th>
                                    <th>{{ __('admin.integrations_health.col_attempts') }}</th>
                                    <th>{{ __('admin.integrations_health.col_dead_at') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($dead_letter_samples ?? [] as $row)
                                    <tr>
                                        <td>{{ (int) ($row['id'] ?? 0) }}</td>
                                        <td>{{ (int) ($row['tenant_id'] ?? 0) }}</td>
                                        <td>{{ $row['webhook_name'] ?? '—' }} (#{{ (int) ($row['webhook_id'] ?? 0) }})</td>
                                        <td>{{ (int) ($row['attempt_count'] ?? 0) }}</td>
                                        <td>{{ $row['dead_lettered_at'] ?? '—' }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="text-muted">{{ __('admin.integrations_health.none') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-ui.layout.page>
