@php
    $state = $getState();
    $state = is_array($state) ? $state : [];

    $summary = is_array($state['summary'] ?? null) ? $state['summary'] : [];
    $artifactSources = is_array($state['artifact_sources'] ?? null) ? $state['artifact_sources'] : [];
    $highlights = is_array($state['highlights'] ?? null) ? $state['highlights'] : [];
    $entries = is_array($state['entries'] ?? null) ? $state['entries'] : [];
    $nextActions = is_array($state['next_actions'] ?? null) ? $state['next_actions'] : [];
    $links = is_array($state['links'] ?? null) ? $state['links'] : [];
    $disclosure = is_string($state['disclosure'] ?? null) ? $state['disclosure'] : null;
    $emptyState = is_string($state['empty_state'] ?? null) ? $state['empty_state'] : null;
    $isControlInterpretation = (bool) ($state['is_control_interpretation'] ?? false);
@endphp

<div class="space-y-3">
    @if ($artifactSources !== [])
        <div class="space-y-2">
            <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Artifact source</div>
            <dl class="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-3">
                @foreach ($artifactSources as $source)
                    @continue(! is_array($source))

                    @foreach ([
                        'Source family' => $source['source_family'] ?? null,
                        'Source kind' => $source['source_kind'] ?? null,
                        'Source target' => $source['source_target_kind'] ?? null,
                        'Control' => $source['control_key'] ?? null,
                        'Detector' => $source['detector_key'] ?? null,
                    ] as $label => $value)
                        @php
                            $value = is_string($value) && trim($value) !== '' ? \Illuminate\Support\Str::headline($value) : null;
                        @endphp

                        @continue($value === null)

                        <div class="rounded-md border border-gray-100 bg-gray-50 px-3 py-2 dark:border-gray-800 dark:bg-gray-950/60">
                            <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">{{ $label }}</dt>
                            <dd class="mt-1 text-sm text-gray-900 dark:text-gray-100">{{ $value }}</dd>
                        </div>
                    @endforeach
                @endforeach
            </dl>
        </div>
    @endif

    @if ($summary !== [])
        <dl class="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-3">
            @foreach ($summary as $item)
                @php
                    $label = is_string($item['label'] ?? null) ? $item['label'] : null;
                    $value = is_string($item['value'] ?? null) ? $item['value'] : null;
                @endphp

                @continue($label === null || $value === null)

                <div class="rounded-md border border-gray-100 bg-gray-50 px-3 py-2 dark:border-gray-800 dark:bg-gray-950/60">
                    <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">{{ $label }}</dt>
                    <dd class="mt-1 text-sm text-gray-900 dark:text-gray-100">{{ $value }}</dd>
                </div>
            @endforeach
        </dl>
    @endif

    @if ($highlights !== [])
        <ul class="space-y-1 text-sm text-gray-700 dark:text-gray-300">
            @foreach ($highlights as $highlight)
                @continue(! is_string($highlight) || trim($highlight) === '')

                <li class="rounded-md border border-gray-100 bg-gray-50 px-3 py-2 dark:border-gray-800 dark:bg-gray-950/60">{{ $highlight }}</li>
            @endforeach
        </ul>
    @endif

    @if ($entries !== [])
        <div class="space-y-2">
            <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">{{ __('localization.review.key_entries') }}</div>
            <div class="space-y-2">
                @foreach ($entries as $entry)
                    @continue(! is_array($entry))

                    <div class="rounded-md border border-gray-100 bg-gray-50 px-3 py-3 text-sm dark:border-gray-800 dark:bg-gray-950/60">
                        @if ($isControlInterpretation)
                            @php
                                $readinessBucket = is_string($entry['readiness_bucket'] ?? null) ? $entry['readiness_bucket'] : 'review_recommended';
                                $readinessColor = match ($readinessBucket) {
                                    'follow_up_required' => 'warning',
                                    'review_recommended' => 'info',
                                    'evidence_on_record' => 'success',
                                    default => 'gray',
                                };
                                $limitationLabels = is_array($entry['limitation_labels'] ?? null) ? $entry['limitation_labels'] : [];
                                $basisItems = is_array($entry['evidence_basis_items'] ?? null) ? $entry['evidence_basis_items'] : [];
                            @endphp

                            <div class="flex flex-wrap items-center justify-between gap-2">
                                <div class="font-medium text-gray-900 dark:text-gray-100">
                                    {{ $entry['control_name'] ?? $entry['title'] ?? __('localization.review.control') }}
                                </div>

                                <x-filament::badge :color="$readinessColor" size="sm">
                                    {{ $entry['readiness_label'] ?? __('localization.review.review_recommended') }}
                                </x-filament::badge>
                            </div>

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

                            @if ($basisItems !== [])
                                <div class="mt-3 space-y-1">
                                    <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">{{ __('localization.review.evidence_basis') }}</div>
                                    <ul class="space-y-1 text-gray-700 dark:text-gray-300">
                                        @foreach ($basisItems as $basisItem)
                                            @continue(! is_string($basisItem) || trim($basisItem) === '')

                                            <li>{{ $basisItem }}</li>
                                        @endforeach
                                    </ul>
                                </div>
                            @endif

                            @if (filled($entry['recommended_next_action'] ?? null))
                                <div class="mt-3 rounded-md border border-primary-100 bg-primary-50 px-3 py-2 text-primary-900 dark:border-primary-900/40 dark:bg-primary-950/30 dark:text-primary-100">
                                    {{ $entry['recommended_next_action'] }}
                                </div>
                            @endif

                            @if ($limitationLabels !== [])
                                <div class="mt-3 flex flex-wrap gap-1">
                                    @foreach ($limitationLabels as $label)
                                        @continue(! is_string($label) || trim($label) === '')

                                        <x-filament::badge color="gray" size="sm">
                                            {{ $label }}
                                        </x-filament::badge>
                                    @endforeach
                                </div>
                            @endif

                            @if (filled($entry['proof_access_state'] ?? null))
                                <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                                    {{ __('localization.review.proof_access_state') }}: {{ \Illuminate\Support\Str::headline((string) $entry['proof_access_state']) }}
                                </div>
                            @endif
                        @else
                            <div class="font-medium text-gray-900 dark:text-gray-100">
                                {{ $entry['title'] ?? $entry['displayName'] ?? $entry['type'] ?? __('localization.review.entry') }}
                            </div>

                            @php
                                $detailParts = collect([
                                    $entry['severity'] ?? null,
                                    $entry['status'] ?? null,
                                    $entry['governance_state'] ?? null,
                                    $entry['outcome'] ?? null,
                                ])->filter(fn ($value) => is_string($value) && trim($value) !== '')->map(fn (string $value) => \Illuminate\Support\Str::headline($value))->all();
                            @endphp

                            @if ($detailParts !== [])
                                <div class="mt-1 text-xs text-gray-500 dark:text-gray-400">{{ implode(' · ', $detailParts) }}</div>
                            @endif
                        @endif
                    </div>
                @endforeach
            </div>
        </div>
    @elseif ($emptyState)
        <div class="rounded-md border border-gray-100 bg-gray-50 px-3 py-2 text-sm text-gray-700 dark:border-gray-800 dark:bg-gray-950/60 dark:text-gray-300">
            {{ $emptyState }}
        </div>
    @endif

    @if ($disclosure)
        <div class="rounded-md border border-amber-100 bg-amber-50 px-3 py-2 text-sm text-amber-800 dark:border-amber-900/40 dark:bg-amber-950/30 dark:text-amber-200">
            {{ $disclosure }}
        </div>
    @endif

    @if ($nextActions !== [])
        <div class="space-y-2">
            <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">{{ __('localization.review.follow_up') }}</div>
            <ul class="space-y-1 text-sm text-gray-700 dark:text-gray-300">
                @foreach ($nextActions as $action)
                    @continue(! is_string($action) || trim($action) === '')

                    <li class="rounded-md border border-primary-100 bg-primary-50 px-3 py-2 dark:border-primary-900/40 dark:bg-primary-950/30">{{ $action }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    @if ($links !== [])
        <div class="flex flex-wrap gap-2">
            @foreach ($links as $link)
                @continue(! is_array($link) || ! is_string($link['label'] ?? null) || ! is_string($link['url'] ?? null))

                <a
                    class="inline-flex items-center rounded-lg border border-gray-300 px-3 py-2 text-sm font-medium text-gray-700 transition hover:bg-gray-50 dark:border-gray-700 dark:text-gray-200 dark:hover:bg-gray-900"
                    href="{{ $link['url'] }}"
                >
                    {{ $link['label'] }}
                </a>
            @endforeach
        </div>
    @endif
</div>
