@php
    $findingsLastRun = $this->findingsLastRun();
    $findingsLastRunStatusSpec = $findingsLastRun
        ? \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::OperationRunStatus, (string) $findingsLastRun->status)
        : null;
    $findingsLastRunOutcomeSpec = $findingsLastRun && (string) $findingsLastRun->status === 'completed'
        ? \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::OperationRunOutcome, (string) $findingsLastRun->outcome)
        : null;
@endphp

<x-filament-panels::page>
    <div class="space-y-6">
        <x-filament::section>
            <div class="flex items-start gap-3">
                <x-heroicon-o-exclamation-triangle class="h-6 w-6 shrink-0 text-amber-500 dark:text-amber-400" />

                <div>
                    <p class="text-sm font-semibold text-amber-700 dark:text-amber-300">Operator warning</p>
                    <p class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                        Runbooks can modify or assess customer data across tenants. Always run preflight first, and ensure you have the correct scope selected.
                    </p>
                </div>
            </div>
        </x-filament::section>

        <x-filament::section>
            <x-slot name="heading">
                Rebuild Findings Lifecycle
            </x-slot>

            <x-slot name="description">
                Backfills legacy findings lifecycle fields, SLA due dates, and consolidates drift duplicate findings.
            </x-slot>

            <x-slot name="afterHeader">
                <x-filament::badge color="info" size="sm">
                    {{ $this->findingsScopeLabel() }}
                </x-filament::badge>
            </x-slot>

            <div class="space-y-4">
                @if ($findingsLastRun)
                    <div class="flex flex-wrap items-center gap-x-4 gap-y-2 rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                        <span class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Last run</span>

                        <span class="text-sm text-gray-700 dark:text-gray-300">
                            {{ $findingsLastRun->created_at?->diffForHumans() ?? '—' }}
                        </span>

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

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

                        @if ($findingsLastRun->initiator_name)
                            <span class="text-xs text-gray-500 dark:text-gray-400">
                                by {{ $findingsLastRun->initiator_name }}
                            </span>
                        @endif
                    </div>
                @endif

                @if (is_array($this->findingsPreflight))
                    <div class="grid grid-cols-1 gap-4 sm:grid-cols-3">
                        <x-filament::section>
                            <div class="text-center">
                                <p class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Affected</p>
                                <p class="mt-2 text-3xl font-bold tabular-nums text-gray-950 dark:text-white">
                                    {{ number_format((int) ($this->findingsPreflight['affected_count'] ?? 0)) }}
                                </p>
                            </div>
                        </x-filament::section>

                        <x-filament::section>
                            <div class="text-center">
                                <p class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Total scanned</p>
                                <p class="mt-2 text-3xl font-bold tabular-nums text-gray-950 dark:text-white">
                                    {{ number_format((int) ($this->findingsPreflight['total_count'] ?? 0)) }}
                                </p>
                            </div>
                        </x-filament::section>

                        <x-filament::section>
                            <div class="text-center">
                                <p class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Estimated tenants</p>
                                <p class="mt-2 text-3xl font-bold tabular-nums text-gray-950 dark:text-white">
                                    {{ is_numeric($this->findingsPreflight['estimated_tenants'] ?? null) ? number_format((int) $this->findingsPreflight['estimated_tenants']) : '—' }}
                                </p>
                            </div>
                        </x-filament::section>
                    </div>

                    @if ((int) ($this->findingsPreflight['affected_count'] ?? 0) <= 0)
                        <div class="flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400">
                            <x-heroicon-m-check-circle class="h-5 w-5 text-success-500" />
                            Nothing to do for the current scope.
                        </div>
                    @endif
                @else
                    <div class="flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400">
                        <x-heroicon-m-magnifying-glass class="h-5 w-5" />
                        Run <span class="mx-1 font-semibold text-gray-700 dark:text-gray-200">Preflight</span> to see how many findings would change for the selected scope.
                    </div>
                @endif
            </div>
        </x-filament::section>

    </div>
</x-filament-panels::page>
