@php
    $issueGroups = collect($surface['issueGroups'] ?? [])
        ->filter(static fn (mixed $group): bool => is_array($group))
        ->values();
    $canAcknowledge = (bool) ($canAcknowledge ?? false);
    $ackAction = $ackAction ?? null;
    $showAssist = (bool) ($showAssist ?? false);
    $assistActionName = is_string($assistActionName ?? null) && trim((string) $assistActionName) !== ''
        ? trim((string) $assistActionName)
        : 'wizardVerificationRequiredPermissionsAssist';
    $linkBehavior = $linkBehavior ?? app(\App\Support\Verification\VerificationLinkBehavior::class);
@endphp

<div data-shared-zone="issues">
    @if ($issueGroups->isEmpty())
        <div class="text-sm text-gray-700 dark:text-gray-200">
            No issues found in this report.
        </div>
    @else
        <div class="space-y-3">
            @foreach ($issueGroups as $group)
                @php
                    $label = is_string($group['label'] ?? null) ? (string) $group['label'] : 'Issues';
                    $checks = collect($group['checks'] ?? [])->filter(static fn (mixed $check): bool => is_array($check))->values();
                    $acknowledged = (bool) ($group['acknowledged'] ?? false);
                @endphp

                @if ($acknowledged)
                    <details class="rounded-lg border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                        <summary class="cursor-pointer text-sm font-semibold text-gray-900 dark:text-white">
                            {{ $label }}
                        </summary>

                        <div class="mt-4 space-y-2">
                            @foreach ($checks as $check)
                                @php
                                    $title = is_string($check['title'] ?? null) && trim((string) $check['title']) !== '' ? trim((string) $check['title']) : 'Check';
                                    $message = is_string($check['message'] ?? null) && trim((string) $check['message']) !== '' ? trim((string) $check['message']) : null;
                                    $statusSpec = \App\Support\Badges\BadgeRenderer::spec(
                                        \App\Support\Badges\BadgeDomain::VerificationCheckStatus,
                                        $check['status'] ?? null,
                                    );
                                    $severitySpec = \App\Support\Badges\BadgeRenderer::spec(
                                        \App\Support\Badges\BadgeDomain::VerificationCheckSeverity,
                                        $check['severity'] ?? null,
                                    );
                                    $acknowledgement = is_array($check['acknowledgement'] ?? null) ? $check['acknowledgement'] : null;
                                @endphp

                                <div class="rounded-lg border border-gray-100 bg-gray-50 p-4 dark:border-gray-800 dark:bg-gray-950">
                                    <div class="flex flex-wrap items-start justify-between gap-3">
                                        <div class="space-y-1">
                                            <div class="text-sm font-medium text-gray-900 dark:text-white">
                                                {{ $title }}
                                            </div>
                                            @if ($message)
                                                <div class="text-sm text-gray-600 dark:text-gray-300">
                                                    {{ $message }}
                                                </div>
                                            @endif
                                        </div>

                                        <div class="flex shrink-0 flex-wrap items-center justify-end gap-2">
                                            <x-filament::badge :color="$severitySpec->color" :icon="$severitySpec->icon" size="sm">
                                                {{ $severitySpec->label }}
                                            </x-filament::badge>
                                            <x-filament::badge :color="$statusSpec->color" :icon="$statusSpec->icon" size="sm">
                                                {{ $statusSpec->label }}
                                            </x-filament::badge>
                                        </div>
                                    </div>

                                    @if ($acknowledgement)
                                        <div class="mt-3 space-y-1 text-sm text-gray-700 dark:text-gray-200">
                                            @if (filled($acknowledgement['ack_reason'] ?? null))
                                                <div>
                                                    <span class="font-semibold">Reason:</span> {{ $acknowledgement['ack_reason'] }}
                                                </div>
                                            @endif
                                            @if (filled($acknowledgement['acknowledged_by']['name'] ?? null) || filled($acknowledgement['acknowledged_at'] ?? null))
                                                <div>
                                                    <span class="font-semibold">Acknowledged:</span>
                                                    @if (filled($acknowledgement['acknowledged_by']['name'] ?? null))
                                                        {{ $acknowledgement['acknowledged_by']['name'] }}
                                                    @endif
                                                    @if (filled($acknowledgement['acknowledged_at'] ?? null))
                                                        <span class="text-gray-500 dark:text-gray-400">({{ $acknowledgement['acknowledged_at'] }})</span>
                                                    @endif
                                                </div>
                                            @endif
                                            @if (filled($acknowledgement['expires_at'] ?? null))
                                                <div>
                                                    <span class="font-semibold">Expires:</span>
                                                    <span class="text-gray-500 dark:text-gray-400">{{ $acknowledgement['expires_at'] }}</span>
                                                </div>
                                            @endif
                                        </div>
                                    @endif
                                </div>
                            @endforeach
                        </div>
                    </details>
                @else
                    <div class="space-y-2">
                        <div class="text-sm font-semibold text-gray-900 dark:text-white">
                            {{ $label }}
                        </div>

                        <div class="space-y-2">
                            @foreach ($checks as $check)
                                @php
                                    $checkKey = is_string($check['key'] ?? null) ? trim((string) $check['key']) : '';
                                    $title = is_string($check['title'] ?? null) && trim((string) $check['title']) !== '' ? trim((string) $check['title']) : 'Check';
                                    $message = is_string($check['message'] ?? null) && trim((string) $check['message']) !== '' ? trim((string) $check['message']) : null;
                                    $statusSpec = \App\Support\Badges\BadgeRenderer::spec(
                                        \App\Support\Badges\BadgeDomain::VerificationCheckStatus,
                                        $check['status'] ?? null,
                                    );
                                    $severitySpec = \App\Support\Badges\BadgeRenderer::spec(
                                        \App\Support\Badges\BadgeDomain::VerificationCheckSeverity,
                                        $check['severity'] ?? null,
                                    );
                                    $nextSteps = collect($check['next_steps'] ?? [])->filter(static fn (mixed $step): bool => is_array($step))->take(2)->values();
                                    $blocking = (bool) ($check['blocking'] ?? false);
                                    $routeNextStepsThroughAssist = $linkBehavior->shouldRouteThroughAssist($check, $showAssist);
                                @endphp

                                <div class="rounded-lg border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
                                    <div class="flex flex-wrap items-start justify-between gap-3">
                                        <div class="space-y-1">
                                            <div class="text-sm font-medium text-gray-900 dark:text-white">
                                                {{ $title }}
                                            </div>
                                            @if ($message)
                                                <div class="text-sm text-gray-600 dark:text-gray-300">
                                                    {{ $message }}
                                                </div>
                                            @endif
                                        </div>

                                        <div class="flex shrink-0 flex-wrap items-center justify-end gap-2">
                                            @if ($blocking)
                                                <x-filament::badge color="danger" size="sm">
                                                    Blocker
                                                </x-filament::badge>
                                            @endif

                                            <x-filament::badge :color="$severitySpec->color" :icon="$severitySpec->icon" size="sm">
                                                {{ $severitySpec->label }}
                                            </x-filament::badge>
                                            <x-filament::badge :color="$statusSpec->color" :icon="$statusSpec->icon" size="sm">
                                                {{ $statusSpec->label }}
                                            </x-filament::badge>

                                            @if ($ackAction !== null && $canAcknowledge && $checkKey !== '')
                                                {{ ($ackAction)(['check_key' => $checkKey]) }}
                                            @endif
                                        </div>
                                    </div>

                                    @if ($nextSteps->isNotEmpty())
                                        <div class="mt-4">
                                            <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                                                Next steps
                                            </div>
                                            <ul class="mt-2 space-y-1 text-sm">
                                                @foreach ($nextSteps as $step)
                                                    @php
                                                        $label = is_string($step['label'] ?? null) ? trim((string) $step['label']) : '';
                                                        $url = is_string($step['url'] ?? null) ? trim((string) $step['url']) : '';
                                                        $testId = $label !== '' ? 'verification-next-step-'.\Illuminate\Support\Str::slug($label) : null;
                                                        $behavior = $routeNextStepsThroughAssist
                                                            ? null
                                                            : $linkBehavior->describe($label, $url);
                                                    @endphp

                                                    @if ($label !== '' && $url !== '')
                                                        <li>
                                                            @if ($routeNextStepsThroughAssist)
                                                                <button
                                                                    type="button"
                                                                    class="inline-flex items-center gap-2 text-primary-600 hover:underline dark:text-primary-400"
                                                                    wire:click="mountAction('{{ $assistActionName }}')"
                                                                    @if ($testId)
                                                                        data-testid="{{ $testId }}"
                                                                    @endif
                                                                >
                                                                    <span>{{ $label }}</span>
                                                                    <span class="text-xs text-gray-500 dark:text-gray-400">
                                                                        Open in assist
                                                                    </span>
                                                                </button>
                                                            @else
                                                                <a
                                                                    href="{{ $url }}"
                                                                    class="inline-flex items-center gap-2 text-primary-600 hover:underline dark:text-primary-400"
                                                                    @if ($testId)
                                                                        data-testid="{{ $testId }}"
                                                                    @endif
                                                                    @if ((bool) ($behavior['opens_in_new_tab'] ?? false))
                                                                        target="_blank" rel="noopener noreferrer"
                                                                    @endif
                                                                >
                                                                    <span>{{ $label }}</span>
                                                                    @if ((bool) ($behavior['show_new_tab_hint'] ?? false))
                                                                        <span class="text-xs text-gray-500 dark:text-gray-400">
                                                                            Opens in new tab
                                                                        </span>
                                                                    @endif
                                                                </a>
                                                            @endif
                                                        </li>
                                                    @endif
                                                @endforeach
                                            </ul>
                                        </div>
                                    @endif
                                </div>
                            @endforeach
                        </div>
                    </div>
                @endif
            @endforeach
        </div>
    @endif
</div>