@php
    use App\Support\Ui\EnterpriseDetail\FactPresentation;

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

    $statusBadges = array_values(array_filter($header['statusBadges'] ?? [], 'is_array'));
    $keyFacts = array_values(array_filter($header['keyFacts'] ?? [], 'is_array'));
    $primaryActions = array_values(array_filter($header['primaryActions'] ?? [], 'is_array'));
@endphp

<x-filament::section
    :heading="$header['title'] ?? 'Detail'"
    :description="$header['subtitle'] ?? null"
>
    @if ($primaryActions !== [])
        <x-slot name="afterHeader">
            <div class="flex flex-wrap items-center gap-2">
                @foreach ($primaryActions as $action)
                    @if (filled($action['url'] ?? null))
                        @if (($action['openInNewTab'] ?? false) === true)
                            <x-filament::button
                                tag="a"
                                size="sm"
                                :color="($action['destructive'] ?? false) === true ? 'danger' : 'gray'"
                                :href="$action['url']"
                                :icon="$action['icon'] ?? null"
                                target="_blank"
                                rel="noreferrer noopener"
                            >
                                {{ $action['label'] }}
                            </x-filament::button>
                        @else
                            <x-filament::button
                                tag="a"
                                size="sm"
                                :color="($action['destructive'] ?? false) === true ? 'danger' : 'gray'"
                                :href="$action['url']"
                                :icon="$action['icon'] ?? null"
                            >
                                {{ $action['label'] }}
                            </x-filament::button>
                        @endif
                    @endif
                @endforeach
            </div>
        </x-slot>
    @endif

    <div class="space-y-4">
        @if ($statusBadges !== [])
            <div class="flex flex-wrap items-center gap-2">
                @foreach ($statusBadges as $badge)
                    <x-filament::badge
                        :color="$badge['color'] ?? 'gray'"
                        :icon="$badge['icon'] ?? null"
                        :icon-color="$badge['iconColor'] ?? null"
                    >
                        {{ $badge['label'] ?? 'State' }}
                    </x-filament::badge>
                @endforeach
            </div>
        @endif

        @if (filled($header['descriptionHint'] ?? null))
            <div class="max-w-3xl text-sm text-gray-600 dark:text-gray-300">
                {{ $header['descriptionHint'] }}
            </div>
        @endif

        @if ($keyFacts !== [])
            @include('filament.infolists.entries.enterprise-detail.section-items', [
                'items' => $keyFacts,
                'variant' => 'header',
            ])
        @endif
    </div>
</x-filament::section>
