<?php

namespace App\Filament\Resources;

use App\Filament\Support\VerificationReportChangeIndicator;
use App\Filament\Support\VerificationReportViewer;
use App\Models\ManagedEnvironment;
use App\Models\OperationRun;
use App\Models\RestoreRun;
use App\Models\User;
use App\Models\VerificationCheckAcknowledgement;
use App\Support\Badges\BadgeCatalog;
use App\Support\Badges\BadgeDomain;
use App\Support\Badges\BadgeRenderer;
use App\Support\Baselines\BaselineCompareEvidenceGapDetails;
use App\Support\Baselines\BaselineCompareReasonCode;
use App\Support\Filament\FilterOptionCatalog;
use App\Support\Filament\FilterPresets;
use App\Support\Filament\TablePaginationProfiles;
use App\Support\Inventory\InventoryCoverage;
use App\Support\Inventory\InventoryPolicyTypeMeta;
use App\Support\Inventory\TenantCoverageTruthResolver;
use App\Support\Navigation\CrossResourceNavigationMatrix;
use App\Support\Navigation\RelatedNavigationResolver;
use App\Support\OperateHub\OperateHubShell;
use App\Support\OperationCatalog;
use App\Support\OperationRunLinks;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\OperationRunType;
use App\Support\OpsUx\OperationUxPresenter;
use App\Support\OpsUx\RunDurationInsights;
use App\Support\OpsUx\SummaryCountsNormalizer;
use App\Support\ReasonTranslation\ReasonPresenter;
use App\Support\ReasonTranslation\ReasonResolutionEnvelope;
use App\Support\RestoreSafety\RestoreSafetyResolver;
use App\Support\Tenants\ReferencedTenantLifecyclePresentation;
use App\Support\Ui\ActionSurface\ActionSurfaceDeclaration;
use App\Support\Ui\ActionSurface\ActionSurfaceDefaults;
use App\Support\Ui\ActionSurface\Enums\ActionSurfaceInspectAffordance;
use App\Support\Ui\ActionSurface\Enums\ActionSurfaceProfile;
use App\Support\Ui\ActionSurface\Enums\ActionSurfaceSlot;
use App\Support\Ui\ActionSurface\Enums\ActionSurfaceType;
use App\Support\Ui\GovernanceArtifactTruth\ArtifactTruthEnvelope;
use App\Support\Ui\GovernanceArtifactTruth\ArtifactTruthPresenter;
use App\Support\Ui\GovernanceArtifactTruth\CompressedGovernanceOutcome;
use App\Support\Ui\GovernanceArtifactTruth\SurfaceCompressionContext;
use App\Support\Ui\OperatorExplanation\OperatorExplanationPattern;
use App\Support\Workspaces\WorkspaceContext;
use BackedEnum;
use Filament\Facades\Filament;
use Filament\Infolists\Components\ViewEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;

class OperationRunResource extends Resource
{
    protected static bool $isScopedToTenant = false;

    protected static ?string $model = OperationRun::class;

    protected static ?string $slug = 'operations';

    protected static bool $shouldRegisterNavigation = false;

    protected static bool $isGloballySearchable = false;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-queue-list';

    protected static string|UnitEnum|null $navigationGroup = 'Monitoring';

    protected static ?string $navigationLabel = 'Operations';

    public static function actionSurfaceDeclaration(): ActionSurfaceDeclaration
    {
        return ActionSurfaceDeclaration::forResource(ActionSurfaceProfile::RunLog, ActionSurfaceType::ReadOnlyRegistryReport)
            ->withDefaults(new ActionSurfaceDefaults(
                moreGroupLabel: 'More',
                exportIsDefaultBulkActionForReadOnly: false,
            ))
            ->exempt(
                ActionSurfaceSlot::ListHeader,
                'Run-log list intentionally has no list-header actions; navigation actions are provided by Monitoring shell pages.',
            )
            ->satisfy(ActionSurfaceSlot::InspectAffordance, ActionSurfaceInspectAffordance::ClickableRow->value)
            ->exempt(
                ActionSurfaceSlot::ListBulkMoreGroup,
                'Operation runs are immutable records; bulk export is deferred and tracked outside this retrofit.',
            )
            ->exempt(
                ActionSurfaceSlot::ListEmptyState,
                'Empty-state action is intentionally omitted; users can adjust filters/date range in-page.',
            )
            ->satisfy(
                ActionSurfaceSlot::DetailHeader,
                'Tenantless detail view keeps back-navigation, refresh, related links, and resumable operation actions in the header.',
            );
    }

    public static function getEloquentQuery(): Builder
    {
        $workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();

        return parent::getEloquentQuery()
            ->with('user')
            ->latest('id')
            ->when($workspaceId, fn (Builder $query) => $query->where('workspace_id', (int) $workspaceId))
            ->when(! $workspaceId, fn (Builder $query) => $query->whereRaw('1 = 0'));
    }

    public static function form(Schema $schema): Schema
    {
        return $schema;
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema
            ->schema([
                ViewEntry::make('enterprise_detail')
                    ->label('')
                    ->view('filament.infolists.entries.enterprise-detail.layout')
                    ->state(fn (OperationRun $record): array => static::enterpriseDetailPage($record)->toArray())
                    ->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->paginated(TablePaginationProfiles::resource())
            ->persistFiltersInSession()
            ->persistSearchInSession()
            ->persistSortInSession()
            ->recordUrl(fn (OperationRun $record): string => OperationRunLinks::tenantlessView($record))
            ->columns([
                Tables\Columns\TextColumn::make('status')
                    ->badge()
                    ->formatStateUsing(fn (mixed $state, OperationRun $record): string => BadgeRenderer::spec(BadgeDomain::OperationRunStatus, static::statusBadgeState($record))->label)
                    ->color(fn (mixed $state, OperationRun $record): string => BadgeRenderer::spec(BadgeDomain::OperationRunStatus, static::statusBadgeState($record))->color)
                    ->icon(fn (mixed $state, OperationRun $record): ?string => BadgeRenderer::spec(BadgeDomain::OperationRunStatus, static::statusBadgeState($record))->icon)
                    ->iconColor(fn (mixed $state, OperationRun $record): ?string => BadgeRenderer::spec(BadgeDomain::OperationRunStatus, static::statusBadgeState($record))->iconColor)
                    ->description(fn (OperationRun $record): ?string => static::lifecycleAttentionSummary($record)),
                Tables\Columns\TextColumn::make('type')
                    ->label('Operation')
                    ->formatStateUsing(fn (?string $state): string => OperationCatalog::label((string) $state))
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('initiator_name')
                    ->label('Initiator')
                    ->searchable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Started')
                    ->since()
                    ->sortable(),
                Tables\Columns\TextColumn::make('duration')
                    ->getStateUsing(function (OperationRun $record): string {
                        if ($record->started_at && $record->completed_at) {
                            return $record->completed_at->diffForHumans($record->started_at, true);
                        }

                        return '—';
                    }),
                Tables\Columns\TextColumn::make('outcome')
                    ->badge()
                    ->formatStateUsing(fn (mixed $state, OperationRun $record): string => BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record))->label)
                    ->color(fn (mixed $state, OperationRun $record): string => BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record))->color)
                    ->icon(fn (mixed $state, OperationRun $record): ?string => BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record))->icon)
                    ->iconColor(fn (mixed $state, OperationRun $record): ?string => BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record))->iconColor)
                    ->description(fn (OperationRun $record): ?string => static::surfaceGuidance($record)),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('managed_environment_id')
                    ->label('Environment')
                    ->options(function (): array {
                        $user = auth()->user();

                        if (! $user instanceof User) {
                            return [];
                        }

                        return collect($user->getTenants(Filament::getCurrentOrDefaultPanel()))
                            ->mapWithKeys(static fn (ManagedEnvironment $tenant): array => [
                                (string) $tenant->getKey() => $tenant->getFilamentName(),
                            ])
                            ->all();
                    })
                    ->default(function (): ?string {
                        $activeEnvironment = app(OperateHubShell::class)->activeEntitledTenant(request());

                        if (! $activeEnvironment instanceof ManagedEnvironment) {
                            return null;
                        }

                        $workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();

                        if ($workspaceId === null || (int) $activeEnvironment->workspace_id !== (int) $workspaceId) {
                            return null;
                        }

                        return (string) $activeEnvironment->getKey();
                    })
                    ->searchable(),
                Tables\Filters\SelectFilter::make('type')
                    ->options(function (): array {
                        $workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();

                        if ($workspaceId === null) {
                            return [];
                        }

                        $types = OperationRun::query()
                            ->where('workspace_id', (int) $workspaceId)
                            ->select('type')
                            ->distinct()
                            ->orderBy('type')
                            ->pluck('type', 'type')
                            ->all();

                        return FilterOptionCatalog::operationTypes(array_keys($types));
                    })
                    ->query(function (Builder $query, array $data): Builder {
                        $value = data_get($data, 'value');

                        if (! is_string($value) || trim($value) === '') {
                            return $query;
                        }

                        return $query->whereIn('type', OperationCatalog::rawValuesForCanonical(
                            OperationCatalog::canonicalCode($value),
                        ));
                    }),
                Tables\Filters\SelectFilter::make('status')
                    ->options(BadgeCatalog::options(BadgeDomain::OperationRunStatus, OperationRunStatus::values())),
                Tables\Filters\SelectFilter::make('outcome')
                    ->options(BadgeCatalog::options(BadgeDomain::OperationRunOutcome, OperationRunOutcome::values(includeReserved: false))),
                Tables\Filters\SelectFilter::make('initiator_name')
                    ->label('Initiator')
                    ->options(function (): array {
                        $workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();

                        if ($workspaceId === null) {
                            return [];
                        }

                        return OperationRun::query()
                            ->where('workspace_id', (int) $workspaceId)
                            ->whereNotNull('initiator_name')
                            ->select('initiator_name')
                            ->distinct()
                            ->orderBy('initiator_name')
                            ->pluck('initiator_name', 'initiator_name')
                            ->all();
                    })
                    ->searchable(),
                FilterPresets::dateRange('created_at', 'Created', 'created_at', [
                    'from' => now()->subDays(30)->toDateString(),
                    'until' => now()->toDateString(),
                ]),
            ])
            ->actions([])
            ->bulkActions([])
            ->emptyStateHeading('No operations found')
            ->emptyStateDescription('Queued, running, and completed operations will appear here when work is triggered in this scope.')
            ->emptyStateIcon('heroicon-o-queue-list');
    }

    private static function enterpriseDetailPage(OperationRun $record): \App\Support\Ui\EnterpriseDetail\EnterpriseDetailPageData
    {
        $factory = new \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory;

        $statusSpec = BadgeRenderer::spec(BadgeDomain::OperationRunStatus, static::statusBadgeState($record));
        $outcomeSpec = BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record));
        $targetScope = static::targetScopeDisplay($record) ?? 'No target scope details were recorded for this operation.';
        $summaryLine = \App\Support\OpsUx\SummaryCountsNormalizer::renderSummaryLine(is_array($record->summary_counts) ? $record->summary_counts : []);
        $referencedTenantLifecycle = $record->tenant instanceof ManagedEnvironment
            ? ReferencedTenantLifecyclePresentation::forOperationRun($record->tenant)
            : null;
        $artifactTruth = static::artifactTruthEnvelope($record);
        $operatorExplanation = $artifactTruth?->operatorExplanation;
        $diagnosticSummary = OperationUxPresenter::governanceDiagnosticSummary($record);
        $reasonEnvelope = app(ReasonPresenter::class)->forOperationRun($record, 'run_detail');
        $primaryNextStep = static::resolvePrimaryNextStep($record, $artifactTruth, $operatorExplanation);
        $decisionNextStep = $diagnosticSummary instanceof \App\Support\OpsUx\GovernanceRunDiagnosticSummary
            ? [
                'text' => $diagnosticSummary->nextActionText,
                'source' => $diagnosticSummary->nextActionCategory,
                'secondaryGuidance' => $primaryNextStep['secondaryGuidance'],
            ]
            : $primaryNextStep;
        $restoreContinuation = static::restoreContinuation($record);
        $supportingGroups = static::supportingGroups(
            record: $record,
            factory: $factory,
            referencedTenantLifecycle: $referencedTenantLifecycle,
            diagnosticSummary: $diagnosticSummary,
            operatorExplanation: $operatorExplanation,
            reasonEnvelope: $reasonEnvelope,
            primaryNextStep: $decisionNextStep,
        );

        $builder = \App\Support\Ui\EnterpriseDetail\EnterpriseDetailBuilder::make('operation_run', 'workspace-context')
            ->header(new \App\Support\Ui\EnterpriseDetail\SummaryHeaderData(
                title: OperationCatalog::label((string) $record->type),
                subtitle: OperationRunLinks::identifier($record),
                statusBadges: [
                    $factory->statusBadge($statusSpec->label, $statusSpec->color, $statusSpec->icon, $statusSpec->iconColor),
                    $factory->statusBadge($outcomeSpec->label, $outcomeSpec->color, $outcomeSpec->icon, $outcomeSpec->iconColor),
                ],
                keyFacts: [
                    $factory->keyFact('Target', $targetScope),
                    $factory->keyFact('Elapsed', RunDurationInsights::elapsedHuman($record)),
                ],
                descriptionHint: 'Decision guidance and high-signal context stay ahead of diagnostic payloads and raw JSON.',
            ))
            ->decisionZone($factory->decisionZone(
                facts: static::decisionFacts(
                    factory: $factory,
                    record: $record,
                    statusSpec: $statusSpec,
                    outcomeSpec: $outcomeSpec,
                    artifactTruth: $artifactTruth,
                    operatorExplanation: $operatorExplanation,
                    restoreContinuation: $restoreContinuation,
                    diagnosticSummary: $diagnosticSummary,
                ),
                primaryNextStep: $factory->primaryNextStep(
                    $decisionNextStep['text'],
                    $decisionNextStep['source'],
                    $decisionNextStep['secondaryGuidance'],
                    'Primary next step',
                ),
                description: $diagnosticSummary instanceof \App\Support\OpsUx\GovernanceRunDiagnosticSummary
                    ? 'Start here to see what happened, how reliable the resulting artifact is, what was affected, and the one next step.'
                    : 'Start here to see how the operation ended, whether the result is trustworthy enough to use, and the one primary next step.',
                compactCounts: $summaryLine !== null
                    ? $factory->countPresentation(summaryLine: $summaryLine)
                    : null,
                attentionNote: static::decisionAttentionNote($record),
            ));

        if ($supportingGroups !== []) {
            $builder->addSupportingGroup(...$supportingGroups);
        }

        $builder->addSection(
            $factory->viewSection(
                id: 'related_context',
                kind: 'related_context',
                title: 'Related context',
                view: 'filament.infolists.entries.related-context',
                viewData: ['entries' => static::relatedContextEntries($record)],
                emptyState: $factory->emptyState('No related context is available for this record.'),
            ),
            $factory->viewSection(
                id: 'artifact_truth',
                kind: 'supporting_detail',
                title: 'Artifact truth details',
                view: 'filament.infolists.entries.governance-artifact-truth',
                viewData: [
                    'artifactTruthState' => $artifactTruth?->toArray(static::artifactTruthOutcome($record)),
                    'surface' => 'expanded',
                ],
                visible: $artifactTruth !== null,
                description: 'Detailed artifact-truth context explains evidence quality and caveats without repeating the top decision summary.',
                collapsible: true,
                collapsed: true,
            ),
        );

        $counts = static::summaryCountFacts($record, $factory);

        if ($counts !== []) {
            $builder->addTechnicalSection(
                $factory->technicalDetail(
                    title: 'Count diagnostics',
                    entries: $counts,
                    description: 'Normalized run counters remain available for deeper inspection without competing with the primary decision.',
                    collapsible: true,
                    collapsed: true,
                    variant: 'diagnostic',
                ),
            );
        }

        if (! empty($record->failure_summary)) {
            $builder->addTechnicalSection(
                $factory->technicalDetail(
                    title: (string) $record->outcome === OperationRunOutcome::Blocked->value ? 'Blocked execution details' : 'Failures',
                    description: 'Detailed failure evidence stays available for investigation after the decision and supporting context.',
                    view: 'filament.infolists.entries.snapshot-json',
                    viewData: ['payload' => $record->failure_summary ?? []],
                    collapsible: true,
                    collapsed: false,
                ),
            );
        }

        if (static::reconciliationPayload($record) !== []) {
            $builder->addTechnicalSection(
                $factory->technicalDetail(
                    title: 'Lifecycle reconciliation',
                    description: 'Lifecycle reconciliation is diagnostic evidence showing when TenantPilot force-resolved the run.',
                    view: 'filament.infolists.entries.snapshot-json',
                    viewData: ['payload' => static::reconciliationPayload($record)],
                    collapsible: true,
                    collapsed: true,
                ),
            );
        }

        if (OperationCatalog::canonicalCode((string) $record->type) === OperationRunType::BaselineCompare->value) {
            $baselineCompareFacts = static::baselineCompareFacts($record, $factory);
            $baselineCompareEvidence = static::baselineCompareEvidencePayload($record);
            $gapDetails = BaselineCompareEvidenceGapDetails::fromOperationRun($record);
            $gapSummary = is_array($gapDetails['summary'] ?? null) ? $gapDetails['summary'] : [];
            $gapBuckets = is_array($gapDetails['buckets'] ?? null) ? $gapDetails['buckets'] : [];

            if ($baselineCompareFacts !== []) {
                $builder->addSection(
                    $factory->factsSection(
                        id: 'baseline_compare',
                        kind: 'type_specific_detail',
                        title: 'Baseline compare',
                        items: $baselineCompareFacts,
                        description: 'Type-specific comparison detail stays below the canonical decision and supporting layers.',
                        collapsible: true,
                        collapsed: true,
                    ),
                );
            }

            if (($gapSummary['detail_state'] ?? 'no_gaps') !== 'no_gaps') {
                $builder->addSection(
                    $factory->viewSection(
                        id: 'baseline_compare_gap_details',
                        kind: 'type_specific_detail',
                        title: 'Evidence gap details',
                        description: 'Governed subjects affected by evidence gaps, grouped by reason and searchable by reason, governed subject, or subject key.',
                        view: 'filament.infolists.entries.evidence-gap-subjects',
                        viewData: [
                            'summary' => $gapSummary,
                            'buckets' => $gapBuckets,
                            'searchId' => 'baseline-compare-gap-search-'.$record->getKey(),
                        ],
                        collapsible: true,
                        collapsed: true,
                    ),
                );
            }

            if ($baselineCompareEvidence !== []) {
                $builder->addSection(
                    $factory->viewSection(
                        id: 'baseline_compare_evidence',
                        kind: 'type_specific_detail',
                        title: 'Baseline compare evidence',
                        view: 'filament.infolists.entries.snapshot-json',
                        viewData: ['payload' => $baselineCompareEvidence],
                        collapsible: true,
                        collapsed: true,
                    ),
                );
            }
        }

        if (OperationCatalog::canonicalCode((string) $record->type) === OperationRunType::BaselineCapture->value) {
            $baselineCaptureEvidence = static::baselineCaptureEvidencePayload($record);

            if ($baselineCaptureEvidence !== []) {
                $builder->addSection(
                    $factory->viewSection(
                        id: 'baseline_capture_evidence',
                        kind: 'type_specific_detail',
                        title: 'Baseline capture evidence',
                        view: 'filament.infolists.entries.snapshot-json',
                        viewData: ['payload' => $baselineCaptureEvidence],
                        collapsible: true,
                        collapsed: true,
                    ),
                );
            }
        }

        $inventorySyncCoverageSection = static::inventorySyncCoverageSection($record);

        if ($inventorySyncCoverageSection !== null) {
            $builder->addSection(
                $factory->viewSection(
                    id: 'inventory_sync_coverage',
                    kind: 'type_specific_detail',
                    title: 'Inventory sync coverage',
                    description: 'Per-type run results explain what this sync established without forcing operators into raw JSON first.',
                    view: 'filament.infolists.entries.inventory-coverage-truth',
                    viewData: $inventorySyncCoverageSection,
                ),
            );
        }

        if (VerificationReportViewer::shouldRenderForRun($record)) {
            $builder->addSection(
                $factory->viewSection(
                    id: 'verification_report',
                    kind: 'type_specific_detail',
                    title: 'Verification report',
                    view: 'filament.components.verification-report-viewer',
                    viewData: static::verificationReportViewData($record),
                ),
            );
        }

        $builder->addTechnicalSection(
            $factory->technicalDetail(
                title: 'Context',
                entries: [
                    $factory->keyFact('Identity hash', $record->run_identity_hash, mono: true),
                    $factory->keyFact('Workspace scope', $record->workspace_id),
                    $factory->keyFact('ManagedEnvironment scope', $record->managed_environment_id),
                ],
                description: 'Stored run context stays available for debugging without dominating the default reading path.',
                view: 'filament.infolists.entries.snapshot-json',
                viewData: ['payload' => static::contextPayload($record)],
            ),
        );

        return $builder->build();
    }

    /**
     * @return list<\App\Support\Ui\EnterpriseDetail\SupportingCardData>
     */
    private static function supportingGroups(
        OperationRun $record,
        \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory $factory,
        ?ReferencedTenantLifecyclePresentation $referencedTenantLifecycle,
        ?\App\Support\OpsUx\GovernanceRunDiagnosticSummary $diagnosticSummary,
        ?OperatorExplanationPattern $operatorExplanation,
        ?ReasonResolutionEnvelope $reasonEnvelope,
        array $primaryNextStep,
    ): array {
        $groups = [];
        $hasElevatedLifecycleState = static::lifecycleAttentionSummary($record) !== null;
        $reasonSemantics = app(ReasonPresenter::class)->semantics($reasonEnvelope);

        $guidanceItems = array_values(array_filter([
            ...array_map(
                static fn (array $fact): array => $factory->keyFact(
                    (string) ($fact['label'] ?? 'Summary detail'),
                    (string) ($fact['value'] ?? '—'),
                    is_string($fact['hint'] ?? null) ? $fact['hint'] : null,
                    tone: match ($fact['emphasis'] ?? null) {
                        'blocked' => 'danger',
                        'caution' => 'warning',
                        default => null,
                    },
                ),
                $diagnosticSummary instanceof \App\Support\OpsUx\GovernanceRunDiagnosticSummary
                    ? array_values(array_filter($diagnosticSummary->secondaryFacts, 'is_array'))
                    : [],
            ),
            $operatorExplanation instanceof OperatorExplanationPattern && $operatorExplanation->coverageStatement !== null
                ? $factory->keyFact('Coverage', $operatorExplanation->coverageStatement)
                : null,
            $operatorExplanation instanceof OperatorExplanationPattern && $operatorExplanation->diagnosticsSummary !== null
                ? $factory->keyFact('Diagnostics summary', $operatorExplanation->diagnosticsSummary)
                : null,
            ...array_map(
                static fn (array $guidance): array => $factory->keyFact($guidance['label'], $guidance['text']),
                array_values(array_filter(
                    $primaryNextStep['secondaryGuidance'] ?? [],
                    static fn (mixed $guidance): bool => is_array($guidance),
                )),
            ),
            static::blockedExecutionReasonCode($record) !== null
                ? $factory->keyFact('Blocked reason', static::blockedExecutionReasonCode($record))
                : null,
            static::blockedExecutionDetail($record) !== null
                ? $factory->keyFact('Blocked detail', static::blockedExecutionDetail($record))
                : null,
            static::blockedExecutionSource($record) !== null
                ? $factory->keyFact('Blocked by', static::blockedExecutionSource($record))
                : null,
            RunDurationInsights::stuckGuidance($record) !== null
                ? $factory->keyFact('Queue guidance', RunDurationInsights::stuckGuidance($record))
                : null,
        ]));

        if ($guidanceItems !== []) {
            $groups[] = $factory->supportingFactsCard(
                kind: 'guidance',
                title: 'Guidance',
                items: $guidanceItems,
                description: 'Secondary guidance explains caveats and context without competing with the primary next step.',
            );
        }

        $reasonSemanticsItems = array_values(array_filter([
            is_string($reasonSemantics['owner_label'] ?? null)
                ? $factory->keyFact('Reason owner', (string) $reasonSemantics['owner_label'])
                : null,
            is_string($reasonSemantics['family_label'] ?? null)
                ? $factory->keyFact('Platform reason family', (string) $reasonSemantics['family_label'])
                : null,
        ]));

        if ($reasonSemanticsItems !== []) {
            $groups[] = $factory->supportingFactsCard(
                kind: 'reason_semantics',
                title: 'Explanation semantics',
                items: $reasonSemanticsItems,
                description: 'Platform meaning stays separate from domain-specific diagnostic detail during rollout.',
            );
        }

        $lifecycleItems = array_values(array_filter([
            $referencedTenantLifecycle !== null
                ? $factory->keyFact(
                    'ManagedEnvironment lifecycle',
                    $referencedTenantLifecycle->presentation->label,
                    badge: $factory->statusBadge(
                        $referencedTenantLifecycle->presentation->label,
                        $referencedTenantLifecycle->presentation->badgeColor,
                        $referencedTenantLifecycle->presentation->badgeIcon,
                        $referencedTenantLifecycle->presentation->badgeIconColor,
                    ),
                )
                : null,
            $referencedTenantLifecycle?->selectorAvailabilityMessage() !== null
                ? $factory->keyFact('ManagedEnvironment selector context', $referencedTenantLifecycle->selectorAvailabilityMessage())
                : null,
            $referencedTenantLifecycle?->contextNote !== null
                ? $factory->keyFact('Viewer context', $referencedTenantLifecycle->contextNote)
                : null,
            ! $hasElevatedLifecycleState && static::freshnessLabel($record) !== null
                ? $factory->keyFact('Freshness', (string) static::freshnessLabel($record))
                : null,
            ! $hasElevatedLifecycleState && static::reconciliationHeadline($record) !== null
                ? $factory->keyFact('Lifecycle truth', (string) static::reconciliationHeadline($record))
                : null,
            static::reconciledAtLabel($record) !== null
                ? $factory->keyFact('Reconciled at', (string) static::reconciledAtLabel($record))
                : null,
            static::reconciliationSourceLabel($record) !== null
                ? $factory->keyFact('Reconciled by', (string) static::reconciliationSourceLabel($record))
                : null,
        ]));

        if ($lifecycleItems !== []) {
            $groups[] = $factory->supportingFactsCard(
                kind: 'lifecycle',
                title: 'Lifecycle',
                items: $lifecycleItems,
                description: 'Lifecycle context explains freshness, reconciliation, and tenant-scoped caveats.',
            );
        }

        $timingItems = [
            $factory->keyFact('Created', static::formatDetailTimestamp($record->created_at)),
            $factory->keyFact('Started', static::formatDetailTimestamp($record->started_at)),
            $factory->keyFact('Completed', static::formatDetailTimestamp($record->completed_at)),
            $factory->keyFact('Elapsed', RunDurationInsights::elapsedHuman($record)),
        ];

        $groups[] = $factory->supportingFactsCard(
            kind: 'timing',
            title: 'Timing',
            items: $timingItems,
        );

        $metadataItems = array_values(array_filter([
            $factory->keyFact('Initiator', $record->initiator_name),
            RunDurationInsights::expectedHuman($record) !== null
                ? $factory->keyFact('Expected duration', RunDurationInsights::expectedHuman($record))
                : null,
        ]));

        if ($metadataItems !== []) {
            $groups[] = $factory->supportingFactsCard(
                kind: 'metadata',
                title: 'Metadata',
                items: $metadataItems,
                description: 'Secondary metadata remains visible without crowding the top decision surface.',
            );
        }

        return $groups;
    }

    /**
     * @return array{
     *     text: string,
     *     source: string,
     *     secondaryGuidance: list<array{label: string, text: string, source: string}>
     * }
     */
    private static function resolvePrimaryNextStep(
        OperationRun $record,
        ?ArtifactTruthEnvelope $artifactTruth,
        ?OperatorExplanationPattern $operatorExplanation,
    ): array {
        $candidates = [];

        static::pushNextStepCandidate($candidates, $operatorExplanation?->nextActionText, 'operator_explanation');
        static::pushNextStepCandidate($candidates, $artifactTruth?->nextStepText(), 'artifact_truth');

        $opsUxSource = match (true) {
            (string) $record->outcome === OperationRunOutcome::Blocked->value => 'blocked_reason',
            static::lifecycleAttentionSummary($record) !== null => 'lifecycle_attention',
            default => 'ops_ux',
        };

        static::pushNextStepCandidate($candidates, static::surfaceGuidance($record), $opsUxSource);

        if ($candidates === []) {
            return [
                'text' => 'No action needed.',
                'source' => 'none_required',
                'secondaryGuidance' => [],
            ];
        }

        $primary = $candidates[0];
        $primarySource = static::normalizeGuidance($primary['text']) === 'no action needed'
            ? 'none_required'
            : $primary['source'];

        $secondaryGuidance = array_map(
            static fn (array $candidate): array => [
                'label' => static::guidanceLabel($candidate['source']),
                'text' => $candidate['text'],
                'source' => $candidate['source'],
            ],
            array_slice($candidates, 1),
        );

        return [
            'text' => $primary['text'],
            'source' => $primarySource,
            'secondaryGuidance' => $secondaryGuidance,
        ];
    }

    /**
     * @param  array<int, array{text: string, source: string, normalized: string}>  $candidates
     */
    private static function pushNextStepCandidate(array &$candidates, ?string $text, string $source): void
    {
        $formattedText = static::formatGuidanceText($text);

        if ($formattedText === null) {
            return;
        }

        $normalized = static::normalizeGuidance($formattedText);

        foreach ($candidates as $candidate) {
            if (($candidate['normalized'] ?? null) === $normalized) {
                return;
            }
        }

        $candidates[] = [
            'text' => $formattedText,
            'source' => $source,
            'normalized' => $normalized,
        ];
    }

    private static function formatGuidanceText(?string $text): ?string
    {
        if (! is_string($text)) {
            return null;
        }

        $text = trim($text);

        if ($text === '') {
            return null;
        }

        if (preg_match('/[.!?]$/', $text) === 1) {
            return $text;
        }

        return $text.'.';
    }

    private static function normalizeGuidance(string $text): string
    {
        $normalized = mb_strtolower(trim($text));
        $normalized = preg_replace('/^next step:\s*/', '', $normalized) ?? $normalized;

        return trim($normalized, " \t\n\r\0\x0B.!?");
    }

    private static function guidanceLabel(string $source): string
    {
        return match ($source) {
            'operator_explanation' => 'Operator guidance',
            'artifact_truth' => 'Artifact guidance',
            'blocked_reason' => 'Blocked prerequisite',
            'lifecycle_attention' => 'Lifecycle guidance',
            default => 'General guidance',
        };
    }

    /**
     * @return array<string, mixed>|null
     */
    private static function artifactTruthFact(
        \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory $factory,
        ?ArtifactTruthEnvelope $artifactTruth,
        ?string $hintOverride = null,
        bool $preferOverride = false,
    ): ?array {
        if (! $artifactTruth instanceof ArtifactTruthEnvelope) {
            return null;
        }

        $outcome = app(ArtifactTruthPresenter::class)->compressedOutcomeFromEnvelope(
            $artifactTruth,
            SurfaceCompressionContext::operationRunArtifact(),
        );
        $badge = $outcome->primaryBadge;

        return $factory->keyFact(
            'Artifact impact',
            $outcome->primaryLabel,
            $preferOverride ? $hintOverride : ($hintOverride ?? $outcome->primaryReason),
            $factory->statusBadge($badge->label, $badge->color, $badge->icon, $badge->iconColor),
        );
    }

    /**
     * @return list<array<string, mixed>>
     */
    private static function decisionFacts(
        \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory $factory,
        OperationRun $record,
        \App\Support\Badges\BadgeSpec $statusSpec,
        \App\Support\Badges\BadgeSpec $outcomeSpec,
        ?ArtifactTruthEnvelope $artifactTruth,
        ?OperatorExplanationPattern $operatorExplanation,
        mixed $restoreContinuation,
        ?\App\Support\OpsUx\GovernanceRunDiagnosticSummary $diagnosticSummary,
    ): array {
        if (! $diagnosticSummary instanceof \App\Support\OpsUx\GovernanceRunDiagnosticSummary) {
            return array_values(array_filter([
                $factory->keyFact(
                    'Execution state',
                    $statusSpec->label,
                    badge: $factory->statusBadge($statusSpec->label, $statusSpec->color, $statusSpec->icon, $statusSpec->iconColor),
                ),
                $factory->keyFact(
                    'Outcome',
                    $outcomeSpec->label,
                    badge: $factory->statusBadge($outcomeSpec->label, $outcomeSpec->color, $outcomeSpec->icon, $outcomeSpec->iconColor),
                ),
                static::artifactTruthFact($factory, $artifactTruth),
                $operatorExplanation instanceof OperatorExplanationPattern
                    ? $factory->keyFact(
                        'Result meaning',
                        $operatorExplanation->evaluationResultLabel(),
                        $operatorExplanation->headline,
                    )
                    : null,
                $operatorExplanation instanceof OperatorExplanationPattern
                    ? $factory->keyFact(
                        'Result trust',
                        $operatorExplanation->trustworthinessLabel(),
                        static::detailHintUnlessDuplicate(
                            $operatorExplanation->reliabilityStatement,
                            $artifactTruth?->primaryExplanation,
                        ),
                    )
                    : null,
                is_array($restoreContinuation)
                    ? $factory->keyFact(
                        'Restore continuation',
                        (string) ($restoreContinuation['badge_label'] ?? 'Restore detail'),
                        (string) ($restoreContinuation['summary'] ?? 'Restore continuation detail is unavailable.'),
                    )
                    : null,
            ]));
        }

        $facts = [
            $factory->keyFact(
                'Execution state',
                $statusSpec->label,
                badge: $factory->statusBadge($statusSpec->label, $statusSpec->color, $statusSpec->icon, $statusSpec->iconColor),
            ),
            $factory->keyFact(
                'Outcome',
                $diagnosticSummary->executionOutcomeLabel,
                badge: $factory->statusBadge($outcomeSpec->label, $outcomeSpec->color, $outcomeSpec->icon, $outcomeSpec->iconColor),
            ),
            static::artifactTruthFact(
                $factory,
                $artifactTruth,
                static::detailHintUnlessDuplicate(
                    $diagnosticSummary->headline,
                    $artifactTruth?->primaryExplanation,
                    $diagnosticSummary->primaryReason,
                ),
                true,
            ),
            $factory->keyFact(
                'Dominant cause',
                $diagnosticSummary->dominantCause['label'],
                $diagnosticSummary->primaryReason,
                tone: in_array($diagnosticSummary->nextActionCategory, ['refresh_prerequisite_data', 'review_scope_or_ambiguous_matches'], true)
                    ? 'warning'
                    : (in_array($diagnosticSummary->nextActionCategory, ['retry_later', 'no_further_action'], true) ? null : 'danger'),
            ),
            $operatorExplanation instanceof OperatorExplanationPattern
                ? $factory->keyFact(
                    'Result trust',
                    $operatorExplanation->trustworthinessLabel(),
                    static::detailHintUnlessDuplicate(
                        $operatorExplanation->reliabilityStatement,
                        $diagnosticSummary->primaryReason,
                    ),
                    tone: match ($operatorExplanation->trustworthinessLevel->value) {
                        'unusable' => 'danger',
                        'diagnostic_only', 'limited_confidence' => 'warning',
                        default => 'success',
                    },
                )
                : null,
            is_array($restoreContinuation)
                ? $factory->keyFact(
                    'Restore continuation',
                    (string) ($restoreContinuation['badge_label'] ?? 'Restore detail'),
                    (string) ($restoreContinuation['summary'] ?? 'Restore continuation detail is unavailable.'),
                )
                : null,
        ];

        if (is_array($diagnosticSummary->affectedScaleCue)) {
            $source = str_replace('_', ' ', (string) ($diagnosticSummary->affectedScaleCue['source'] ?? 'recorded detail'));

            $facts[] = $factory->keyFact(
                (string) ($diagnosticSummary->affectedScaleCue['label'] ?? 'Affected scale'),
                (string) ($diagnosticSummary->affectedScaleCue['value'] ?? 'Recorded detail is available.'),
                'Backed by '.$source.'.',
            );
        }

        return array_values(array_filter($facts));
    }

    private static function decisionAttentionNote(OperationRun $record): ?string
    {
        return OperationUxPresenter::decisionAttentionNote($record);
    }

    private static function detailHintUnlessDuplicate(?string $hint, ?string ...$duplicates): ?string
    {
        $normalizedHint = static::normalizeDetailText($hint);

        if ($normalizedHint === null) {
            return null;
        }

        foreach ($duplicates as $duplicate) {
            if ($normalizedHint === static::normalizeDetailText($duplicate)) {
                return null;
            }
        }

        return trim($hint ?? '');
    }

    private static function normalizeDetailText(?string $value): ?string
    {
        if (! is_string($value)) {
            return null;
        }

        $normalized = trim((string) preg_replace('/\s+/', ' ', $value));

        if ($normalized === '') {
            return null;
        }

        return mb_strtolower($normalized);
    }

    /**
     * @return list<array<string, mixed>>
     */
    private static function summaryCountFacts(
        OperationRun $record,
        \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory $factory,
    ): array {
        $counts = \App\Support\OpsUx\SummaryCountsNormalizer::normalize(is_array($record->summary_counts) ? $record->summary_counts : []);

        return array_map(
            static fn (string $key, int $value): array => $factory->keyFact(
                SummaryCountsNormalizer::label($key),
                $value,
                tone: self::countTone($key, $value),
            ),
            array_keys($counts),
            array_values($counts),
        );
    }

    private static function countTone(string $key, int $value): ?string
    {
        if (in_array($key, ['failed', 'errors_recorded', 'findings_reopened'], true)) {
            return $value > 0 ? 'danger' : 'success';
        }

        if ($key === 'succeeded' && $value > 0) {
            return 'success';
        }

        return null;
    }

    private static function blockedExecutionReasonCode(OperationRun $record): ?string
    {
        if ((string) $record->outcome !== OperationRunOutcome::Blocked->value) {
            return null;
        }

        $reasonEnvelope = app(ReasonPresenter::class)->forOperationRun($record, 'run_detail');

        if ($reasonEnvelope !== null) {
            return $reasonEnvelope->operatorLabel;
        }

        $context = is_array($record->context) ? $record->context : [];
        $reasonCode = data_get($context, 'execution_legitimacy.reason_code')
            ?? data_get($context, 'reason_code')
            ?? data_get($record->failure_summary, '0.reason_code');

        return is_string($reasonCode) && trim($reasonCode) !== '' ? trim($reasonCode) : null;
    }

    private static function blockedExecutionDetail(OperationRun $record): ?string
    {
        if ((string) $record->outcome !== OperationRunOutcome::Blocked->value) {
            return null;
        }

        $reasonEnvelope = app(ReasonPresenter::class)->forOperationRun($record, 'run_detail');

        if ($reasonEnvelope !== null) {
            return $reasonEnvelope->shortExplanation;
        }

        $message = data_get($record->failure_summary, '0.message');

        return is_string($message) && trim($message) !== '' ? trim($message) : 'Execution was refused before work began.';
    }

    private static function blockedExecutionSource(OperationRun $record): ?string
    {
        if ((string) $record->outcome !== OperationRunOutcome::Blocked->value) {
            return null;
        }

        $context = is_array($record->context) ? $record->context : [];
        $blockedBy = $context['blocked_by'] ?? null;

        if (! is_string($blockedBy) || trim($blockedBy) === '') {
            return null;
        }

        return match (trim($blockedBy)) {
            'queued_execution_legitimacy' => 'Execution legitimacy revalidation',
            default => ucfirst(str_replace('_', ' ', trim($blockedBy))),
        };
    }

    /**
     * @return list<array<string, mixed>>
     */
    private static function baselineCompareFacts(
        OperationRun $record,
        \App\Support\Ui\EnterpriseDetail\EnterpriseDetailSectionFactory $factory,
    ): array {
        $context = is_array($record->context) ? $record->context : [];
        $gapDetails = BaselineCompareEvidenceGapDetails::fromOperationRun($record);
        $gapSummary = is_array($gapDetails['summary'] ?? null) ? $gapDetails['summary'] : [];
        $facts = [];

        $fidelity = data_get($context, 'baseline_compare.fidelity');
        if (is_string($fidelity) && trim($fidelity) !== '') {
            $facts[] = $factory->keyFact('Fidelity', $fidelity);
        }

        $proof = data_get($context, 'baseline_compare.coverage.proof');
        $uncoveredTypes = data_get($context, 'baseline_compare.coverage.uncovered_types');
        $uncoveredTypes = is_array($uncoveredTypes) ? array_values(array_filter($uncoveredTypes, 'is_string')) : [];

        $facts[] = $factory->keyFact(
            'Coverage',
            match (true) {
                $proof === false => 'Unproven',
                $uncoveredTypes !== [] => 'Warnings',
                $proof === true => 'Covered',
                default => 'Unknown',
            },
        );

        $reasonCode = data_get($context, 'baseline_compare.reason_code');
        if (is_string($reasonCode) && trim($reasonCode) !== '') {
            $enum = BaselineCompareReasonCode::tryFrom(trim($reasonCode));
            $facts[] = $factory->keyFact(
                'Why no findings',
                $enum?->message() ?? trim($reasonCode),
                trim($reasonCode),
            );
        }

        $strategy = data_get($context, 'baseline_compare.strategy');
        if (is_array($strategy)) {
            $strategyKey = is_string($strategy['key'] ?? null) && trim((string) $strategy['key']) !== ''
                ? trim((string) $strategy['key'])
                : null;
            $selectionState = is_string($strategy['selection_state'] ?? null) && trim((string) $strategy['selection_state']) !== ''
                ? trim((string) $strategy['selection_state'])
                : null;
            $operatorReason = is_string($strategy['operator_reason'] ?? null) && trim((string) $strategy['operator_reason']) !== ''
                ? trim((string) $strategy['operator_reason'])
                : null;
            $stateCounts = is_array($strategy['state_counts'] ?? null)
                ? array_filter(
                    array_map(static fn (mixed $count): int => (int) $count, $strategy['state_counts']),
                    static fn (int $count): bool => $count > 0,
                )
                : [];

            if ($strategyKey !== null) {
                $facts[] = $factory->keyFact(
                    'Compare strategy',
                    \Illuminate\Support\Str::of($strategyKey)->replace('_', ' ')->headline()->toString(),
                );
            }

            if ($selectionState !== null) {
                $facts[] = $factory->keyFact(
                    'Strategy selection',
                    \Illuminate\Support\Str::of($selectionState)->replace('_', ' ')->headline()->toString(),
                    $operatorReason,
                );
            }

            if ($stateCounts !== []) {
                $facts[] = $factory->keyFact(
                    'Strategy subject states',
                    collect($stateCounts)
                        ->map(static fn (int $count, string $state): string => \Illuminate\Support\Str::of($state)->replace('_', ' ')->headline()->append(' ', (string) $count)->toString())
                        ->implode(', '),
                );
            }
        }

        if ((int) ($gapSummary['count'] ?? 0) > 0) {
            $facts[] = $factory->keyFact(
                'Evidence gap detail',
                match ($gapSummary['detail_state'] ?? 'no_gaps') {
                    'structured_details_recorded' => 'Structured subject details available',
                    'details_not_recorded' => 'Detailed rows were not recorded',
                    'legacy_broad_reason' => 'Legacy development payload should be regenerated',
                    default => 'No evidence gaps recorded',
                },
            );
        }

        if ((int) ($gapSummary['structural_count'] ?? 0) > 0) {
            $facts[] = $factory->keyFact('Structural gaps', (string) (int) $gapSummary['structural_count']);
        }

        if ((int) ($gapSummary['operational_count'] ?? 0) > 0) {
            $facts[] = $factory->keyFact('Operational gaps', (string) (int) $gapSummary['operational_count']);
        }

        if ((int) ($gapSummary['transient_count'] ?? 0) > 0) {
            $facts[] = $factory->keyFact('Transient gaps', (string) (int) $gapSummary['transient_count']);
        }

        if ($uncoveredTypes !== []) {
            sort($uncoveredTypes, SORT_STRING);
            $facts[] = $factory->keyFact('Uncovered types', implode(', ', array_slice($uncoveredTypes, 0, 12)).(count($uncoveredTypes) > 12 ? '…' : ''));
        }

        $inventorySyncRunId = data_get($context, 'baseline_compare.inventory_sync_run_id');
        if (is_numeric($inventorySyncRunId)) {
            $facts[] = $factory->keyFact('Inventory sync run', '#'.(int) $inventorySyncRunId);
        }

        return $facts;
    }

    /**
     * @return array<string, mixed>
     */
    private static function baselineCompareEvidencePayload(OperationRun $record): array
    {
        $context = is_array($record->context) ? $record->context : [];

        return array_filter([
            'subjects_total' => is_numeric(data_get($context, 'baseline_compare.subjects_total'))
                ? (int) data_get($context, 'baseline_compare.subjects_total')
                : null,
            'evidence_gaps_count' => is_numeric(data_get($context, 'baseline_compare.evidence_gaps.count'))
                ? (int) data_get($context, 'baseline_compare.evidence_gaps.count')
                : null,
            'resume_token' => data_get($context, 'baseline_compare.resume_token'),
            'strategy' => is_array(data_get($context, 'baseline_compare.strategy'))
                ? data_get($context, 'baseline_compare.strategy')
                : null,
            'evidence_capture' => is_array(data_get($context, 'baseline_compare.evidence_capture'))
                ? data_get($context, 'baseline_compare.evidence_capture')
                : null,
            'evidence_gaps' => is_array(data_get($context, 'baseline_compare.evidence_gaps'))
                ? data_get($context, 'baseline_compare.evidence_gaps')
                : null,
        ], static fn (mixed $value): bool => $value !== null && $value !== []);
    }

    /**
     * @return array<string, mixed>
     */
    private static function baselineCaptureEvidencePayload(OperationRun $record): array
    {
        $context = is_array($record->context) ? $record->context : [];

        return array_filter([
            'subjects_total' => is_numeric(data_get($context, 'baseline_capture.subjects_total'))
                ? (int) data_get($context, 'baseline_capture.subjects_total')
                : null,
            'gaps_count' => is_numeric(data_get($context, 'baseline_capture.gaps.count'))
                ? (int) data_get($context, 'baseline_capture.gaps.count')
                : null,
            'resume_token' => data_get($context, 'baseline_capture.resume_token'),
            'evidence_capture' => is_array(data_get($context, 'baseline_capture.evidence_capture'))
                ? data_get($context, 'baseline_capture.evidence_capture')
                : null,
            'gaps' => is_array(data_get($context, 'baseline_capture.gaps'))
                ? data_get($context, 'baseline_capture.gaps')
                : null,
        ], static fn (mixed $value): bool => $value !== null && $value !== []);
    }

    /**
     * @return array<string, mixed>
     */
    private static function verificationReportViewData(OperationRun $record): array
    {
        $report = VerificationReportViewer::report($record);
        $fingerprint = is_array($report) ? VerificationReportViewer::fingerprint($report) : null;
        $changeIndicator = VerificationReportChangeIndicator::forRun($record);
        $previousRunUrl = null;

        if ($changeIndicator !== null) {
            $tenant = app(OperateHubShell::class)->activeEntitledTenant(request());

            $previousRunUrl = $tenant instanceof ManagedEnvironment
                ? OperationRunLinks::view($changeIndicator['previous_report_id'], $tenant)
                : OperationRunLinks::tenantlessView($changeIndicator['previous_report_id']);
        }

        $acknowledgements = VerificationCheckAcknowledgement::query()
            ->where('managed_environment_id', (int) ($record->managed_environment_id ?? 0))
            ->where('workspace_id', (int) ($record->workspace_id ?? 0))
            ->where('operation_run_id', (int) $record->getKey())
            ->with('acknowledgedByUser')
            ->get()
            ->mapWithKeys(static function (VerificationCheckAcknowledgement $ack): array {
                $user = $ack->acknowledgedByUser;

                return [
                    (string) $ack->check_key => [
                        'check_key' => (string) $ack->check_key,
                        'ack_reason' => (string) $ack->ack_reason,
                        'acknowledged_at' => $ack->acknowledged_at?->toJSON(),
                        'expires_at' => $ack->expires_at?->toJSON(),
                        'acknowledged_by' => $user instanceof User
                            ? [
                                'id' => (int) $user->getKey(),
                                'name' => (string) $user->name,
                            ]
                            : null,
                    ],
                ];
            })
            ->all();

        return [
            'report' => $report,
            'run' => [
                'id' => (int) $record->getKey(),
                'type' => (string) $record->type,
                'status' => (string) $record->status,
                'outcome' => (string) $record->outcome,
                'started_at' => $record->started_at?->toJSON(),
                'completed_at' => $record->completed_at?->toJSON(),
            ],
            'fingerprint' => $fingerprint,
            'changeIndicator' => $changeIndicator,
            'previousRunUrl' => $previousRunUrl,
            'acknowledgements' => $acknowledgements,
            'surface' => VerificationReportViewer::surface($record, $acknowledgements, [
                'hostKind' => 'operation_run_detail',
                'changeIndicator' => $changeIndicator,
                'previousRunUrl' => $previousRunUrl,
                'hostVariation' => [
                    'ownsNoRunState' => false,
                    'ownsActiveState' => false,
                    'supportsAssist' => false,
                    'supportsAcknowledge' => false,
                    'supportsTechnicalDetailsTrigger' => false,
                ],
            ]),
            'redactionNotes' => VerificationReportViewer::redactionNotes($report),
        ];
    }

    /**
     * @return array<string, mixed>
     */
    private static function contextPayload(OperationRun $record): array
    {
        $context = is_array($record->context) ? $record->context : [];

        if (array_key_exists('verification_report', $context)) {
            $context['verification_report'] = [
                'redacted' => true,
                'note' => 'Rendered in the Verification report section.',
            ];
        }

        return $context;
    }

    /**
     * @return array{status:string,freshness_state:string}
     */
    private static function statusBadgeState(OperationRun $record): array
    {
        return [
            'status' => (string) $record->status,
            'freshness_state' => $record->freshnessState()->value,
        ];
    }

    /**
     * @return array{outcome:string,status:string,freshness_state:string}
     */
    private static function outcomeBadgeState(OperationRun $record): array
    {
        return [
            'outcome' => (string) $record->outcome,
            'status' => (string) $record->status,
            'freshness_state' => $record->freshnessState()->value,
        ];
    }

    private static function freshnessLabel(OperationRun $record): ?string
    {
        return match ($record->freshnessState()->value) {
            'fresh_active' => 'Fresh activity',
            'likely_stale' => 'Likely stale',
            'reconciled_failed' => 'Automatically reconciled',
            'terminal_normal' => 'Terminal truth confirmed',
            default => null,
        };
    }

    private static function reconciliationHeadline(OperationRun $record): ?string
    {
        if (! $record->isLifecycleReconciled()) {
            return null;
        }

        return 'TenantPilot force-resolved this run after normal lifecycle truth was lost.';
    }

    private static function reconciledAtLabel(OperationRun $record): ?string
    {
        $reconciledAt = data_get($record->reconciliation(), 'reconciled_at');

        return is_string($reconciledAt) && trim($reconciledAt) !== '' ? trim($reconciledAt) : null;
    }

    private static function reconciliationSourceLabel(OperationRun $record): ?string
    {
        $source = data_get($record->reconciliation(), 'source');

        if (! is_string($source) || trim($source) === '') {
            return null;
        }

        return match (trim($source)) {
            'failed_callback' => 'Direct failed() bridge',
            'scheduled_reconciler' => 'Scheduled reconciler',
            'adapter_reconciler' => 'Adapter reconciler',
            default => ucfirst(str_replace('_', ' ', trim($source))),
        };
    }

    /**
     * @return array<string, mixed>
     */
    private static function reconciliationPayload(OperationRun $record): array
    {
        $reconciliation = $record->reconciliation();

        return $reconciliation;
    }

    /**
     * @return array{
     *     rows: list<array{
     *         type: string,
     *         label: string,
     *         segment: string,
     *         category: string,
     *         coverageState: string,
     *         followUpRequired: bool,
     *         followUpPriority: int,
     *         followUpGuidance: string,
     *         itemCount: int,
     *         errorCode: ?string
     *     }>,
     *     summary: array{
     *         totalTypes: int,
     *         succeededTypes: int,
     *         failedTypes: int,
     *         skippedTypes: int,
     *         followUpTypes: int,
     *         observedItems: int
     *     },
     *     runOutcomeLabel: string,
     *     runOutcomeColor: string,
     *     runOutcomeIcon: ?string
     * }|null
     */
    private static function inventorySyncCoverageSection(OperationRun $record): ?array
    {
        if (OperationCatalog::canonicalCode((string) $record->type) !== OperationRunType::InventorySync->value) {
            return null;
        }

        $coverage = $record->inventoryCoverage();

        if (! $coverage instanceof InventoryCoverage) {
            return null;
        }

        $rows = collect($coverage->rows())
            ->map(function (array $row): array {
                $type = (string) ($row['type'] ?? '');
                $meta = InventoryPolicyTypeMeta::metaFor($type);
                $status = is_string($row['status'] ?? null) ? (string) $row['status'] : InventoryCoverage::StatusFailed;
                $errorCode = is_string($row['error_code'] ?? null) ? (string) $row['error_code'] : null;
                $itemCount = is_int($row['item_count'] ?? null) ? (int) $row['item_count'] : 0;

                return [
                    'type' => $type,
                    'label' => is_string($meta['label'] ?? null) && $meta['label'] !== ''
                        ? (string) $meta['label']
                        : $type,
                    'segment' => (string) ($row['segment'] ?? 'policy'),
                    'category' => is_string($meta['category'] ?? null) && $meta['category'] !== ''
                        ? (string) $meta['category']
                        : 'Other',
                    'coverageState' => $status,
                    'followUpRequired' => $status !== InventoryCoverage::StatusSucceeded,
                    'followUpPriority' => TenantCoverageTruthResolver::followUpPriorityForState($status),
                    'followUpGuidance' => TenantCoverageTruthResolver::followUpGuidanceForState($status, $errorCode),
                    'itemCount' => $itemCount,
                    'errorCode' => $errorCode,
                ];
            })
            ->sort(function (array $left, array $right): int {
                $priority = ((int) ($left['followUpPriority'] ?? 0)) <=> ((int) ($right['followUpPriority'] ?? 0));

                if ($priority !== 0) {
                    return $priority;
                }

                $items = ((int) ($right['itemCount'] ?? 0)) <=> ((int) ($left['itemCount'] ?? 0));

                if ($items !== 0) {
                    return $items;
                }

                return strnatcasecmp((string) ($left['label'] ?? ''), (string) ($right['label'] ?? ''));
            })
            ->values()
            ->all();

        $outcomeSpec = BadgeRenderer::spec(BadgeDomain::OperationRunOutcome, static::outcomeBadgeState($record));

        return [
            'rows' => $rows,
            'summary' => [
                'totalTypes' => count($rows),
                'succeededTypes' => count(array_filter($rows, static fn (array $row): bool => ($row['coverageState'] ?? null) === InventoryCoverage::StatusSucceeded)),
                'failedTypes' => count(array_filter($rows, static fn (array $row): bool => ($row['coverageState'] ?? null) === InventoryCoverage::StatusFailed)),
                'skippedTypes' => count(array_filter($rows, static fn (array $row): bool => ($row['coverageState'] ?? null) === InventoryCoverage::StatusSkipped)),
                'followUpTypes' => count(array_filter($rows, static fn (array $row): bool => (bool) ($row['followUpRequired'] ?? false))),
                'observedItems' => array_sum(array_map(static fn (array $row): int => (int) ($row['itemCount'] ?? 0), $rows)),
            ],
            'runOutcomeLabel' => $outcomeSpec->label,
            'runOutcomeColor' => $outcomeSpec->color,
            'runOutcomeIcon' => $outcomeSpec->icon,
        ];
    }

    private static function formatDetailTimestamp(mixed $value): string
    {
        if (! $value instanceof \Illuminate\Support\Carbon) {
            return '—';
        }

        return $value->toDayDateTimeString();
    }

    public static function getPages(): array
    {
        return [];
    }

    private static function artifactTruthEnvelope(OperationRun $record, bool $fresh = false): ?ArtifactTruthEnvelope
    {
        if (! $record->supportsOperatorExplanation()) {
            return null;
        }

        $presenter = app(ArtifactTruthPresenter::class);

        return $fresh
            ? $presenter->forOperationRunFresh($record)
            : $presenter->forOperationRun($record);
    }

    private static function artifactTruthOutcome(OperationRun $record, bool $fresh = false): ?CompressedGovernanceOutcome
    {
        if (! $record->supportsOperatorExplanation()) {
            return null;
        }

        $presenter = app(ArtifactTruthPresenter::class);

        return $presenter->compressedOutcomeFor($record, SurfaceCompressionContext::operationRunArtifact(), $fresh);
    }

    private static function lifecycleAttentionSummary(OperationRun $record, bool $fresh = false): ?string
    {
        return $fresh
            ? OperationUxPresenter::lifecycleAttentionSummaryFresh($record)
            : OperationUxPresenter::lifecycleAttentionSummary($record);
    }

    private static function surfaceGuidance(OperationRun $record, bool $fresh = false): ?string
    {
        return $fresh
            ? OperationUxPresenter::surfaceGuidanceFresh($record)
            : OperationUxPresenter::surfaceGuidance($record);
    }

    /**
     * @return array{
     *     restore_run_id: int,
     *     state: string,
     *     summary: string,
     *     primary_next_action: string,
     *     recovery_claim_boundary: string,
     *     follow_up_required: bool,
     *     badge_label: string,
     *     link_url: ?string,
     *     link_available: bool
     * }|null
     */
    public static function restoreContinuation(OperationRun $record): ?array
    {
        if ($record->type !== 'restore.execute') {
            return null;
        }

        $context = is_array($record->context) ? $record->context : [];
        $restoreRunId = is_numeric($context['restore_run_id'] ?? null) ? (int) $context['restore_run_id'] : null;

        $restoreRun = $restoreRunId !== null
            ? RestoreRun::query()->find($restoreRunId)
            : RestoreRun::query()->where('operation_run_id', (int) $record->getKey())->latest('id')->first();

        if (! $restoreRun instanceof RestoreRun) {
            return null;
        }

        $attention = app(RestoreSafetyResolver::class)->resultAttentionForRun($restoreRun);
        $tenant = $record->tenant;
        $user = auth()->user();
        $canOpenRestore = $tenant instanceof ManagedEnvironment
            && $user instanceof User
            && app(\App\Services\Auth\CapabilityResolver::class)->isMember($user, $tenant);

        return [
            'restore_run_id' => (int) $restoreRun->getKey(),
            'state' => $attention->state,
            'summary' => $attention->summary,
            'primary_next_action' => $attention->primaryNextAction,
            'recovery_claim_boundary' => $attention->recoveryClaimBoundary,
            'follow_up_required' => $attention->followUpRequired,
            'badge_label' => \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::RestoreResultStatus, $attention->state)->label,
            'link_url' => $canOpenRestore
                ? RestoreRunResource::getUrl('view', ['record' => $restoreRun], tenant: $tenant)
                : null,
            'link_available' => $canOpenRestore,
        ];
    }

    /**
     * @return list<array{
     *     key: string,
     *     label: string,
     *     value: string,
     *     secondaryValue: ?string,
     *     targetUrl: ?string,
     *     targetKind: string,
     *     availability: string,
     *     unavailableReason: ?string,
     *     contextBadge: ?string,
     *     priority: int,
     *     actionLabel: string
     * }>
     */
    private static function relatedContextEntries(OperationRun $record, bool $fresh = false): array
    {
        $resolver = app(RelatedNavigationResolver::class);

        return $fresh
            ? $resolver->detailEntriesFresh(CrossResourceNavigationMatrix::SOURCE_OPERATION_RUN, $record)
            : $resolver->detailEntries(CrossResourceNavigationMatrix::SOURCE_OPERATION_RUN, $record);
    }

    private static function targetScopeDisplay(OperationRun $record): ?string
    {
        $context = is_array($record->context) ? $record->context : [];

        $targetScope = $context['target_scope'] ?? null;
        if (! is_array($targetScope)) {
            return null;
        }

        $scopeDisplayName = $targetScope['scope_display_name'] ?? null;
        $scopeIdentifier = $targetScope['scope_identifier'] ?? null;
        $scopeDisplayName = is_string($scopeDisplayName) ? trim($scopeDisplayName) : null;
        $scopeIdentifier = is_string($scopeIdentifier) ? trim($scopeIdentifier) : null;

        if ($scopeDisplayName !== null && $scopeDisplayName !== '') {
            return $scopeIdentifier !== null && $scopeIdentifier !== '' && $scopeIdentifier !== $scopeDisplayName
                ? "{$scopeDisplayName} ({$scopeIdentifier})"
                : $scopeDisplayName;
        }

        if ($scopeIdentifier !== null && $scopeIdentifier !== '') {
            return $scopeIdentifier;
        }

        $entraTenantName = $targetScope['entra_tenant_name'] ?? null;
        $entraTenantId = $targetScope['entra_tenant_id'] ?? null;
        $directoryContextId = $targetScope['directory_context_id'] ?? null;

        $entraTenantName = is_string($entraTenantName) ? trim($entraTenantName) : null;
        $entraTenantId = is_string($entraTenantId) ? trim($entraTenantId) : null;

        $directoryContextId = match (true) {
            is_string($directoryContextId) => trim($directoryContextId),
            is_int($directoryContextId) => (string) $directoryContextId,
            default => null,
        };

        $entra = null;

        if ($entraTenantName !== null && $entraTenantName !== '') {
            $entra = $entraTenantId ? "{$entraTenantName} ({$entraTenantId})" : $entraTenantName;
        } elseif ($entraTenantId !== null && $entraTenantId !== '') {
            $entra = $entraTenantId;
        }

        $parts = array_values(array_filter([
            $entra,
            $directoryContextId ? "directory_context_id: {$directoryContextId}" : null,
        ], fn (?string $value): bool => $value !== null && $value !== ''));

        return $parts !== [] ? implode(' · ', $parts) : null;
    }
}
