@php
    $state = $getState() ?? [];
    $state = is_array($state) ? $state : [];
    $resultAttention = is_array($state['resultAttention'] ?? null) ? $state['resultAttention'] : [];
    $executionSafetySnapshot = is_array($state['executionSafetySnapshot'] ?? null) ? $state['executionSafetySnapshot'] : [];
    $state = is_array($state['results'] ?? null) ? $state['results'] : $state;
    $isFoundationEntry = function ($item) {
        return is_array($item) && array_key_exists('decision', $item) && array_key_exists('sourceId', $item);
    };

    if (is_array($state) && array_key_exists('items', $state)) {
        $foundationItems = collect($state['foundations'] ?? [])->filter($isFoundationEntry);
        $policyItems = collect($state['items'] ?? [])->values();
        $results = $state;
    } else {
        $results = $state;
        $foundationItems = collect($results)->filter($isFoundationEntry);
        $policyItems = collect($results)->reject($isFoundationEntry);
    }

    $tenant = rescue(fn () => \App\Models\ManagedEnvironment::current(), null);
    $groupLabelResolver = $tenant ? app(\App\Services\Directory\EntraGroupLabelResolver::class) : null;

    $formatGroupId = function ($groupId, $fallbackName = null) use ($tenant, $groupLabelResolver) {
        if (! is_string($groupId) || $groupId === '') {
            return null;
        }

        $cachedName = null;

        if ($tenant && $groupLabelResolver) {
            $cached = $groupLabelResolver->lookupMany($tenant, [$groupId]);
            $cachedName = $cached[strtolower($groupId)] ?? null;
        }

        $name = is_string($fallbackName) && $fallbackName !== '' ? $fallbackName : null;

        return \App\Services\Directory\EntraGroupLabelResolver::formatLabel($cachedName ?? $name, $groupId);
    };
@endphp

@if ($foundationItems->isEmpty() && $policyItems->isEmpty())
    <p class="text-sm text-gray-600">No restore results have been recorded yet.</p>
@else
    @php
        $needsAttention = (bool) ($resultAttention['follow_up_required'] ?? false)
            || $policyItems->contains(function ($item) {
                $status = $item['status'] ?? null;

                return in_array($status, ['partial', 'manual_required'], true);
            });
        $attentionSpec = \App\Support\Badges\BadgeRenderer::spec(
            \App\Support\Badges\BadgeDomain::RestoreResultStatus,
            $resultAttention['state'] ?? ($needsAttention ? 'completed_with_follow_up' : 'completed')
        );
        $executionBasisLabel = \App\Support\RestoreSafety\RestoreSafetyCopy::safetyStateLabel(
            is_string($executionSafetySnapshot['safety_state'] ?? null) ? $executionSafetySnapshot['safety_state'] : null
        );
        $primaryNextAction = \App\Support\RestoreSafety\RestoreSafetyCopy::primaryNextAction(
            is_string($resultAttention['primary_next_action'] ?? null) ? $resultAttention['primary_next_action'] : 'review_result'
        );
        $primaryCauseFamily = \App\Support\RestoreSafety\RestoreSafetyCopy::primaryCauseFamily(
            is_string($resultAttention['primary_cause_family'] ?? null) ? $resultAttention['primary_cause_family'] : 'none'
        );
        $recoveryBoundary = \App\Support\RestoreSafety\RestoreSafetyCopy::recoveryBoundary(
            is_string($resultAttention['recovery_claim_boundary'] ?? null)
                ? $resultAttention['recovery_claim_boundary']
                : 'run_completed_not_recovery_proven'
        );
    @endphp

    <div class="space-y-4">
        <div class="rounded-lg border border-slate-200 bg-slate-50 p-4 text-sm text-slate-900 dark:border-white/10 dark:bg-white/5 dark:text-slate-100">
            <div class="flex flex-wrap items-center gap-2">
                <x-filament::badge :color="$attentionSpec->color" :icon="$attentionSpec->icon" size="sm">
                    {{ $attentionSpec->label }}
                </x-filament::badge>
                @if (($executionSafetySnapshot['safety_state'] ?? null) !== null)
                    <x-filament::badge color="gray" size="sm">
                        Execution basis: {{ $executionBasisLabel }}
                    </x-filament::badge>
                @endif
            </div>

            <div class="mt-3 grid gap-3 md:grid-cols-2">
                <div>
                    <div class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">What this run proves</div>
                    <div class="mt-1">{{ $resultAttention['summary'] ?? 'Restore result truth is unavailable.' }}</div>
                </div>
                <div>
                    <div class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">Primary next step</div>
                    <div class="mt-1">{{ $primaryNextAction }}</div>
                </div>
            </div>

            <div class="mt-3 grid gap-3 md:grid-cols-2">
                <div>
                    <div class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">Main follow-up driver</div>
                    <div class="mt-1 text-xs text-slate-600 dark:text-slate-300">{{ $primaryCauseFamily }}</div>
                </div>
                <div>
                    <div class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">What this record does not prove</div>
                    <div class="mt-1 text-xs text-slate-600 dark:text-slate-300">{{ $recoveryBoundary }}</div>
                </div>
            </div>
        </div>

        @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';
                        $foundationIsPreviewOnly = ($item['reason'] ?? null) === 'preview_only'
                            || ($item['restore_mode'] ?? null) === 'preview-only'
                            || $decision === 'dry_run';
                        $decisionSpec = $foundationIsPreviewOnly
                            ? \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::PolicyRestoreMode, 'preview_only')
                            : \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestorePreviewDecision, $decision);
                        $foundationReason = $item['reason'] ?? null;

                        if ($foundationReason === 'preview_only') {
                            $foundationReason = 'Preview only. This foundation type is not applied during execution.';
                        }
                    @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($foundationReason))
                            <div class="mt-2 rounded border border-amber-200 bg-amber-50 px-2 py-1 text-xs text-amber-900">
                                {{ $foundationReason }}
                            </div>
                        @endif
                    </div>
                @endforeach
            </div>
        @endif

        @if ($needsAttention)
            <div class="rounded border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-900">
                {{ $resultAttention['summary'] ?? 'Some items still need follow-up. Review the per-item details below.' }}
            </div>
        @endif

        @if ($policyItems->isNotEmpty())
            <div class="space-y-3">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500">Policies</div>
                @foreach ($policyItems as $item)
                    <div class="rounded border border-gray-200 bg-white p-3 shadow-sm">
                        <div class="flex items-center justify-between text-sm">
                            <div class="font-semibold text-gray-900">
                                {{ $item['policy_identifier'] ?? $item['policy_id'] ?? 'Policy' }}
                                <span class="ml-2 text-xs text-gray-500">{{ $item['policy_type'] ?? '' }}</span>
                            </div>
                            @php
                                $status = $item['status'] ?? 'unknown';
                                $restoreMode = $item['restore_mode'] ?? null;
                                $statusSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestoreResultStatus, $status);
                            @endphp
	                            <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
                                <x-filament::badge :color="$statusSpec->color" :icon="$statusSpec->icon" size="sm">
                                    {{ $statusSpec->label }}
                                </x-filament::badge>
                            </div>
                        </div>

                        @php
                            $itemReason = $item['reason'] ?? null;
                            $itemGraphMessage = $item['graph_error_message'] ?? null;

                            if ($itemReason === 'preview_only') {
                                $itemReason = 'Preview only. This policy type is not applied during execution.';
                            }
                        @endphp

                        @if (! empty($itemReason) && ($itemGraphMessage === null || $itemGraphMessage !== $itemReason))
                            <div class="mt-2 text-sm text-gray-800">
                                {{ $itemReason }}
                            </div>
                        @endif

                        @if (! empty($item['assignment_summary']) && is_array($item['assignment_summary']))
                            @php
                                $summary = $item['assignment_summary'];
                                $assignmentOutcomes = $item['assignment_outcomes'] ?? [];
                                $assignmentIssues = collect($assignmentOutcomes)
                                    ->filter(fn ($outcome) => in_array($outcome['status'] ?? null, ['failed', 'skipped'], true))
                                    ->values();
                            @endphp

                            <div class="mt-2 text-xs text-gray-700">
                                Assignments: {{ (int) ($summary['success'] ?? 0) }} applied •
                                {{ (int) ($summary['failed'] ?? 0) }} failed items •
                                {{ (int) ($summary['skipped'] ?? 0) }} not applied
                            </div>

                            @if ($assignmentIssues->isNotEmpty())
                                <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">Assignment details</summary>
                                    <div class="mt-2 space-y-2">
                                        @foreach ($assignmentIssues as $outcome)
                                            @php
                                                $outcomeStatus = $outcome['status'] ?? 'unknown';
                                                $outcomeSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestoreResultStatus, $outcomeStatus);
                                                $assignmentGroupId = $outcome['group_id']
                                                    ?? ($outcome['assignment']['target']['groupId'] ?? null);
                                                $assignmentGroupLabel = $formatGroupId(is_string($assignmentGroupId) ? $assignmentGroupId : null);
                                                $mappedGroupId = $outcome['mapped_group_id'] ?? null;
                                                $mappedGroupLabel = $formatGroupId(is_string($mappedGroupId) ? $mappedGroupId : null);
                                            @endphp

                                            <div class="rounded border border-amber-200 bg-white p-2">
                                                <div class="flex items-center justify-between">
                                                    <div class="font-semibold text-gray-900">
                                                        Assignment {{ $assignmentGroupLabel ?? ($assignmentGroupId ?? 'unknown group') }}
                                                    </div>
                                                    <x-filament::badge :color="$outcomeSpec->color" :icon="$outcomeSpec->icon" size="sm">
                                                        {{ $outcomeSpec->label }}
                                                    </x-filament::badge>
                                                </div>

                                                @if (! empty($outcome['mapped_group_id']))
                                                    <div class="mt-1 text-[11px] text-gray-800">
                                                        Mapped to: {{ $mappedGroupLabel ?? $outcome['mapped_group_id'] }}
                                                    </div>
                                                @endif

                                                @php
                                                    $outcomeReason = $outcome['reason'] ?? null;
                                                    $outcomeGraphMessage = $outcome['graph_error_message'] ?? null;
                                                @endphp

                                                @if (! empty($outcomeReason) && ($outcomeGraphMessage === null || $outcomeGraphMessage !== $outcomeReason))
                                                    <div class="mt-1 text-[11px] text-gray-800">
                                                        {{ $outcomeReason }}
                                                    </div>
                                                @endif

                                                @if (! empty($outcome['graph_error_message']) || ! empty($outcome['graph_error_code']))
                                                    <div class="mt-1 text-[11px] text-amber-900">
                                                        <div>{{ $outcome['graph_error_message'] ?? 'Unknown error' }}</div>
                                                        @if (! empty($outcome['graph_error_code']))
                                                            <div class="mt-0.5 text-amber-800">Code: {{ $outcome['graph_error_code'] }}</div>
                                                        @endif
                                                    </div>
                                                @endif
                                            </div>
                                        @endforeach
                                    </div>
                                </details>
                            @endif
                        @endif

                        @if (! empty($item['compliance_action_summary']) && is_array($item['compliance_action_summary']))
                            @php
                                $summary = $item['compliance_action_summary'];
                                $complianceOutcomes = is_array($item['compliance_action_outcomes'] ?? null)
                                    ? $item['compliance_action_outcomes']
                                    : [];
                                $complianceEntries = collect($complianceOutcomes)->values();
                            @endphp

                            <div class="mt-2 text-xs text-gray-700">
                                Compliance notifications: {{ (int) ($summary['mapped'] ?? 0) }} mapped •
                                {{ (int) ($summary['skipped'] ?? 0) }} not applied
                            </div>

                            @if ($complianceEntries->isNotEmpty())
                                <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">Compliance notification details</summary>
                                    <div class="mt-2 space-y-2">
                                        @foreach ($complianceEntries as $outcome)
                                            @php
                                                $outcomeStatus = $outcome['status'] ?? 'unknown';
                                                $outcomeSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestoreResultStatus, $outcomeStatus);
                                            @endphp
                                            <div class="rounded border border-amber-200 bg-white p-2">
                                                <div class="flex items-center justify-between">
                                                    <div class="font-semibold text-gray-900">
                                                        Template {{ $outcome['template_id'] ?? 'unknown' }}
                                                    </div>
                                                    <x-filament::badge :color="$outcomeSpec->color" :icon="$outcomeSpec->icon" size="sm">
                                                        {{ $outcomeSpec->label }}
                                                    </x-filament::badge>
                                                </div>
                                                @if (! empty($outcome['rule_name']))
                                                    <div class="mt-1 text-[11px] text-gray-700">
                                                        Rule: {{ $outcome['rule_name'] }}
                                                    </div>
                                                @endif
                                                @if (! empty($outcome['mapped_template_id']))
                                                    <div class="mt-1 text-[11px] text-gray-700">
                                                        Mapped to: {{ $outcome['mapped_template_id'] }}
                                                    </div>
                                                @endif
                                                @if (! empty($outcome['reason']))
                                                    <div class="mt-1 text-[11px] text-gray-800">
                                                        {{ $outcome['reason'] }}
                                                    </div>
                                                @endif
                                            </div>
                                        @endforeach
                                    </div>
                                </details>
                            @endif
                        @endif

                        @if (! empty($item['created_policy_id']))
                            @php
                                $createdMode = $item['created_policy_mode'] ?? null;
                                $createdMessage = match ($createdMode) {
                                    'metadata_only' => 'New policy created (metadata only). Apply settings manually.',
                                    'created' => 'New policy created.',
                                    default => 'New policy created (manual cleanup required).',
                                };
                            @endphp
                            <div class="mt-2 text-xs text-amber-800">
                                {{ $createdMessage }} ID: {{ $item['created_policy_id'] }}
                            </div>
                        @endif

                        @if (! empty($item['graph_error_message']) || ! empty($item['graph_error_code']))
                            <div class="mt-2 rounded border border-amber-200 bg-amber-50 px-2 py-1 text-xs text-amber-900">
                                <div class="font-semibold">Graph error</div>
                                <div>{{ $item['graph_error_message'] ?? 'Unknown error' }}</div>
                                @if (! empty($item['graph_error_code']))
                                    <div class="mt-1 text-[11px] text-amber-800">Code: {{ $item['graph_error_code'] }}</div>
                                @endif
                                @if (! empty($item['graph_request_id']) || ! empty($item['graph_client_request_id']) || ! empty($item['graph_method']) || ! empty($item['graph_path']))
                                    <details class="mt-1">
                                        <summary class="cursor-pointer text-[11px] font-semibold text-amber-800">Details</summary>
                                        <div class="mt-1 space-y-0.5 text-[11px] text-amber-800">
                                            @if (! empty($item['graph_method']))
                                                <div>method: {{ $item['graph_method'] }}</div>
                                            @endif
                                            @if (! empty($item['graph_path']))
                                                <div>path: {{ $item['graph_path'] }}</div>
                                            @endif
                                            @if (! empty($item['graph_request_id']))
                                                <div>request-id: {{ $item['graph_request_id'] }}</div>
                                            @endif
                                            @if (! empty($item['graph_client_request_id']))
                                                <div>client-request-id: {{ $item['graph_client_request_id'] }}</div>
                                            @endif
                                        </div>
                                    </details>
                                @endif
                            </div>
                        @endif

                        @if (! empty($item['settings_apply']) && is_array($item['settings_apply']))
                            @php
                                $apply = $item['settings_apply'];
                                $total = (int) ($apply['total'] ?? 0);
                                $applied = (int) ($apply['applied'] ?? 0);
                                $failed = (int) ($apply['failed'] ?? 0);
                                $manual = (int) ($apply['manual_required'] ?? 0);
                                $issues = $apply['issues'] ?? [];
                            @endphp

                            <div class="mt-2 text-xs text-gray-700">
                                Settings applied: {{ $applied }}/{{ $total }}
                                @if ($failed > 0 || $manual > 0)
                                    • {{ $failed }} failed • {{ $manual }} manual
                                @endif
                            </div>

                            @if (! empty($issues))
                                <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">Settings requiring attention</summary>
                                    <div class="mt-2 space-y-2">
                                        @foreach ($issues as $issue)
                                            @php
                                                $issueStatus = $issue['status'] ?? 'unknown';
                                                $issueColor = match ($issueStatus) {
                                                    'failed' => 'text-red-700 bg-red-100 border-red-200',
                                                    'manual_required' => 'text-amber-900 bg-amber-100 border-amber-200',
                                                    default => 'text-gray-700 bg-gray-100 border-gray-200',
                                                };
                                            @endphp
                                            <div class="rounded border border-amber-200 bg-white p-2">
                                                <div class="flex items-center justify-between">
                                                    <div class="font-semibold text-gray-900">
                                                        Setting {{ $issue['setting_id'] ?? 'unknown' }}
                                                    </div>
                                                    <span class="rounded border px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wide {{ $issueColor }}">
                                                        {{ $issueStatus }}
                                                    </span>
                                                </div>

                                                @if (! empty($issue['reason']))
                                                    <div class="mt-1 text-[11px] text-gray-800">
                                                        {{ $issue['reason'] }}
                                                    </div>
                                                @endif

                                                @if (! empty($issue['graph_error_message']) || ! empty($issue['graph_error_code']))
                                                    <div class="mt-1 text-[11px] text-amber-900">
                                                        <div>{{ $issue['graph_error_message'] ?? 'Unknown error' }}</div>
                                                        @if (! empty($issue['graph_error_code']))
                                                            <div class="mt-0.5 text-amber-800">Code: {{ $issue['graph_error_code'] }}</div>
                                                        @endif
                                                        @if (! empty($issue['graph_request_id']) || ! empty($issue['graph_client_request_id']))
                                                            <div class="mt-0.5 space-y-0.5 text-amber-800">
                                                                @if (! empty($issue['graph_request_id']))
                                                                    <div>request-id: {{ $issue['graph_request_id'] }}</div>
                                                                @endif
                                                                @if (! empty($issue['graph_client_request_id']))
                                                                    <div>client-request-id: {{ $issue['graph_client_request_id'] }}</div>
                                                                @endif
                                                            </div>
                                                        @endif
                                                    </div>
                                                @endif
                                            </div>
                                        @endforeach
                                    </div>
                                </details>
                            @endif
                        @endif

                        @if (! empty($item['platform']))
                            <div class="mt-2 text-[11px] text-gray-500">
                                Platform: {{ $item['platform'] }}
                            </div>
                        @endif
                    </div>
                @endforeach
            </div>
        @endif
    </div>
@endif
