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

    $severityColors = [
        'ok' => 'success',
        'warning' => 'warning',
        'critical' => 'danger',
        'unknown' => 'gray',
    ];

    $severityIcons = [
        'ok' => Heroicon::CheckCircle,
        'warning' => Heroicon::ExclamationTriangle,
        'critical' => Heroicon::XCircle,
        'unknown' => Heroicon::QuestionMarkCircle,
    ];

    $summaryGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'sm' => 2, 'lg' => 4]);
    $summaryRow = (new ComponentAttributeBag())->grid(['default' => 2]);
    $mainGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'xl' => 3]);
    $queuesColumn = (new ComponentAttributeBag())->gridColumn(['default' => 'full', 'xl' => 2]);
    $alertsColumn = (new ComponentAttributeBag())->gridColumn(['default' => 'full', 'xl' => 1]);
    $stacked = (new ComponentAttributeBag())->grid(['default' => 1], GridDirection::Column);
@endphp

<x-filament-widgets::widget>
    <x-filament::section
        :heading="__('admin.ops_health.queue.heading')"
        :description="__('admin.ops_health.queue.description')"
        :icon="Heroicon::QueueList"
    >
        <x-slot name="afterHeader">
            <x-filament::badge color="gray" :icon="Heroicon::Clock">
                {{ $snapshotLabel }}
            </x-filament::badge>
            <x-filament::badge :color="$alertCount > 0 ? 'danger' : 'success'" :icon="Heroicon::BellAlert">
                {{ __('admin.ops_health.queue.alerts_heading') }}: {{ number_format($alertCount) }}
            </x-filament::badge>
            @if($stalledAssets > 0)
                <x-filament::badge color="warning" :icon="Heroicon::ExclamationTriangle">
                    {{ __('admin.ops_health.queue.stalled_assets', ['minutes' => $stalledMinutes]) }}:
                    {{ number_format($stalledAssets) }}
                </x-filament::badge>
            @endif
        </x-slot>

        @if($snapshotMissing)
            <x-filament::empty-state
                :heading="__('admin.ops_health.queue.no_snapshot')"
                :description="__('admin.ops_health.queue.description')"
                :icon="Heroicon::ExclamationTriangle"
            />
        @else
            <div {{ $summaryGrid }}>
                @foreach($statusSummary as $summary)
                    <x-filament::card>
                        <div {{ $summaryRow }}>
                            <x-filament::badge
                                :color="$severityColors[$summary['severity']] ?? 'gray'"
                                :icon="$severityIcons[$summary['severity']] ?? Heroicon::QuestionMarkCircle"
                            >
                                {{ $summary['label'] }}
                            </x-filament::badge>
                            <x-filament::badge color="gray">
                                {{ number_format($summary['count']) }}
                            </x-filament::badge>
                        </div>
                    </x-filament::card>
                @endforeach
            </div>

            <div {{ $mainGrid }}>
                <div {{ $queuesColumn }}>
                    <div {{ $stacked }}>
                        @forelse($queues as $queue)
                            <x-filament::card
                                :heading="$queue['name']"
                                :description="__('admin.ops_health.queue.thresholds') . ': ' . (($queue['warning'] || $queue['critical']) ? 'W ' . number_format($queue['warning']) . ' / C ' . number_format($queue['critical']) : '-')"
                            >
                                <x-slot name="afterHeader">
                                    <x-filament::badge
                                        :color="$severityColors[$queue['severity']] ?? 'gray'"
                                        :icon="$severityIcons[$queue['severity']] ?? Heroicon::QuestionMarkCircle"
                                    >
                                        {{ __('admin.ops_health.severity.'.$queue['severity']) }}
                                    </x-filament::badge>
                                </x-slot>

                                <div {{ $stacked }}>
                                    <x-filament::badge color="gray" :icon="Heroicon::QueueList">
                                        {{ __('admin.ops_health.queue.size') }}: {{ $queue['size_label'] }}
                                    </x-filament::badge>
                                    <x-filament::badge color="gray" :icon="Heroicon::ExclamationCircle">
                                        {{ __('admin.ops_health.queue.failed') }}: {{ $queue['failed_label'] }}
                                    </x-filament::badge>
                                    <x-filament::badge color="gray" :icon="Heroicon::ArrowTrendingUp">
                                        {{ __('admin.ops_health.queue.utilization') }}: {{ $queue['utilization_label'] }}
                                    </x-filament::badge>
                                </div>
                            </x-filament::card>
                        @empty
                            <x-filament::empty-state
                                :heading="__('admin.ops_health.queue.no_queues')"
                                :icon="Heroicon::QueueList"
                                compact
                            />
                        @endforelse
                    </div>
                </div>

                <div {{ $alertsColumn }}>
                    <x-filament::card :heading="__('admin.ops_health.queue.alerts_heading')">
                        <div {{ $stacked }}>
                            @forelse($alerts as $alert)
                                <x-filament::card compact>
                                    <x-slot name="afterHeader">
                                        <x-filament::badge
                                            :color="$severityColors[$alert['severity']] ?? 'gray'"
                                            :icon="$severityIcons[$alert['severity']] ?? Heroicon::QuestionMarkCircle"
                                        >
                                            {{ __('admin.ops_health.severity.'.$alert['severity']) }}
                                        </x-filament::badge>
                                    </x-slot>

                                    <div {{ $stacked }}>
                                        @if($alert['queue'])
                                            <x-filament::badge color="gray" :icon="Heroicon::QueueList">
                                                {{ $alert['queue'] }}
                                            </x-filament::badge>
                                        @endif
                                        <x-filament::badge color="gray" :icon="Heroicon::BellAlert">
                                            @switch($alert['type'])
                                                @case('size')
                                                    {{ __('admin.ops_health.alert_types.size', ['size' => number_format($alert['size'] ?? 0)]) }}
                                                    @break
                                                @case('failed_jobs')
                                                    {{ __('admin.ops_health.alert_types.failed_jobs', ['failed' => number_format($alert['failed'] ?? 0)]) }}
                                                    @break
                                                @case('pending_assets')
                                                    {{ __('admin.ops_health.alert_types.pending_assets', ['count' => number_format($alert['count'] ?? 0), 'minutes' => $alert['older_than_minutes'] ?? $stalledMinutes]) }}
                                                    @break
                                                @default
                                                    {{ __('admin.ops_health.snapshot_missing') }}
                                            @endswitch
                                        </x-filament::badge>
                                    </div>
                                </x-filament::card>
                            @empty
                                <x-filament::empty-state
                                    :heading="__('admin.ops_health.queue.alerts_empty')"
                                    :icon="Heroicon::BellAlert"
                                    compact
                                />
                            @endforelse
                        </div>
                    </x-filament::card>
                </div>
            </div>
        @endif
    </x-filament::section>
</x-filament-widgets::widget>
