@php
    use Illuminate\Support\Str;

    /** @var array<string, mixed> $bundle */
    $summary = is_array($bundle['summary'] ?? null) ? $bundle['summary'] : [];
    $context = is_array($bundle['context'] ?? null) ? $bundle['context'] : [];
    $sections = is_array($bundle['sections'] ?? null) ? $bundle['sections'] : [];
    $redaction = is_array($bundle['redaction'] ?? null) ? $bundle['redaction'] : [];
    $notes = is_array($bundle['notes'] ?? null) ? $bundle['notes'] : [];

    $availabilityColor = static function (?string $availability): string {
        return match ($availability) {
            'available', 'current', 'fresh', 'ready' => 'success',
            'partial', 'stale' => 'warning',
            'error', 'missing', 'unavailable' => 'danger',
            default => 'gray',
        };
    };

    $referenceDescription = static function (array $reference): string {
        $parts = [
            is_string($reference['type'] ?? null) && trim((string) $reference['type']) !== ''
                ? (string) $reference['type']
                : 'reference',
            is_string($reference['availability'] ?? null) && trim((string) $reference['availability']) !== ''
                ? (string) $reference['availability']
                : 'missing',
        ];

        if (is_string($reference['freshness_note'] ?? null) && trim((string) $reference['freshness_note']) !== '') {
            $parts[] = (string) $reference['freshness_note'];
        }

        return implode(' - ', $parts);
    };
@endphp

<div class="space-y-4">
    <x-filament::section
        :heading="data_get($summary, 'headline', data_get($bundle, 'headline', 'Support diagnostics'))"
        :description="data_get($summary, 'dominant_issue', data_get($bundle, 'dominant_issue', 'No dominant issue available.'))"
    >
        <x-slot name="afterHeader">
            <div class="flex flex-wrap gap-2">
                <x-filament::badge color="gray" size="sm">
                    {{ data_get($context, 'type', data_get($bundle, 'context_type', 'tenant')) }}
                </x-filament::badge>
                <x-filament::badge color="gray" size="sm">
                    {{ data_get($summary, 'freshness_state', data_get($bundle, 'freshness_state', 'mixed')) }}
                </x-filament::badge>
                <x-filament::badge color="warning" size="sm">
                    {{ str_replace('_', '-', (string) data_get($redaction, 'mode', data_get($bundle, 'redaction_mode', 'default-redacted'))) }}
                </x-filament::badge>
            </div>
        </x-slot>

        <dl class="grid grid-cols-1 gap-3 text-sm sm:grid-cols-2">
            <div class="space-y-1">
                <dt class="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">Workspace</dt>
                <dd class="text-gray-950 dark:text-white">{{ data_get($context, 'workspace_label', 'Workspace unavailable') }}</dd>
            </div>
            <div class="space-y-1">
                <dt class="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">ManagedEnvironment</dt>
                <dd class="text-gray-950 dark:text-white">{{ data_get($context, 'tenant_label', 'ManagedEnvironment unavailable') }}</dd>
            </div>
        </dl>
    </x-filament::section>

    @if (is_array($bundle['contextual_help'] ?? null))
        @include('filament.components.product-knowledge.contextual-help', ['help' => $bundle['contextual_help']])
    @endif

    @if ($notes !== [])
        <x-filament::section
            heading="Support notes"
            description="The bundle stays read-only and redacted even when the source records include provider-only details."
            compact
        >
            <div class="space-y-2">
                @foreach ($notes as $note)
                    @if (is_string($note) && trim($note) !== '')
                        <div class="flex items-start gap-2">
                            <x-filament::badge color="warning" size="sm">Note</x-filament::badge>
                            <p class="text-sm leading-6 text-gray-700 dark:text-gray-200">{{ $note }}</p>
                        </div>
                    @endif
                @endforeach
            </div>
        </x-filament::section>
    @endif

    <div class="space-y-3">
        @foreach ($sections as $section)
            @php
                $references = is_array($section['references'] ?? null) ? $section['references'] : [];
                $markers = is_array($section['redaction_markers'] ?? null) ? $section['redaction_markers'] : [];
                $availability = is_string($section['availability'] ?? null) && trim((string) $section['availability']) !== ''
                    ? (string) $section['availability']
                    : 'missing';
                $sectionLabel = $section['label'] ?? $section['key'] ?? 'Section';
                $sectionSummary = $section['summary'] ?? 'No summary available.';
            @endphp

            <x-filament::section
                :heading="$sectionLabel"
                :description="$sectionSummary"
                compact
            >
                <x-slot name="afterHeader">
                    <x-filament::badge :color="$availabilityColor($availability)" size="sm">
                        {{ $availability }}
                    </x-filament::badge>
                </x-slot>

                <div class="space-y-3">
                    @if (is_string($section['freshness_note'] ?? null) && trim((string) $section['freshness_note']) !== '')
                        <p class="text-xs text-gray-500 dark:text-gray-400">{{ $section['freshness_note'] }}</p>
                    @endif

                    @if ($references !== [])
                        <div class="space-y-2">
                            @foreach ($references as $reference)
                                @php
                                    $referenceLabel = $reference['label'] ?? 'Reference unavailable';
                                    $referenceUrl = is_string($reference['url'] ?? null) && trim((string) $reference['url']) !== ''
                                        ? (string) $reference['url']
                                        : null;
                                    $referenceActionLabel = $reference['action_label'] ?? ($referenceUrl ? 'Open' : 'Unavailable');
                                @endphp

                                <x-filament::section
                                    :heading="$referenceLabel"
                                    :description="$referenceDescription($reference)"
                                    compact
                                    secondary
                                >
                                    <x-slot name="afterHeader">
                                        @if ($referenceUrl)
                                            <x-filament::link :href="$referenceUrl" size="sm">
                                                {{ $referenceActionLabel }}
                                            </x-filament::link>
                                        @else
                                            <x-filament::badge color="gray" size="sm">
                                                {{ $referenceActionLabel }}
                                            </x-filament::badge>
                                        @endif
                                    </x-slot>
                                </x-filament::section>
                            @endforeach
                        </div>
                    @endif

                    @if ($markers !== [])
                        <div class="flex flex-wrap gap-2">
                            @foreach ($markers as $marker)
                                <x-filament::badge color="warning" size="sm">
                                    {{ trim((string) (($marker['replacement_text'] ?? '[REDACTED]').' '.Str::of((string) ($marker['reason'] ?? 'redacted'))->replace('_', ' '))) }}
                                </x-filament::badge>
                            @endforeach
                        </div>
                    @endif
                </div>
            </x-filament::section>
        @endforeach
    </div>
</div>

