@php
    $summary = is_array($summary ?? null) ? $summary : [];
    $buckets = is_array($buckets ?? null) ? $buckets : [];
    $detailState = is_string($summary['detail_state'] ?? null) ? $summary['detail_state'] : 'no_gaps';
    $structuralCount = is_numeric($summary['structural_count'] ?? null) ? (int) $summary['structural_count'] : 0;
    $operationalCount = is_numeric($summary['operational_count'] ?? null) ? (int) $summary['operational_count'] : 0;
    $transientCount = is_numeric($summary['transient_count'] ?? null) ? (int) $summary['transient_count'] : 0;
    $tableContext = is_string($searchId ?? null) && $searchId !== '' ? $searchId : 'evidence-gap-search';
@endphp

@if ($detailState === 'legacy_broad_reason')
    <div class="rounded-xl border border-danger-300 bg-danger-50/80 p-4 dark:border-danger-800 dark:bg-danger-950/30">
        <div class="space-y-1">
            <div class="text-sm font-semibold text-danger-950 dark:text-danger-100">
                {{ __('baseline-compare.evidence_gap_legacy_title') }}
            </div>
            <p class="text-sm text-danger-900 dark:text-danger-200">
                {{ __('baseline-compare.evidence_gap_legacy_body') }}
            </p>
        </div>
    </div>
@elseif ($detailState === 'details_not_recorded' && $buckets === [])
    <div class="rounded-xl border border-warning-300 bg-warning-50/80 p-4 dark:border-warning-800 dark:bg-warning-950/30">
        <div class="space-y-1">
            <div class="text-sm font-semibold text-warning-950 dark:text-warning-100">
                {{ __('baseline-compare.evidence_gap_missing_details_title') }}
            </div>
            <p class="text-sm text-warning-900 dark:text-warning-200">
                {{ __('baseline-compare.evidence_gap_missing_details_body') }}
            </p>
        </div>
    </div>
@elseif ($buckets !== [])
    <div class="space-y-4">
        @if ($detailState === 'structured_details_recorded' && ($structuralCount > 0 || $operationalCount > 0 || $transientCount > 0))
            <div class="flex flex-wrap gap-2">
                <x-filament::badge color="danger" size="sm">
                    {{ __('baseline-compare.evidence_gap_structural', ['count' => $structuralCount]) }}
                </x-filament::badge>
                <x-filament::badge color="primary" size="sm">
                    {{ __('baseline-compare.evidence_gap_operational', ['count' => $operationalCount]) }}
                </x-filament::badge>
                <x-filament::badge color="warning" size="sm">
                    {{ __('baseline-compare.evidence_gap_transient', ['count' => $transientCount]) }}
                </x-filament::badge>
            </div>
        @endif

        @if ($detailState === 'details_not_recorded')
            <div class="rounded-xl border border-warning-300 bg-warning-50/80 p-4 dark:border-warning-800 dark:bg-warning-950/30">
                <div class="space-y-1">
                    <div class="text-sm font-semibold text-warning-950 dark:text-warning-100">
                        {{ __('baseline-compare.evidence_gap_missing_details_title') }}
                    </div>
                    <p class="text-sm text-warning-900 dark:text-warning-200">
                        {{ __('baseline-compare.evidence_gap_missing_details_body') }}
                    </p>
                </div>
            </div>
        @endif

        <div class="grid gap-4 lg:grid-cols-2 xl:grid-cols-3">
            @foreach ($buckets as $bucket)
                @php
                    $reasonLabel = is_string($bucket['reason_label'] ?? null) ? $bucket['reason_label'] : 'Evidence gap';
                    $reasonCode = is_string($bucket['reason_code'] ?? null) ? $bucket['reason_code'] : 'default';
                    $count = is_numeric($bucket['count'] ?? null) ? (int) $bucket['count'] : 0;
                    $recordedCount = is_numeric($bucket['recorded_count'] ?? null) ? (int) $bucket['recorded_count'] : 0;
                    $missingDetailCount = is_numeric($bucket['missing_detail_count'] ?? null) ? (int) $bucket['missing_detail_count'] : 0;
                    $bucketHelpKey = 'baseline-compare.evidence_gap_bucket_help_'.$reasonCode;
                    $bucketHelp = __($bucketHelpKey) !== $bucketHelpKey
                        ? __($bucketHelpKey)
                        : __('baseline-compare.evidence_gap_bucket_help_default');
                @endphp

                <section class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                    <div class="space-y-3">
                        <div class="space-y-1">
                            <h4 class="text-sm font-semibold text-gray-950 dark:text-white">
                                {{ $reasonLabel }}
                            </h4>
                            <p class="text-xs text-gray-500 dark:text-gray-400">
                                {{ $bucketHelp }}
                            </p>
                        </div>

                        <div class="flex flex-wrap gap-2">
                            <x-filament::badge color="warning" size="sm">
                                {{ __('baseline-compare.evidence_gap_reason_affected', ['count' => $count]) }}
                            </x-filament::badge>
                            <x-filament::badge color="primary" size="sm">
                                {{ __('baseline-compare.evidence_gap_reason_recorded', ['count' => $recordedCount]) }}
                            </x-filament::badge>
                            @if ($missingDetailCount > 0)
                                <x-filament::badge color="gray" size="sm">
                                    {{ __('baseline-compare.evidence_gap_reason_missing_detail', ['count' => $missingDetailCount]) }}
                                </x-filament::badge>
                            @endif
                            @if ((int) ($bucket['structural_count'] ?? 0) > 0)
                                <x-filament::badge color="danger" size="sm">
                                    {{ __('baseline-compare.evidence_gap_bucket_structural', ['count' => (int) $bucket['structural_count']]) }}
                                </x-filament::badge>
                            @endif
                            @if ((int) ($bucket['operational_count'] ?? 0) > 0)
                                <x-filament::badge color="primary" size="sm">
                                    {{ __('baseline-compare.evidence_gap_bucket_operational', ['count' => (int) $bucket['operational_count']]) }}
                                </x-filament::badge>
                            @endif
                            @if ((int) ($bucket['transient_count'] ?? 0) > 0)
                                <x-filament::badge color="warning" size="sm">
                                    {{ __('baseline-compare.evidence_gap_bucket_transient', ['count' => (int) $bucket['transient_count']]) }}
                                </x-filament::badge>
                            @endif
                        </div>

                        @if ($missingDetailCount > 0)
                            <div class="rounded-lg border border-gray-200 bg-gray-50 px-3 py-3 text-sm text-gray-700 dark:border-gray-800 dark:bg-gray-950/60 dark:text-gray-200">
                                {{ __('baseline-compare.evidence_gap_missing_reason_body', ['count' => $missingDetailCount]) }}
                            </div>
                        @endif
                    </div>
                </section>
            @endforeach
        </div>

        <div class="space-y-2">
            <div class="space-y-1">
                <h4 class="text-sm font-semibold text-gray-950 dark:text-white">
                    {{ __('baseline-compare.evidence_gap_search_label') }}
                </h4>
                <p class="text-xs text-gray-500 dark:text-gray-400">
                    {{ __('baseline-compare.evidence_gap_search_help') }}
                </p>
            </div>

            @livewire(
                \App\Livewire\BaselineCompareEvidenceGapTable::class,
                [
                    'buckets' => $buckets,
                    'context' => $tableContext,
                ],
                key('baseline-compare-evidence-gap-table-'.$tableContext)
            )
        </div>
    </div>
@endif
