@php
    $fieldWrapperView = $getFieldWrapperView();
    $run = is_array($run ?? null) ? $run : null;
    $runUrl = is_string($runUrl ?? null) && trim((string) $runUrl) !== '' ? trim((string) $runUrl) : null;
    $surface = is_array($surface ?? null) ? $surface : [];
    $redactionNotes = is_array($redactionNotes ?? null)
        ? array_values(array_filter($redactionNotes, 'is_string'))
        : [];
    $contextualHelp = is_array($contextualHelp ?? null) ? $contextualHelp : null;
    $assistVisibility = is_array($assistVisibility ?? null) ? $assistVisibility : [];
    $assistActionName = is_string($assistActionName ?? null) && trim((string) $assistActionName) !== ''
        ? trim((string) $assistActionName)
        : 'wizardVerificationRequiredPermissionsAssist';
    $technicalDetailsActionName = is_string($technicalDetailsActionName ?? null) && trim((string) $technicalDetailsActionName) !== ''
        ? trim((string) $technicalDetailsActionName)
        : 'wizardVerificationTechnicalDetails';
    $showAssist = (bool) ($assistVisibility['is_visible'] ?? false);
    $completedAt = is_string($run['completed_at'] ?? null) && trim((string) $run['completed_at']) !== '' ? (string) $run['completed_at'] : null;
    $completedAtLabel = null;

    if ($completedAt !== null) {
        try {
            $completedAtLabel = \Carbon\CarbonImmutable::parse($completedAt)->format('Y-m-d H:i');
        } catch (\Throwable) {
            $completedAtLabel = $completedAt;
        }
    }

    $status = is_string($run['status'] ?? null) ? (string) $run['status'] : null;
    $runState = is_string($runState ?? null) ? (string) $runState : null;

    if (! in_array($runState, ['no_run', 'active', 'completed'], true)) {
        $runState = $run === null
            ? 'no_run'
            : ($status === 'completed' ? 'completed' : 'active');
    }

    $ackAction = null;

    if (isset($this) && method_exists($this, 'acknowledgeVerificationCheckAction')) {
        $ackAction = $this->acknowledgeVerificationCheckAction();
    }

    $linkBehavior = app(\App\Support\Verification\VerificationLinkBehavior::class);
@endphp

<x-dynamic-component :component="$fieldWrapperView" :field="$field">
    <div class="space-y-4">
        <x-filament::section
            heading="Stored verification details"
            :description="$completedAtLabel ? ('Completed: ' . $completedAtLabel) : 'Stored details for the latest verification operation.'"
        >
            @if ($runState === 'no_run')
                <div class="space-y-2 rounded-lg border border-gray-200 bg-white p-4 text-sm text-gray-600 shadow-sm dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300">
                    <div class="font-medium text-gray-900 dark:text-white">
                        No verification operation has been started yet.
                    </div>

                    <div>
                        Use the workflow action above to start verification for this environment.
                    </div>
                </div>
            @elseif ($runState === 'active')
                <div class="rounded-lg border border-gray-200 bg-white p-4 text-sm text-gray-600 shadow-sm dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300">
                    Report unavailable while the operation is in progress. Stored status updates automatically about every 5 seconds. Use “Refresh” to re-check immediately.
                </div>

                <div class="mt-4 flex flex-wrap items-center gap-2">
                    @if ($runUrl)
                        <x-filament::button
                            tag="a"
                            :href="$runUrl"
                            color="primary"
                            size="sm"
                        >
                            {{ \App\Support\OperationRunLinks::openLabel() }}
                        </x-filament::button>
                    @endif

                    <x-filament::button
                        color="gray"
                        size="sm"
                        wire:click="mountAction('{{ $technicalDetailsActionName }}')"
                    >
                        Technical details
                    </x-filament::button>
                </div>
            @else
                <div class="space-y-4">
                    <div class="flex flex-wrap items-center gap-2">
                        @if ($runUrl)
                            <x-filament::button
                                tag="a"
                                :href="$runUrl"
                                color="primary"
                                size="sm"
                            >
                                {{ \App\Support\OperationRunLinks::openLabel() }}
                            </x-filament::button>
                        @endif

                        <x-filament::button
                            color="gray"
                            size="sm"
                            wire:click="mountAction('{{ $technicalDetailsActionName }}')"
                        >
                            Technical details
                        </x-filament::button>
                    </div>

                    @if ($contextualHelp !== null)
                        @include('filament.components.product-knowledge.contextual-help', ['help' => $contextualHelp])
                    @endif

                    @include('filament.components.verification-report-viewer', [
                        'surface' => $surface,
                        'redactionNotes' => $redactionNotes,
                        'canAcknowledge' => (bool) ($canAcknowledge ?? false),
                        'ackAction' => $ackAction,
                        'showAssist' => $showAssist,
                        'assistActionName' => $assistActionName,
                        'linkBehavior' => $linkBehavior,
                    ])
                </div>
            @endif
        </x-filament::section>
    </div>
</x-dynamic-component>
