@php
    $detail = isset($getState) ? $getState() : ($detail ?? null);
    $detail = is_array($detail) ? $detail : [];

    $decisionZone = is_array($detail['decisionZone'] ?? null) ? $detail['decisionZone'] : [];
    $mainSections = array_values(array_filter($detail['mainSections'] ?? [], 'is_array'));
    $supportingGroups = array_values(array_filter($detail['supportingGroups'] ?? [], 'is_array'));
    $technicalSections = array_values(array_filter($detail['technicalSections'] ?? [], 'is_array'));
    $emptyStateNotes = array_values(array_filter($detail['emptyStateNotes'] ?? [], 'is_array'));
@endphp

<div class="space-y-6">
    @include('filament.infolists.entries.enterprise-detail.header', [
        'header' => is_array($detail['header'] ?? null) ? $detail['header'] : [],
    ])

    @if ($decisionZone !== [])
        @include('filament.infolists.entries.enterprise-detail.decision-zone', [
            'decisionZone' => $decisionZone,
        ])
    @endif

    @if ($emptyStateNotes !== [])
        <div class="space-y-3">
            @foreach ($emptyStateNotes as $state)
                @include('filament.infolists.entries.enterprise-detail.empty-state', ['state' => $state])
            @endforeach
        </div>
    @endif

    @if ($supportingGroups !== [])
        <div class="grid gap-4 xl:grid-cols-2">
            @foreach ($supportingGroups as $card)
                @include('filament.infolists.entries.enterprise-detail.supporting-card', ['card' => $card])
            @endforeach
        </div>
    @endif

    <div class="space-y-6">
        @foreach ($mainSections as $section)
            @php
                $view = is_string($section['view'] ?? null) && trim($section['view']) !== '' ? trim($section['view']) : null;
                $items = is_array($section['items'] ?? null) ? $section['items'] : [];
                $emptyState = is_array($section['emptyState'] ?? null) ? $section['emptyState'] : null;
            @endphp

            <x-filament::section
                :heading="$section['title'] ?? 'Details'"
                :description="$section['description'] ?? null"
                :collapsible="(bool) ($section['collapsible'] ?? false)"
                :collapsed="(bool) ($section['collapsed'] ?? false)"
            >
                @if ($view !== null)
                    {!! view($view, is_array($section['viewData'] ?? null) ? $section['viewData'] : [])->render() !!}
                @elseif ($items !== [])
                    @include('filament.infolists.entries.enterprise-detail.section-items', [
                        'items' => $items,
                        'action' => is_array($section['action'] ?? null) ? $section['action'] : null,
                    ])
                @elseif ($emptyState !== null)
                    @include('filament.infolists.entries.enterprise-detail.empty-state', ['state' => $emptyState])
                @endif
            </x-filament::section>
        @endforeach
    </div>

    @if ($technicalSections !== [])
        <div class="space-y-4">
            @foreach ($technicalSections as $section)
                @include('filament.infolists.entries.enterprise-detail.technical-detail', ['section' => $section])
            @endforeach
        </div>
    @endif
</div>
