@php
    /** @var \App\Models\OperationRun $run */
    $run = $this->run;

    $statusSpec = \App\Support\Badges\BadgeRenderer::spec(
        \App\Support\Badges\BadgeDomain::OperationRunStatus,
        [
            'status' => (string) $run->status,
            'freshness_state' => $run->freshnessState()->value,
        ],
    );

    $outcomeSpec = \App\Support\Badges\BadgeRenderer::spec(
        \App\Support\Badges\BadgeDomain::OperationRunOutcome,
        [
            'outcome' => (string) $run->outcome,
            'status' => (string) $run->status,
            'freshness_state' => $run->freshnessState()->value,
        ],
    );

    $summaryCounts = is_array($run->summary_counts) ? $run->summary_counts : [];
    $hasSummary = count($summaryCounts) > 0;
    $integrityNote = \App\Support\RedactionIntegrity::noteForRun($run);
    $guidance = \App\Support\OpsUx\OperationUxPresenter::surfaceGuidance($run);
    $decisionTruth = \App\Support\OpsUx\OperationUxPresenter::decisionZoneTruth($run);
    $workspaceClosureSpec = \App\Support\Badges\BadgeRenderer::spec(
        \App\Support\Badges\BadgeDomain::WorkspaceClosurePosture,
        $run->workspace?->isClosed() ? 'closed' : 'open',
    );
    $tenantWorkspacePostureSpec = \App\Support\Badges\BadgeRenderer::spec(
        \App\Support\Badges\BadgeDomain::TenantWorkspacePosture,
        $run->tenant?->isRemovedFromWorkspace() ? 'removed_from_workspace' : 'active',
    );
@endphp

<x-filament-panels::page>
    <div class="space-y-6">
        <x-filament::section>
            <x-slot name="heading">
                Operation #{{ (int) $run->getKey() }}
            </x-slot>

            <x-slot name="description">
                {{ \App\Support\OperationCatalog::label((string) $run->type) }}
            </x-slot>

            <x-slot name="afterHeader">
                <div class="flex items-center gap-2">
                    <x-filament::badge
                        :color="$statusSpec->color"
                        :icon="$statusSpec->icon"
                    >
                        {{ $statusSpec->label }}
                    </x-filament::badge>

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

            <dl class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Workspace</dt>
                    <dd class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ $run->workspace?->name ?? 'Unknown workspace' }}
                    </dd>
                </div>

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">ManagedEnvironment</dt>
                    <dd class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ $run->tenant?->name ?? 'Tenantless' }}
                    </dd>
                </div>

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Workspace lifecycle</dt>
                    <dd class="mt-1">
                        <x-filament::badge :color="$workspaceClosureSpec->color" :icon="$workspaceClosureSpec->icon">
                            {{ $workspaceClosureSpec->label }}
                        </x-filament::badge>
                    </dd>
                </div>

                @if ($run->tenant)
                    <div>
                        <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Tenant workspace posture</dt>
                        <dd class="mt-1">
                            <x-filament::badge :color="$tenantWorkspacePostureSpec->color" :icon="$tenantWorkspacePostureSpec->icon">
                                {{ $tenantWorkspacePostureSpec->label }}
                            </x-filament::badge>
                        </dd>
                    </div>
                @endif

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Started</dt>
                    <dd class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ $run->started_at?->toDayDateTimeString() ?? '—' }}
                    </dd>
                </div>

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Completed</dt>
                    <dd class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ $run->completed_at?->toDayDateTimeString() ?? '—' }}
                    </dd>
                </div>

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Initiator</dt>
                    <dd class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ (string) ($run->initiator_name ?? '—') }}
                    </dd>
                </div>

                <div>
                    <dt class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Navigation</dt>
                    <dd class="mt-1 text-sm">
                        <div class="flex flex-col gap-2">
                            <x-filament::link href="{{ \App\Support\System\SystemOperationRunLinks::index() }}">
                                Show all operations
                            </x-filament::link>

                            <x-filament::link href="{{ \App\Filament\System\Pages\Ops\Runbooks::getUrl(panel: 'system') }}">
                                Go to runbooks
                            </x-filament::link>
                        </div>
                    </dd>
                </div>
            </dl>
        </x-filament::section>

        <x-filament::section>
            <x-slot name="heading">
                Current lifecycle truth
            </x-slot>

            <div class="grid grid-cols-1 gap-4 sm:grid-cols-3">
                <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                    <div class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Still active</div>
                    <div class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ ($decisionTruth['isCurrentlyActive'] ?? false) ? 'Yes' : 'No' }}
                    </div>
                </div>

                <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                    <div class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Automatic reconciliation</div>
                    <div class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ ($decisionTruth['isReconciled'] ?? false) ? 'Yes' : 'No' }}
                    </div>
                </div>

                <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                    <div class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Problem class</div>
                    <div class="mt-1 text-sm font-medium text-gray-950 dark:text-white">
                        {{ $decisionTruth['problemClassLabel'] ?? 'None' }}
                    </div>
                </div>
            </div>

            @if (filled($decisionTruth['attentionNote'] ?? null))
                <div class="mt-4 rounded-lg border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-100">
                    {{ $decisionTruth['attentionNote'] }}
                </div>
            @endif

            @if (filled($decisionTruth['staleLineageNote'] ?? null))
                <div class="mt-4 rounded-lg border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-900 dark:border-rose-500/30 dark:bg-rose-500/10 dark:text-rose-100">
                    {{ $decisionTruth['staleLineageNote'] }}
                </div>
            @endif
        </x-filament::section>

        @if ($integrityNote)
            <x-filament::section>
                <div class="rounded-lg border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-100">
                    {{ $integrityNote }}
                </div>
            </x-filament::section>
        @endif

        @if ($guidance)
            <x-filament::section>
                <x-slot name="heading">
                    Next step
                </x-slot>

                <div class="rounded-lg border border-gray-200 bg-gray-50 px-4 py-3 text-sm text-gray-800 dark:border-white/10 dark:bg-white/5 dark:text-gray-100">
                    {{ $guidance }}
                </div>
            </x-filament::section>
        @endif

        @if ($hasSummary)
            <x-filament::section>
                <x-slot name="heading">
                    Summary counts
                </x-slot>

                <div class="grid grid-cols-2 gap-4 sm:grid-cols-3 lg:grid-cols-4">
                    @foreach ($summaryCounts as $key => $value)
                        <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                            <p class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">
                                {{ \App\Support\OpsUx\SummaryCountsNormalizer::label((string) $key) }}
                            </p>
                            <p class="mt-1 text-xl font-bold text-gray-950 dark:text-white">
                                {{ is_numeric($value) ? number_format((int) $value) : $value }}
                            </p>
                        </div>
                    @endforeach
                </div>
            </x-filament::section>
        @endif

        @if (! empty($run->failure_summary))
            <x-filament::section>
                <x-slot name="heading">
                    <div class="flex items-center gap-2 text-danger-600 dark:text-danger-400">
                        <x-heroicon-m-exclamation-circle class="h-5 w-5" />
                        Failures
                    </div>
                </x-slot>

                @include('filament.partials.json-viewer', ['value' => $run->failure_summary])
            </x-filament::section>
        @endif
    </div>
</x-filament-panels::page>
