<x-filament-panels::page>
    @php($scope = $this->appliedScope())
    @php($summary = $this->summaryCounts())
    @php($reasonFilters = $this->availableReasonFilters())

    <div class="space-y-6">
        <x-filament::section>
            <div class="flex flex-col gap-4">
                <div class="space-y-2">
                    <div class="inline-flex w-fit items-center gap-2 rounded-full border border-danger-200 bg-danger-50 px-3 py-1 text-xs font-medium text-danger-700 dark:border-danger-700/60 dark:bg-danger-950/40 dark:text-danger-200">
                        <x-filament::icon icon="heroicon-o-wrench-screwdriver" class="h-3.5 w-3.5" />
                        Findings hygiene
                    </div>

                    <div class="space-y-1">
                        <h1 class="text-2xl font-semibold tracking-tight text-gray-950 dark:text-white">
                            Findings hygiene report
                        </h1>

                        <p class="max-w-3xl text-sm leading-6 text-gray-600 dark:text-gray-300">
                            Review visible broken assignments and stale in-progress work across entitled tenants in one read-first repair queue. Existing finding detail stays the only place where reassignment or lifecycle repair happens.
                        </p>
                    </div>
                </div>

                <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
                    <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-white/10 dark:bg-white/5">
                        <div class="text-xs font-medium uppercase tracking-[0.14em] text-gray-500 dark:text-gray-400">
                            Visible issues
                        </div>
                        <div class="mt-2 text-3xl font-semibold text-gray-950 dark:text-white">
                            {{ $summary['unique_issue_count'] }}
                        </div>
                        <div class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                            One row per visible finding, even when multiple hygiene reasons apply.
                        </div>
                    </div>

                    <div class="rounded-2xl border border-danger-200 bg-danger-50/70 p-4 shadow-sm dark:border-danger-700/50 dark:bg-danger-950/30">
                        <div class="text-xs font-medium uppercase tracking-[0.14em] text-danger-700 dark:text-danger-200">
                            Broken assignments
                        </div>
                        <div class="mt-2 text-3xl font-semibold text-danger-950 dark:text-danger-100">
                            {{ $summary['broken_assignment_count'] }}
                        </div>
                        <div class="mt-1 text-sm text-danger-800 dark:text-danger-200">
                            Assignees who can no longer act on the finding.
                        </div>
                    </div>

                    <div class="rounded-2xl border border-warning-200 bg-warning-50/70 p-4 shadow-sm dark:border-warning-700/50 dark:bg-warning-950/30">
                        <div class="text-xs font-medium uppercase tracking-[0.14em] text-warning-700 dark:text-warning-200">
                            Stale in progress
                        </div>
                        <div class="mt-2 text-3xl font-semibold text-warning-950 dark:text-warning-100">
                            {{ $summary['stale_in_progress_count'] }}
                        </div>
                        <div class="mt-1 text-sm text-warning-800 dark:text-warning-200">
                            In-progress findings with no meaningful workflow movement for seven days.
                        </div>
                    </div>

                    <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-white/10 dark:bg-white/5">
                        <div class="text-xs font-medium uppercase tracking-[0.14em] text-gray-500 dark:text-gray-400">
                            Applied scope
                        </div>
                        <div class="mt-2 text-sm font-semibold text-gray-950 dark:text-white">
                            {{ $scope['reason_filter_label'] }}
                        </div>
                        <div class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                            @if (($scope['tenant_prefilter_source'] ?? 'none') === 'active_tenant_context')
                                ManagedEnvironment prefilter from active context:
                                <span class="font-medium text-gray-950 dark:text-white">{{ $scope['tenant_label'] }}</span>
                            @elseif (($scope['tenant_prefilter_source'] ?? 'none') === 'explicit_filter')
                                ManagedEnvironment filter applied:
                                <span class="font-medium text-gray-950 dark:text-white">{{ $scope['tenant_label'] }}</span>
                            @else
                                All visible tenants are currently included.
                            @endif
                        </div>
                    </div>
                </div>

                <div class="flex flex-wrap gap-2">
                    @foreach ($reasonFilters as $reasonFilter)
                        <a
                            href="{{ $reasonFilter['url'] }}"
                            class="inline-flex items-center gap-2 rounded-full border px-3 py-1.5 text-sm font-medium transition {{ $reasonFilter['active'] ? 'border-primary-200 bg-primary-50 text-primary-700 dark:border-primary-700/60 dark:bg-primary-950/40 dark:text-primary-300' : 'border-gray-200 bg-white text-gray-700 hover:border-gray-300 hover:text-gray-950 dark:border-white/10 dark:bg-white/5 dark:text-gray-300 dark:hover:border-white/20 dark:hover:text-white' }}"
                        >
                            <span>{{ $reasonFilter['label'] }}</span>
                            <span class="rounded-full bg-black/5 px-2 py-0.5 text-xs font-semibold dark:bg-white/10">
                                {{ $reasonFilter['badge_count'] }}
                            </span>
                            <span class="text-[11px] uppercase tracking-[0.12em] opacity-70">Fixed</span>
                        </a>
                    @endforeach
                </div>
            </div>
        </x-filament::section>

        {{ $this->table }}
    </div>
</x-filament-panels::page>
