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

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

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

    $stacked = (new ComponentAttributeBag())->grid(['default' => 1], GridDirection::Column);
    $composeGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'lg' => 2]);
    $actionsGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'sm' => 3]);
    $logsGrid = (new ComponentAttributeBag())->grid(['default' => 1, 'lg' => 2]);
@endphp

<x-filament-panels::page>
    <div {{ $stacked }}>
        <x-filament::section heading="Compose Controls" :icon="Heroicon::RocketLaunch">
            <div {{ $composeGrid }}>
                @forelse($composes as $compose)
                    <x-filament::card :heading="$compose['label']" :description="$compose['compose_id']">
                        <x-slot name="afterHeader">
                            <x-filament::badge
                                :color="$composeStatusColors[$compose['status']] ?? 'gray'"
                                :icon="$composeStatusIcons[$compose['status']] ?? Heroicon::QuestionMarkCircle"
                            >
                                {{ ucfirst($compose['status'] ?? 'unknown') }}
                            </x-filament::badge>
                        </x-slot>

                        <div {{ $actionsGrid }}>
                            <x-filament::button size="sm" color="warning" wire:click="redeploy('{{ $compose['compose_id'] }}')">
                                Redeploy
                            </x-filament::button>
                            <x-filament::button size="sm" color="danger" wire:click="stop('{{ $compose['compose_id'] }}')">
                                Stop
                            </x-filament::button>
                            @if($dokployWebUrl)
                                <x-filament::button
                                    tag="a"
                                    size="sm"
                                    color="gray"
                                    href="{{ rtrim($dokployWebUrl, '/') }}"
                                    target="_blank"
                                    :icon="Heroicon::ArrowTopRightOnSquare"
                                >
                                    Open in Dokploy
                                </x-filament::button>
                            @endif
                        </div>
                    </x-filament::card>
                @empty
                    <x-filament::empty-state heading="No compose stacks configured." :icon="Heroicon::QuestionMarkCircle" />
                @endforelse
            </div>
        </x-filament::section>

        <x-filament::section heading="Recent Actions" :icon="Heroicon::Clock">
            @if(empty($recentLogs))
                <x-filament::empty-state heading="No actions recorded yet." :icon="Heroicon::Clock" />
            @else
                <div {{ $logsGrid }}>
                    @foreach($recentLogs as $log)
                        @php
                            $statusCode = $log['status_code'] ?? null;
                            $statusColor = 'gray';
                            $statusIcon = Heroicon::QuestionMarkCircle;

                            if (is_numeric($statusCode)) {
                                if ($statusCode >= 500) {
                                    $statusColor = 'danger';
                                    $statusIcon = Heroicon::XCircle;
                                } elseif ($statusCode >= 400) {
                                    $statusColor = 'danger';
                                    $statusIcon = Heroicon::ExclamationTriangle;
                                } elseif ($statusCode >= 300) {
                                    $statusColor = 'warning';
                                    $statusIcon = Heroicon::ExclamationTriangle;
                                } elseif ($statusCode >= 200) {
                                    $statusColor = 'success';
                                    $statusIcon = Heroicon::CheckCircle;
                                }
                            }
                        @endphp

                        <x-filament::card :heading="$log['service_id']" :description="$log['created_at']">
                            <div {{ $stacked }}>
                                <x-filament::badge color="gray" :icon="Heroicon::User">
                                    {{ $log['user'] }}
                                </x-filament::badge>
                                <x-filament::badge color="gray" :icon="Heroicon::CommandLine">
                                    {{ ucfirst($log['action']) }}
                                </x-filament::badge>
                                <x-filament::badge :color="$statusColor" :icon="$statusIcon">
                                    {{ $statusCode ?? '—' }}
                                </x-filament::badge>
                            </div>
                        </x-filament::card>
                    @endforeach
                </div>
            @endif

            <x-filament::button
                tag="a"
                href="{{ route('filament.superadmin.resources.infrastructure-action-logs.index') }}"
                color="gray"
                size="sm"
                :icon="Heroicon::ArrowTopRightOnSquare"
            >
                View full log
            </x-filament::button>
        </x-filament::section>
    </div>
</x-filament-panels::page>
