@php
    use App\Support\Badges\BadgeCatalog;
    use App\Support\Badges\BadgeDomain;
    use App\Support\Badges\TagBadgeCatalog;
    use App\Support\Badges\TagBadgeDomain;

    $rows = array_values(array_filter($rows ?? [], 'is_array'));
    $summary = is_array($summary ?? null) ? $summary : [];
    $followUpRows = array_values(array_filter($rows, static fn (array $row): bool => (bool) ($row['followUpRequired'] ?? false)));
    $topFollowUp = $followUpRows[0] ?? null;
@endphp

<div class="space-y-4">
    <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
        <div class="rounded-2xl border border-gray-200 bg-white px-4 py-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
            <div class="text-xs font-medium uppercase tracking-[0.18em] text-gray-500 dark:text-gray-400">
                Types in run
            </div>

            <div class="mt-2 text-2xl font-semibold text-gray-950 dark:text-white">
                {{ (int) ($summary['totalTypes'] ?? count($rows)) }}
            </div>

            <div class="mt-2 text-sm text-gray-600 dark:text-gray-300">
                Succeeded: {{ (int) ($summary['succeededTypes'] ?? 0) }}. Failed: {{ (int) ($summary['failedTypes'] ?? 0) }}. Skipped: {{ (int) ($summary['skippedTypes'] ?? 0) }}.
            </div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white px-4 py-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
            <div class="text-xs font-medium uppercase tracking-[0.18em] text-gray-500 dark:text-gray-400">
                Need follow-up
            </div>

            <div class="mt-2 text-2xl font-semibold text-gray-950 dark:text-white">
                {{ (int) ($summary['followUpTypes'] ?? count($followUpRows)) }}
            </div>

            <div class="mt-2 text-sm text-gray-600 dark:text-gray-300">
                Execution outcome stays separate from the per-type results below.
            </div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white px-4 py-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
            <div class="text-xs font-medium uppercase tracking-[0.18em] text-gray-500 dark:text-gray-400">
                Observed items
            </div>

            <div class="mt-2 text-2xl font-semibold text-gray-950 dark:text-white">
                {{ (int) ($summary['observedItems'] ?? 0) }}
            </div>

            <div class="mt-2 text-sm text-gray-600 dark:text-gray-300">
                Item counts show what this run observed for the listed types.
            </div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white px-4 py-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
            <div class="text-xs font-medium uppercase tracking-[0.18em] text-gray-500 dark:text-gray-400">
                Run outcome
            </div>

            <div class="mt-2">
                <x-filament::badge :color="$runOutcomeColor ?? 'gray'" :icon="$runOutcomeIcon ?? null">
                    {{ $runOutcomeLabel ?? 'Unknown' }}
                </x-filament::badge>
            </div>

            <div class="mt-2 text-sm text-gray-600 dark:text-gray-300">
                Coverage truth below explains which types created the follow-up.
            </div>
        </div>
    </div>

    @if ($topFollowUp !== null)
        <div class="rounded-2xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-900/60 dark:bg-amber-950/30 dark:text-amber-100">
            Highest-priority follow-up: {{ $topFollowUp['label'] ?? ($topFollowUp['type'] ?? 'Unknown type') }}. {{ $topFollowUp['followUpGuidance'] ?? 'Review the latest inventory sync details before retrying.' }}
        </div>
    @endif

    <div class="space-y-3">
        @foreach ($rows as $row)
            @php
                $typeSpec = TagBadgeCatalog::spec(TagBadgeDomain::PolicyType, $row['type'] ?? null);
                $categorySpec = TagBadgeCatalog::spec(TagBadgeDomain::PolicyCategory, $row['category'] ?? null);
                $stateSpec = BadgeCatalog::spec(BadgeDomain::InventoryCoverageState, $row['coverageState'] ?? null);
            @endphp

            <div class="rounded-2xl border border-gray-200 bg-white px-4 py-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/60">
                <div class="flex flex-wrap items-start justify-between gap-3">
                    <div class="min-w-0 space-y-2">
                        <div class="flex flex-wrap items-center gap-2">
                            <x-filament::badge :color="$typeSpec->color" :icon="$typeSpec->icon">
                                {{ $typeSpec->label }}
                            </x-filament::badge>

                            <x-filament::badge :color="$stateSpec->color" :icon="$stateSpec->icon">
                                {{ $stateSpec->label }}
                            </x-filament::badge>

                            <x-filament::badge :color="$categorySpec->color" :icon="$categorySpec->icon">
                                {{ $categorySpec->label }}
                            </x-filament::badge>

                            <x-filament::badge color="{{ ($row['segment'] ?? 'policy') === 'foundation' ? 'gray' : 'info' }}">
                                {{ ($row['segment'] ?? 'policy') === 'foundation' ? 'Foundation' : 'Policy' }}
                            </x-filament::badge>
                        </div>

                        <div class="text-sm font-medium text-gray-900 dark:text-white">
                            {{ $row['label'] ?? ($row['type'] ?? 'Unknown type') }}
                        </div>

                        <div class="text-xs text-gray-500 dark:text-gray-400">
                            {{ $row['type'] ?? 'unknown' }}
                        </div>
                    </div>

                    <div class="rounded-xl bg-gray-50 px-3 py-2 text-right dark:bg-gray-950/60">
                        <div class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                            Observed items
                        </div>
                        <div class="mt-1 text-lg font-semibold text-gray-950 dark:text-white">
                            {{ (int) ($row['itemCount'] ?? 0) }}
                        </div>
                    </div>
                </div>

                <div class="mt-3 text-sm text-gray-700 dark:text-gray-200">
                    {{ $row['followUpGuidance'] ?? 'No follow-up is currently required.' }}
                </div>

                @if (filled($row['errorCode'] ?? null))
                    <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                        Reason code: {{ $row['errorCode'] }}
                    </div>
                @endif
            </div>
        @endforeach
    </div>
</div>
