@php
    $run = $run ?? null;
    $run = is_array($run) ? $run : null;

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

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

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

    $outcome = $run['outcome'] ?? null;
    $outcome = is_string($outcome) ? $outcome : null;

    $startedAt = $run['started_at'] ?? null;
    $startedAt = is_string($startedAt) && $startedAt !== '' ? $startedAt : null;

    $updatedAt = $run['updated_at'] ?? null;
    $updatedAt = is_string($updatedAt) && $updatedAt !== '' ? $updatedAt : null;

    $completedAt = $run['completed_at'] ?? null;
    $completedAt = is_string($completedAt) && $completedAt !== '' ? $completedAt : null;

    $hasReport = $hasReport ?? false;
    $hasReport = is_bool($hasReport) ? $hasReport : false;

    $formatTs = static function (?string $ts): ?string {
        if ($ts === null) {
            return null;
        }

        try {
            return \Carbon\CarbonImmutable::parse($ts)->format('Y-m-d H:i');
        } catch (\Throwable) {
            return $ts;
        }
    };

    $relativeTs = static function (?string $ts): ?string {
        if ($ts === null) {
            return null;
        }

        try {
            return \Carbon\CarbonImmutable::parse($ts)->diffForHumans(null, true, true);
        } catch (\Throwable) {
            return null;
        }
    };

    $targetScope = $run['target_scope'] ?? [];
    $targetScope = is_array($targetScope) ? $targetScope : [];

    $entraTenantId = $targetScope['entra_tenant_id'] ?? null;
    $entraTenantId = is_string($entraTenantId) && $entraTenantId !== '' ? $entraTenantId : null;

    $runStatusSpec = null;

    if ($status !== null) {
        $runStatusSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::OperationRunStatus, $status);
    }

    $runOutcomeSpec = null;

    if ($outcome !== null && $status === 'completed') {
        $runOutcomeSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::OperationRunOutcome, $outcome);
    }

    $workerHint = match ($status) {
        'queued' => 'Awaiting worker',
        'running' => 'Worker running',
        default => null,
    };
@endphp

<div class="space-y-4">
    @if ($run === null)
        <div class="text-sm text-gray-600 dark:text-gray-300">
            No verification operation has been started yet.
        </div>
    @else
        <div class="flex flex-wrap items-center gap-2">
            <div class="text-sm font-medium text-gray-900 dark:text-white">
                {{ \App\Support\OperationRunLinks::identifier((int) ($run['id'] ?? 0)) }}
            </div>

            @if ($runStatusSpec)
                <x-filament::badge :color="$runStatusSpec->color" :icon="$runStatusSpec->icon" size="sm">
                    {{ $runStatusSpec->label }}
                </x-filament::badge>
            @endif

            @if ($runOutcomeSpec)
                <x-filament::badge :color="$runOutcomeSpec->color" :icon="$runOutcomeSpec->icon" size="sm">
                    {{ $runOutcomeSpec->label }}
                </x-filament::badge>
            @endif
        </div>

        @if ($status !== 'completed')
            <div class="space-y-1 text-sm text-gray-600 dark:text-gray-300">
                @if ($workerHint)
                    <div>{{ $workerHint }}.</div>
                @endif

                @if (! $hasReport)
                    <div>No report yet. Stored status updates automatically about every 5 seconds.</div>
                @else
                    <div>Partial results are available. The wizard updates automatically about every 5 seconds, or you can use “Refresh” for an immediate re-check.</div>
                @endif
            </div>
        @endif

        <div class="grid grid-cols-1 gap-3 sm:grid-cols-2">
            <div class="rounded-lg border border-gray-200 bg-white p-3 dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Operation</div>
                <div class="mt-1 text-sm text-gray-900 dark:text-white">{{ (string) ($run['type'] ?? '—') }}</div>
                <div class="mt-2 text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Scope (Entra tenant)</div>
                <div class="mt-1 text-sm text-gray-900 dark:text-white">{{ $entraTenantId ?? '—' }}</div>
            </div>

            <div class="rounded-lg border border-gray-200 bg-white p-3 dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Timestamps</div>
                <dl class="mt-2 space-y-1 text-sm text-gray-700 dark:text-gray-200">
                    <div class="flex items-start justify-between gap-4">
                        <dt class="text-gray-500 dark:text-gray-400">Started</dt>
                        <dd class="text-right">{{ $formatTs($startedAt) ?? '—' }}</dd>
                    </div>
                    <div class="flex items-start justify-between gap-4">
                        <dt class="text-gray-500 dark:text-gray-400">Last update</dt>
                        <dd class="text-right">
                            {{ $formatTs($updatedAt) ?? '—' }}
                            @if ($updatedAt !== null && ($relativeTs($updatedAt) !== null))
                                <span class="text-gray-500 dark:text-gray-400">({{ $relativeTs($updatedAt) }} ago)</span>
                            @endif
                        </dd>
                    </div>
                    <div class="flex items-start justify-between gap-4">
                        <dt class="text-gray-500 dark:text-gray-400">Completed</dt>
                        <dd class="text-right">{{ $formatTs($completedAt) ?? '—' }}</dd>
                    </div>
                </dl>
            </div>
        </div>

        @if ($previousRunUrl || $runUrl)
            <div class="rounded-lg border border-gray-200 bg-white p-3 dark:border-gray-800 dark:bg-gray-900">
                <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                    Diagnostics links
                </div>

                <div class="mt-2 space-y-3">
                    @if ($previousRunUrl)
                        <div class="space-y-1">
                            <a
                                href="{{ $previousRunUrl }}"
                                class="text-sm font-medium text-gray-600 hover:underline dark:text-gray-300"
                                target="_blank"
                                rel="noreferrer"
                            >
                                Open previous operation
                            </a>

                            <div class="text-xs text-gray-500 dark:text-gray-400">
                                Compare against the prior verification run without changing the onboarding workflow.
                            </div>
                        </div>
                    @endif

                    @if ($runUrl)
                        <div class="space-y-1">
                            <a
                                href="{{ $runUrl }}"
                                class="text-sm font-medium text-gray-600 hover:underline dark:text-gray-300"
                                target="_blank"
                                rel="noreferrer"
                            >
                                {{ \App\Support\OperationRunLinks::advancedMonitoringLabel() }}
                            </a>

                            <div class="text-xs text-gray-500 dark:text-gray-400">
                                {{ \App\Support\OperationRunLinks::advancedMonitoringDescription() }}
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        @endif
    @endif
</div>
