@php
    $fieldWrapperView = $getFieldWrapperView();

    $results = $getState() ?? [];
    $results = is_array($results) ? $results : [];

    $summary = $summary ?? [];
    $summary = is_array($summary) ? $summary : [];

    $blocking = (int) ($summary['blocking'] ?? 0);
    $warning = (int) ($summary['warning'] ?? 0);
    $safe = (int) ($summary['safe'] ?? 0);

    $ranAt = $ranAt ?? null;
    $ranAtLabel = null;

    if (is_string($ranAt) && $ranAt !== '') {
        try {
            $ranAtLabel = \Carbon\CarbonImmutable::parse($ranAt)->format('Y-m-d H:i');
        } catch (\Throwable) {
            $ranAtLabel = $ranAt;
        }
    }

    $severityColor = static function (?string $severity): string {
        return match ($severity) {
            'blocking' => 'danger',
            'warning' => 'warning',
            default => 'success',
        };
    };

    $limitedList = static function (array $items, int $limit = 5): array {
        if (count($items) <= $limit) {
            return $items;
        }

        return array_slice($items, 0, $limit);
    };
@endphp

<x-dynamic-component :component="$fieldWrapperView" :field="$field">
    <div class="space-y-4">
        <x-filament::section
            heading="Safety checks"
            :description="$ranAtLabel ? ('Last run: ' . $ranAtLabel) : 'Run checks to evaluate risk before previewing.'"
        >
            <div class="flex flex-wrap gap-2">
                <x-filament::badge :color="$blocking > 0 ? 'danger' : 'gray'">
                    {{ $blocking }} blocking
                </x-filament::badge>
                <x-filament::badge :color="$warning > 0 ? 'warning' : 'gray'">
                    {{ $warning }} warnings
                </x-filament::badge>
                <x-filament::badge :color="$safe > 0 ? 'success' : 'gray'">
                    {{ $safe }} safe
                </x-filament::badge>
            </div>
        </x-filament::section>

        @if ($results === [])
            <x-filament::section>
                <div class="text-sm text-gray-600 dark:text-gray-300">
                    No checks have been run yet.
                </div>
            </x-filament::section>
        @else
            <div class="space-y-3">
                @foreach ($results as $result)
                    @php
                        $severity = is_array($result) ? ($result['severity'] ?? 'safe') : 'safe';
                        $title = is_array($result) ? ($result['title'] ?? $result['code'] ?? 'Check') : 'Check';
                        $message = is_array($result) ? ($result['message'] ?? null) : null;
                        $meta = is_array($result) ? ($result['meta'] ?? []) : [];
                        $meta = is_array($meta) ? $meta : [];

                        $unmappedGroups = $meta['unmapped'] ?? [];
                        $unmappedGroups = is_array($unmappedGroups) ? $limitedList($unmappedGroups) : [];
                    @endphp

                    <x-filament::section>
                        <div class="flex items-start justify-between gap-4">
                            <div class="space-y-1">
                                <div class="text-sm font-medium text-gray-900 dark:text-white">
                                    {{ $title }}
                                </div>
                                @if (is_string($message) && $message !== '')
                                    <div class="text-sm text-gray-600 dark:text-gray-300">
                                        {{ $message }}
                                    </div>
                                @endif
                            </div>

                            <x-filament::badge :color="$severityColor($severity)" size="sm">
                                {{ ucfirst((string) $severity) }}
                            </x-filament::badge>
                        </div>

                        @if ($unmappedGroups !== [])
                            <div class="mt-3">
                                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                                    Unmapped groups
                                </div>
                                <ul class="mt-2 space-y-1 text-sm text-gray-700 dark:text-gray-200">
                                    @foreach ($unmappedGroups as $group)
                                        @php
                                            $label = is_array($group) ? ($group['label'] ?? $group['id'] ?? null) : null;
                                        @endphp
                                        @if (is_string($label) && $label !== '')
                                            <li>{{ $label }}</li>
                                        @endif
                                    @endforeach
                                </ul>
                            </div>
                        @endif
                    </x-filament::section>
                @endforeach
            </div>
        @endif
    </div>
</x-dynamic-component>
