@php
    $diff = $getState() ?? [];
    $summary = $diff['summary'] ?? [];

    $added = is_array($diff['added'] ?? null) ? $diff['added'] : [];
    $removed = is_array($diff['removed'] ?? null) ? $diff['removed'] : [];
    $baseline = is_array($diff['baseline'] ?? null) ? $diff['baseline'] : [];
    $current = is_array($diff['current'] ?? null) ? $diff['current'] : [];
@endphp

<div class="space-y-4">
    <x-filament::section
        heading="Scope tags diff"
        :description="$summary['message'] ?? sprintf('%d added, %d removed', $summary['added'] ?? 0, $summary['removed'] ?? 0)"
    >
        <div class="flex flex-wrap gap-2">
            <x-filament::badge color="success">
                {{ (int) ($summary['added'] ?? 0) }} added
            </x-filament::badge>
            <x-filament::badge color="danger">
                {{ (int) ($summary['removed'] ?? 0) }} removed
            </x-filament::badge>
            <x-filament::badge color="gray">
                Baseline: {{ (int) ($summary['baseline_count'] ?? 0) }}
            </x-filament::badge>
            <x-filament::badge color="gray">
                Current: {{ (int) ($summary['current_count'] ?? 0) }}
            </x-filament::badge>
        </div>
    </x-filament::section>

    @if ($added !== [])
        <x-filament::section heading="Added" collapsible :collapsed="true">
            <div class="space-y-2">
                @foreach ($added as $row)
                    @php
                        $name = (string) ($row['name'] ?? 'Unknown');
                        $id = (string) ($row['id'] ?? '');
                    @endphp

                    <div class="rounded-lg border border-gray-200/70 bg-white p-3 text-sm dark:border-gray-700 dark:bg-gray-900">
                        <div class="font-medium">{{ $name }}</div>
                        @if ($id !== '')
                            <div class="mt-1 text-gray-600 dark:text-gray-300">{{ $id }}</div>
                        @endif
                    </div>
                @endforeach
            </div>
        </x-filament::section>
    @endif

    @if ($removed !== [])
        <x-filament::section heading="Removed" collapsible :collapsed="true">
            <div class="space-y-2">
                @foreach ($removed as $row)
                    @php
                        $name = (string) ($row['name'] ?? 'Unknown');
                        $id = (string) ($row['id'] ?? '');
                    @endphp

                    <div class="rounded-lg border border-gray-200/70 bg-white p-3 text-sm dark:border-gray-700 dark:bg-gray-900">
                        <div class="font-medium">{{ $name }}</div>
                        @if ($id !== '')
                            <div class="mt-1 text-gray-600 dark:text-gray-300">{{ $id }}</div>
                        @endif
                    </div>
                @endforeach
            </div>
        </x-filament::section>
    @endif

    @if ($current !== [])
        <x-filament::section heading="Current" collapsible :collapsed="true">
            <div class="space-y-2">
                @foreach ($current as $row)
                    @php
                        $name = (string) ($row['name'] ?? 'Unknown');
                        $id = (string) ($row['id'] ?? '');
                    @endphp

                    <div class="rounded-lg border border-gray-200/70 bg-white p-3 text-sm dark:border-gray-700 dark:bg-gray-900">
                        <div class="font-medium">{{ $name }}</div>
                        @if ($id !== '')
                            <div class="mt-1 text-gray-600 dark:text-gray-300">{{ $id }}</div>
                        @endif
                    </div>
                @endforeach
            </div>
        </x-filament::section>
    @endif

    @if ($baseline !== [])
        <x-filament::section heading="Baseline" collapsible :collapsed="true">
            <div class="space-y-2">
                @foreach ($baseline as $row)
                    @php
                        $name = (string) ($row['name'] ?? 'Unknown');
                        $id = (string) ($row['id'] ?? '');
                    @endphp

                    <div class="rounded-lg border border-gray-200/70 bg-white p-3 text-sm dark:border-gray-700 dark:bg-gray-900">
                        <div class="font-medium">{{ $name }}</div>
                        @if ($id !== '')
                            <div class="mt-1 text-gray-600 dark:text-gray-300">{{ $id }}</div>
                        @endif
                    </div>
                @endforeach
            </div>
        </x-filament::section>
    @endif
</div>
