@php
    $entries = isset($entries) ? $entries : (isset($getState) ? $getState() : []);
    $entries = is_array($entries) ? $entries : [];
@endphp

<div class="space-y-3">
    @if ($entries === [])
        <div class="rounded-lg border border-dashed border-gray-300 px-4 py-6 text-sm text-gray-600 dark:border-gray-700 dark:text-gray-300">
            No related context is available for this record.
        </div>
    @else
        @foreach ($entries as $entry)
            @php
                $isAvailable = ($entry['availability'] ?? null) === 'available' && filled($entry['targetUrl'] ?? null);
                $reference = is_array($entry['reference'] ?? null) ? $entry['reference'] : null;
            @endphp

            <div class="rounded-xl border border-gray-200 bg-white/80 px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
                <div class="space-y-2">
                    <div class="text-xs font-medium uppercase tracking-[0.18em] text-gray-500 dark:text-gray-400">
                        {{ $entry['label'] ?? 'Related record' }}
                    </div>

                    @if ($reference !== null)
                        @include('filament.infolists.entries.resolved-reference-detail', ['reference' => $reference])
                    @else
                        <div class="flex items-start justify-between gap-4">
                            <div class="min-w-0 space-y-1">
                                @if ($isAvailable)
                                    <x-filament::link :href="$entry['targetUrl']">
                                        {{ $entry['value'] ?? 'Open related record' }}
                                    </x-filament::link>
                                @else
                                    <div class="text-sm font-semibold text-gray-900 dark:text-white">
                                        {{ $entry['value'] ?? 'Unavailable' }}
                                    </div>
                                @endif

                                @if (filled($entry['secondaryValue'] ?? null))
                                    <div class="text-xs text-gray-500 dark:text-gray-400">
                                        {{ $entry['secondaryValue'] }}
                                    </div>
                                @endif

                                @if (filled($entry['unavailableReason'] ?? null))
                                    <div class="text-xs text-gray-600 dark:text-gray-300">
                                        {{ $entry['unavailableReason'] }}
                                    </div>
                                @endif
                            </div>

                            <div class="flex shrink-0 items-center gap-2">
                                @if (filled($entry['contextBadge'] ?? null))
                                    <x-filament::badge color="gray" size="sm">
                                        {{ $entry['contextBadge'] }}
                                    </x-filament::badge>
                                @endif

                                @unless ($isAvailable)
                                    <x-filament::badge color="gray" size="sm">
                                        Unavailable
                                    </x-filament::badge>
                                @endunless

                                @if ($isAvailable && filled($entry['actionLabel'] ?? null))
                                    <x-filament::link
                                        :href="$entry['targetUrl']"
                                        icon="heroicon-m-arrow-top-right-on-square"
                                        size="sm"
                                    >
                                        {{ $entry['actionLabel'] }}
                                    </x-filament::link>
                                @endif
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        @endforeach
    @endif
</div>
