@php
    use App\Support\Badges\BadgeDomain;
    use App\Support\Badges\BadgeRenderer;
    use Illuminate\Support\Carbon;

    $rows = isset($rows) ? $rows : (isset($getState) ? $getState() : []);
    $rows = is_array($rows) ? $rows : [];

    $formatTimestamp = static function (?string $value): string {
        if (! is_string($value) || trim($value) === '') {
            return '—';
        }

        try {
            return Carbon::parse($value)->toDayDateTimeString();
        } catch (\Throwable) {
            return $value;
        }
    };
@endphp

<div class="space-y-3">
    @if ($rows === [])
        <div class="rounded-lg border border-dashed border-gray-300 px-4 py-6 text-sm text-gray-600 dark:border-gray-700 dark:text-gray-300">
            No captured governed subjects are available in this snapshot.
        </div>
    @else
        <div class="overflow-x-auto rounded-lg border border-gray-200 dark:border-gray-700">
            <table class="min-w-full divide-y divide-gray-200 text-left text-sm dark:divide-gray-700">
                <thead class="bg-gray-50 dark:bg-gray-900/50">
                    <tr class="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
                        <th class="px-4 py-3">Governed subject</th>
                        <th class="px-4 py-3">Items</th>
                        <th class="px-4 py-3">Fidelity</th>
                        <th class="px-4 py-3">Coverage state</th>
                        <th class="px-4 py-3">Latest evidence</th>
                        <th class="px-4 py-3">Notes</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-gray-800">
                    @foreach ($rows as $row)
                        @php
                            $fidelitySpec = BadgeRenderer::spec(BadgeDomain::BaselineSnapshotFidelity, $row['fidelity'] ?? null);
                            $gapState = (($row['gapCount'] ?? 0) > 0) ? 'gaps_present' : 'clear';
                            $gapSpec = BadgeRenderer::spec(BadgeDomain::BaselineSnapshotGapStatus, $gapState);
                        @endphp

                        <tr class="align-top">
                            <td class="px-4 py-3 font-medium text-gray-900 dark:text-white">
                                {{ $row['governedSubjectLabel'] ?? ($row['label'] ?? ($row['policyType'] ?? 'Governed subject')) }}
                            </td>
                            <td class="px-4 py-3 text-gray-700 dark:text-gray-200">
                                {{ (int) ($row['itemCount'] ?? 0) }}
                            </td>
                            <td class="px-4 py-3">
                                <x-filament::badge :color="$fidelitySpec->color" :icon="$fidelitySpec->icon" size="sm">
                                    {{ $fidelitySpec->label }}
                                </x-filament::badge>
                            </td>
                            <td class="px-4 py-3">
                                <div class="flex items-center gap-2">
                                    <x-filament::badge :color="$gapSpec->color" :icon="$gapSpec->icon" size="sm">
                                        {{ $gapSpec->label }}
                                    </x-filament::badge>
                                    <span class="text-xs text-gray-500 dark:text-gray-400">
                                        {{ (int) ($row['gapCount'] ?? 0) }}
                                    </span>
                                </div>
                            </td>
                            <td class="px-4 py-3 text-gray-700 dark:text-gray-200">
                                {{ $formatTimestamp($row['capturedAt'] ?? null) }}
                            </td>
                            <td class="px-4 py-3 text-gray-600 dark:text-gray-300">
                                {{ $row['coverageHint'] ?? '—' }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @endif
</div>
