@php
    $normalized = $getState() ?? [];
    $warnings = $normalized['warnings'] ?? [];
    $settings = $normalized['settings'] ?? [];
    $settingsTable = $normalized['settings_table'] ?? null;
    $settingsTableRows = is_array($settingsTable) ? ($settingsTable['rows'] ?? []) : [];
    $context = $normalized['context'] ?? 'policy';
    $recordId = $normalized['record_id'] ?? null;
@endphp

<div class="space-y-3">
    @if (! empty($warnings))
        <div class="rounded-md border border-amber-300 bg-amber-50 p-3 text-sm text-amber-800">
            <div class="font-semibold">Warnings</div>
            <ul class="mt-1 list-disc space-y-1 pl-5">
                @foreach ($warnings as $warning)
                    <li>{{ $warning }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    @if (empty($settings) && empty($settingsTableRows))
        <p class="text-sm text-gray-600">No settings available.</p>
    @endif

    @if (! empty($settingsTableRows))
        @php
            $settingsTableTitle = is_array($settingsTable) ? ($settingsTable['title'] ?? null) : null;
            $shouldShowTitle = is_string($settingsTableTitle)
                && $settingsTableTitle !== ''
                && ! ($context === 'policy' && strtolower($settingsTableTitle) === 'settings');
        @endphp

        <div class="space-y-2">
            @if ($shouldShowTitle)
                <div class="text-sm font-semibold text-gray-800">{{ $settingsTableTitle }}</div>
            @endif

            <livewire:settings-catalog-settings-table
                :settings-rows="$settingsTableRows"
                :context="$context"
                :key="$recordId ? ('sc-settings-'.$context.'-'.$recordId) : ('sc-settings-'.$context)"
            />
        </div>
    @endif

    @foreach ($settings as $block)
        @php
            $title = $block['title'] ?? 'Settings';
            $isGeneral = is_string($title) && strtolower($title) === 'general';
        @endphp

        @if ($isGeneral)
            <x-filament::section
                :heading="$title"
                collapsible
                :collapsed="true"
                data-block="general"
            >
                <x-slot name="headerEnd">
                    <span class="text-sm text-gray-500 dark:text-gray-400">
                        {{ count($block['entries'] ?? []) }} fields
                    </span>
                </x-slot>

                @if (($block['type'] ?? 'keyValue') === 'table')
                    @php
                        $columns = $block['columns'] ?? null;
                        $hasColumns = is_array($columns) && ! empty($columns);
                        $columnMeta = [
                            'definitionId' => ['width' => 'w-[35%]', 'style' => 'width: 35%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                            'instanceType' => ['width' => 'w-[20%]', 'style' => 'width: 20%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                            'value' => ['width' => 'w-[25%]', 'style' => 'width: 25%;', 'cell' => 'break-words whitespace-pre-wrap', 'cellStyle' => 'overflow-wrap: anywhere; white-space: pre-wrap;'],
                            'path' => ['width' => 'w-[20%]', 'style' => 'width: 20%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                        ];
                    @endphp
                    <div class="overflow-x-auto rounded-lg border border-gray-200" style="overflow-x: auto;">
                        <table class="min-w-[900px] w-full table-fixed text-left text-sm" style="table-layout: fixed; width: 100%; min-width: 900px;">
                            <thead class="bg-gray-50 text-gray-700">
                                <tr>
                                    @if ($hasColumns)
                                        @foreach ($columns as $column)
                                            @php
                                                $key = $column['key'] ?? null;
                                                $meta = is_string($key) ? ($columnMeta[$key] ?? []) : [];
                                            @endphp
                                            <th class="px-3 py-2 {{ $meta['width'] ?? '' }}" style="{{ $meta['style'] ?? '' }}">{{ $column['label'] ?? $column['key'] ?? '-' }}</th>
                                        @endforeach
                                    @else
                                        <th class="px-3 py-2">Path</th>
                                        <th class="px-3 py-2">Value</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100">
                                @foreach ($block['rows'] ?? [] as $row)
                                    <tr>
                                        @if ($hasColumns)
                                            @foreach ($columns as $column)
                                                @php
                                                    $key = $column['key'] ?? null;
                                                    $cell = is_string($key) ? ($row[$key] ?? null) : null;
                                                    $meta = is_string($key) ? ($columnMeta[$key] ?? []) : [];
                                                @endphp
                                                <td class="px-3 py-2 align-top text-gray-800 {{ $meta['cell'] ?? 'whitespace-pre-wrap' }}" style="{{ $meta['cellStyle'] ?? '' }}">
                                                    @if (is_array($cell))
                                                        <pre class="overflow-x-auto text-xs">{{ json_encode($cell, JSON_PRETTY_PRINT) }}</pre>
                                                    @elseif (is_bool($cell))
                                                        <span>{{ $cell ? 'true' : 'false' }}</span>
                                                    @else
                                                        <span title="{{ is_string($cell) ? $cell : '' }}">{{ $cell ?? '-' }}</span>
                                                    @endif
                                                </td>
                                            @endforeach
                                        @else
                                            <td class="px-3 py-2 align-top">
                                                <div class="font-mono text-xs font-medium text-gray-800 break-all whitespace-normal" style="word-break: break-all; overflow-wrap: anywhere; white-space: normal;">{{ $row['path'] ?? '-' }}</div>
                                                @if (! empty($row['label']))
                                                    <div class="text-xs text-gray-600">{{ $row['label'] }}</div>
                                                @endif
                                            </td>
                                            <td class="px-3 py-2 align-top text-gray-800 break-words whitespace-pre-wrap" style="overflow-wrap: anywhere; white-space: pre-wrap;">
                                                {{ is_array($row['value'] ?? null) ? json_encode($row['value'], JSON_PRETTY_PRINT) : ($row['value'] ?? '-') }}
                                            </td>
                                        @endif
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="divide-y divide-gray-200 dark:divide-gray-700">
                        @foreach ($block['entries'] ?? [] as $entry)
                            <div class="py-3 sm:grid sm:grid-cols-3 sm:gap-4">
                                <dt class="text-sm font-medium text-gray-500 dark:text-gray-400">
                                    {{ $entry['key'] ?? '-' }}
                                </dt>
                                <dd class="mt-1 sm:mt-0 sm:col-span-2">
                                    <span class="text-sm text-gray-900 dark:text-white whitespace-pre-wrap break-words">
                                        {{ is_array($entry['value'] ?? null) ? json_encode($entry['value'], JSON_PRETTY_PRINT) : ($entry['value'] ?? '-') }}
                                    </span>
                                </dd>
                            </div>
                        @endforeach
                    </div>
                @endif
            </x-filament::section>
        @else
            <div class="space-y-2 rounded-md border border-gray-200 bg-white p-3 shadow-sm">
                <div class="text-sm font-semibold text-gray-800">{{ $title }}</div>

                @if (($block['type'] ?? 'keyValue') === 'table')
                    @php
                        $columns = $block['columns'] ?? null;
                        $hasColumns = is_array($columns) && ! empty($columns);
                        $columnMeta = [
                            'definitionId' => ['width' => 'w-[35%]', 'style' => 'width: 35%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                            'instanceType' => ['width' => 'w-[20%]', 'style' => 'width: 20%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                            'value' => ['width' => 'w-[25%]', 'style' => 'width: 25%;', 'cell' => 'break-words whitespace-pre-wrap', 'cellStyle' => 'overflow-wrap: anywhere; white-space: pre-wrap;'],
                            'path' => ['width' => 'w-[20%]', 'style' => 'width: 20%;', 'cell' => 'font-mono text-xs break-all whitespace-normal', 'cellStyle' => 'word-break: break-all; overflow-wrap: anywhere; white-space: normal;'],
                        ];
                    @endphp
                    <div class="overflow-x-auto rounded-lg border border-gray-200" style="overflow-x: auto;">
                        <table class="min-w-[900px] w-full table-fixed text-left text-sm" style="table-layout: fixed; width: 100%; min-width: 900px;">
                            <thead class="bg-gray-50 text-gray-700">
                                <tr>
                                    @if ($hasColumns)
                                        @foreach ($columns as $column)
                                            @php
                                                $key = $column['key'] ?? null;
                                                $meta = is_string($key) ? ($columnMeta[$key] ?? []) : [];
                                            @endphp
                                            <th class="px-3 py-2 {{ $meta['width'] ?? '' }}" style="{{ $meta['style'] ?? '' }}">{{ $column['label'] ?? $column['key'] ?? '-' }}</th>
                                        @endforeach
                                    @else
                                        <th class="px-3 py-2">Path</th>
                                        <th class="px-3 py-2">Value</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100">
                                @foreach ($block['rows'] ?? [] as $row)
                                    <tr>
                                        @if ($hasColumns)
                                            @foreach ($columns as $column)
                                                @php
                                                    $key = $column['key'] ?? null;
                                                    $cell = is_string($key) ? ($row[$key] ?? null) : null;
                                                    $meta = is_string($key) ? ($columnMeta[$key] ?? []) : [];
                                                @endphp
                                                <td class="px-3 py-2 align-top text-gray-800 {{ $meta['cell'] ?? 'whitespace-pre-wrap' }}" style="{{ $meta['cellStyle'] ?? '' }}">
                                                    @if (is_array($cell))
                                                        <pre class="overflow-x-auto text-xs">{{ json_encode($cell, JSON_PRETTY_PRINT) }}</pre>
                                                    @elseif (is_bool($cell))
                                                        <span>{{ $cell ? 'true' : 'false' }}</span>
                                                    @else
                                                        <span title="{{ is_string($cell) ? $cell : '' }}">{{ $cell ?? '-' }}</span>
                                                    @endif
                                                </td>
                                            @endforeach
                                        @else
                                            <td class="px-3 py-2 align-top">
                                                <div class="font-mono text-xs font-medium text-gray-800 break-all whitespace-normal" style="word-break: break-all; overflow-wrap: anywhere; white-space: normal;">{{ $row['path'] ?? '-' }}</div>
                                                @if (! empty($row['label']))
                                                    <div class="text-xs text-gray-600">{{ $row['label'] }}</div>
                                                @endif
                                            </td>
                                            <td class="px-3 py-2 align-top text-gray-800 break-words whitespace-pre-wrap" style="overflow-wrap: anywhere; white-space: pre-wrap;">
                                                {{ is_array($row['value'] ?? null) ? json_encode($row['value'], JSON_PRETTY_PRINT) : ($row['value'] ?? '-') }}
                                            </td>
                                        @endif
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <dl class="grid grid-cols-1 gap-3 sm:grid-cols-2">
                        @foreach ($block['entries'] ?? [] as $entry)
                            <div class="rounded border border-gray-100 bg-gray-50 p-3">
                                <dt class="text-xs uppercase tracking-wide text-gray-500">{{ $entry['key'] ?? '-' }}</dt>
                                <dd class="whitespace-pre-wrap text-sm text-gray-800">
                                    {{ is_array($entry['value'] ?? null) ? json_encode($entry['value'], JSON_PRETTY_PRINT) : ($entry['value'] ?? '-') }}
                                </dd>
                            </div>
                        @endforeach
                    </dl>
                @endif
            </div>
        @endif
    @endforeach
</div>
