@php
    use Filament\Support\Enums\GridDirection;
    use Filament\Support\Icons\Heroicon;
    use Illuminate\View\ComponentAttributeBag;

    $statusColors = [
        'healthy' => 'success',
        'pending' => 'warning',
        'degraded' => 'danger',
        'unconfigured' => 'danger',
        'unknown' => 'gray',
    ];

    $statusIcons = [
        'healthy' => Heroicon::CheckCircle,
        'pending' => Heroicon::Clock,
        'degraded' => Heroicon::ExclamationTriangle,
        'unconfigured' => Heroicon::XCircle,
        'unknown' => Heroicon::QuestionMarkCircle,
    ];

    $cardsGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'lg' => 2]);
    $metricsGrid = (new ComponentAttributeBag())->grid(['default' => 1], GridDirection::Column);
@endphp

<x-filament-widgets::widget>
    <x-filament::section heading="{{ __('admin.integrations_health.heading') }}">
        <div {{ $cardsGrid }}>
            @forelse($providers as $provider)
                <x-filament::card
                    :heading="$provider['label']"
                    :description="$provider['config_label']"
                >
                    <x-slot name="afterHeader">
                        <x-filament::badge :color="$provider['is_configured'] ? 'success' : 'danger'">
                            {{ $provider['is_configured'] ? __('admin.integrations_health.configured') : __('admin.integrations_health.unconfigured') }}
                        </x-filament::badge>
                        <x-filament::badge
                            :color="$statusColors[$provider['status']] ?? 'gray'"
                            :icon="$statusIcons[$provider['status']] ?? Heroicon::QuestionMarkCircle"
                        >
                            {{ $provider['status_label'] }}
                        </x-filament::badge>
                    </x-slot>

                    <div {{ $metricsGrid }}>
                        <x-filament::badge color="gray" :icon="Heroicon::Clock">
                            {{ __('admin.integrations_health.last_received') }}:
                            {{ optional(data_get($provider, 'last_event.received_at'))->diffForHumans() ?? '—' }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::ArrowPath">
                            {{ __('admin.integrations_health.last_processed') }}:
                            {{ optional(data_get($provider, 'last_processed.processed_at'))->diffForHumans() ?? '—' }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::Clock">
                            {{ __('admin.integrations_health.processing_lag') }}:
                            {{ $provider['processing_lag']['label'] ?? '—' }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::QueueList">
                            {{ __('admin.integrations_health.pending_events') }}:
                            {{ number_format($provider['pending_count']) }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::ExclamationTriangle">
                            {{ __('admin.integrations_health.recent_failures') }}:
                            {{ number_format($provider['recent_failures']) }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::RectangleStack">
                            {{ __('admin.integrations_health.queue_backlog') }}:
                            {{ number_format($provider['queue_backlog']) }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::XCircle">
                            {{ __('admin.integrations_health.failed_jobs') }}:
                            {{ number_format($provider['failed_jobs']) }}
                        </x-filament::badge>
                        @if(! empty(data_get($provider, 'last_failed.error_message')))
                            <x-filament::badge color="danger" :icon="Heroicon::ExclamationTriangle">
                                {{ __('admin.integrations_health.last_error') }}:
                                {{ data_get($provider, 'last_failed.error_message') }}
                            </x-filament::badge>
                        @endif
                    </div>
                </x-filament::card>
            @empty
                <x-filament::empty-state
                    :heading="__('admin.integrations_health.empty')"
                    :icon="Heroicon::QuestionMarkCircle"
                />
            @endforelse
        </div>
    </x-filament::section>
</x-filament-widgets::widget>
