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

    $statusColors = [
        'done' => 'success',
        'deploying' => 'warning',
        'pending' => 'warning',
        'unreachable' => 'danger',
        'error' => 'danger',
        'failed' => 'danger',
    ];

    $statusIcons = [
        'done' => Heroicon::CheckCircle,
        'deploying' => Heroicon::ArrowPath,
        'pending' => Heroicon::Clock,
        'unreachable' => Heroicon::ExclamationTriangle,
        'error' => Heroicon::XCircle,
        'failed' => Heroicon::XCircle,
    ];

    $serviceColors = [
        'running' => 'success',
        'done' => 'success',
        'starting' => 'warning',
        'deploying' => 'warning',
        'unhealthy' => 'danger',
        'error' => 'danger',
        'failed' => 'danger',
    ];

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

<x-filament-widgets::widget>
    <x-filament::section heading="Infra Status (Dokploy)">
        <div {{ $cardsGrid }}>
            @forelse($composes as $compose)
                <x-filament::card
                    :heading="$compose['label']"
                    :description="$compose['name'] ?? '–'"
                >
                    <x-slot name="afterHeader">
                        <x-filament::badge
                            :color="$statusColors[$compose['status']] ?? 'gray'"
                            :icon="$statusIcons[$compose['status']] ?? Heroicon::QuestionMarkCircle"
                        >
                            {{ ucfirst($compose['status']) }}
                        </x-filament::badge>
                        <x-filament::badge color="gray" :icon="Heroicon::Identification">
                            {{ $compose['compose_id'] }}
                        </x-filament::badge>
                    </x-slot>

                    @if(isset($compose['error']))
                        <x-filament::badge color="danger" :icon="Heroicon::ExclamationTriangle">
                            {{ $compose['error'] }}
                        </x-filament::badge>
                    @else
                        <div {{ $serviceGrid }}>
                            @forelse($compose['services'] as $service)
                                <x-filament::badge
                                    :color="$serviceColors[$service['status']] ?? 'gray'"
                                    :icon="array_key_exists($service['status'] ?? '', $serviceColors) ? Heroicon::Server : Heroicon::QuestionMarkCircle"
                                >
                                    {{ $service['name'] }}: {{ strtoupper($service['status'] ?? 'N/A') }}
                                </x-filament::badge>
                            @empty
                                <x-filament::badge color="gray" :icon="Heroicon::QuestionMarkCircle">
                                    No services reported.
                                </x-filament::badge>
                            @endforelse
                            <x-filament::badge color="gray" :icon="Heroicon::Clock">
                                Last deploy:
                                {{ $compose['last_deploy'] ? \Illuminate\Support\Carbon::parse($compose['last_deploy'])->diffForHumans() : '—' }}
                            </x-filament::badge>
                        </div>
                    @endif
                </x-filament::card>
            @empty
                <x-filament::empty-state
                    heading="No Dokploy compose stacks configured."
                    :icon="Heroicon::QuestionMarkCircle"
                />
            @endforelse
        </div>
    </x-filament::section>
</x-filament-widgets::widget>
