@php
    $preview = $getState() ?? [];
    $foundationItems = collect($preview)->filter(function ($item) {
        return is_array($item) && array_key_exists('decision', $item) && array_key_exists('sourceId', $item);
    });
    $policyItems = collect($preview)->reject(function ($item) {
        return is_array($item) && array_key_exists('decision', $item) && array_key_exists('sourceId', $item);
    });
@endphp

@if (empty($preview))
    <p class="text-sm text-gray-600">No preview available.</p>
@else
    <div class="space-y-4">
        @if ($foundationItems->isNotEmpty())
            <div class="space-y-2">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500">Foundations</div>
                @foreach ($foundationItems as $item)
                    @php
                        $decision = $item['decision'] ?? 'mapped_existing';
                        $decisionSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestorePreviewDecision, $decision);
                    @endphp
                    <div class="rounded border border-gray-200 bg-white p-3 shadow-sm">
                        <div class="flex items-center justify-between text-sm text-gray-800">
                            <span class="font-semibold">{{ $item['sourceName'] ?? $item['sourceId'] ?? 'Foundation' }}</span>
                            <x-filament::badge :color="$decisionSpec->color" :icon="$decisionSpec->icon" size="sm">
                                {{ $decisionSpec->label }}
                            </x-filament::badge>
                        </div>
                        <div class="mt-1 text-xs text-gray-600">
                            {{ $item['type'] ?? 'foundation' }}
                        </div>
                        @if (! empty($item['targetName']))
                            <div class="mt-1 text-xs text-gray-600">
                                Target: {{ $item['targetName'] }}
                            </div>
                        @endif
                        @if (! empty($item['reason']))
                            <div class="mt-2 rounded border border-amber-300 bg-amber-50 px-2 py-1 text-xs text-amber-800">
                                {{ $item['reason'] }}
                            </div>
                        @endif
                    </div>
                @endforeach
            </div>
        @endif

        @if ($policyItems->isNotEmpty())
            <div class="space-y-2">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500">Policies</div>
                @foreach ($policyItems as $item)
                    @php
                        $restoreMode = $item['restore_mode'] ?? null;
                    @endphp
                    <div class="rounded border border-gray-200 bg-white p-3 shadow-sm">
                        <div class="flex items-center justify-between text-sm text-gray-800">
	                            <span class="font-semibold">{{ $item['policy_identifier'] ?? 'Policy' }}</span>
	                            <div class="flex items-center gap-2">
	                                @if ($restoreMode === 'preview-only')
	                                    @php
	                                        $restoreModeSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::PolicyRestoreMode, $restoreMode);
	                                    @endphp
	                                    <x-filament::badge :color="$restoreModeSpec->color" :icon="$restoreModeSpec->icon" size="sm">
	                                        {{ $restoreModeSpec->label }}
	                                    </x-filament::badge>
	                                @endif
                                <span class="rounded bg-gray-100 px-2 py-0.5 text-xs uppercase tracking-wide text-gray-700">
                                    {{ $item['action'] ?? 'action' }}
                                </span>
                            </div>
                        </div>
                        <div class="mt-1 text-xs text-gray-600">
                            {{ $item['policy_type'] ?? 'type' }} • {{ $item['platform'] ?? 'platform' }}
                        </div>

                        @if (! empty($item['validation_warning']))
                            <div class="mt-2 rounded border border-amber-300 bg-amber-50 px-2 py-1 text-xs text-amber-800">
                                {{ $item['validation_warning'] }}
                            </div>
                        @endif

                        @if (! empty($item['compliance_action_warning']))
                            <div class="mt-2 rounded border border-amber-300 bg-amber-50 px-2 py-1 text-xs text-amber-800">
                                {{ $item['compliance_action_warning'] }}
                            </div>
                        @endif
                        @if (! empty($item['compliance_action_summary']) && is_array($item['compliance_action_summary']))
                            @php
                                $summary = $item['compliance_action_summary'];
                                $missingTemplates = $item['compliance_action_missing_templates'] ?? [];
                                $total = (int) ($summary['total'] ?? 0);
                                $missing = (int) ($summary['missing'] ?? 0);
                            @endphp

                            <div class="mt-2 text-xs text-gray-600">
                                Compliance notifications: {{ $total }} total • {{ $missing }} missing
                            </div>

                            @if (! empty($missingTemplates) && is_array($missingTemplates))
                                <details class="mt-2 rounded border border-amber-200 bg-amber-50 px-2 py-1 text-xs text-amber-900">
                                    <summary class="cursor-pointer font-semibold">Missing notification templates</summary>
                                    <div class="mt-2 space-y-1">
                                        @foreach ($missingTemplates as $templateId)
                                            <div class="rounded border border-amber-200 bg-white px-2 py-1 text-[11px] text-gray-800">
                                                {{ $templateId }}
                                            </div>
                                        @endforeach
                                    </div>
                                </details>
                            @endif
                        @endif
                    </div>
                @endforeach
            </div>
        @endif
    </div>
@endif
