@php
    use App\Support\Verification\VerificationLinkBehavior;

    $help = is_array($help ?? null) ? $help : [];
    $links = is_array($help['docs_links'] ?? null) ? $help['docs_links'] : [];
    $steps = is_array($help['troubleshooting_steps'] ?? null) ? $help['troubleshooting_steps'] : [];
    $linkBehavior = app(VerificationLinkBehavior::class);
    $headline = is_string($help['headline'] ?? null) && trim((string) ($help['headline'] ?? '')) !== ''
        ? (string) ($help['headline'])
        : 'Contextual help';
    $reasonLabel = is_string($help['reason_label'] ?? null) && trim((string) ($help['reason_label'] ?? '')) !== ''
        ? (string) $help['reason_label']
        : null;
    $showReasonLabel = $reasonLabel !== null && trim(mb_strtolower($reasonLabel)) !== trim(mb_strtolower($headline));
@endphp

@if ($help !== [])
    <div class="rounded-lg border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-gray-900/80" data-testid="contextual-help-block">
        <div class="space-y-3">
            <div class="flex flex-wrap items-center gap-2">
                <x-filament::badge color="info" size="sm">
                    Contextual help
                </x-filament::badge>

                @if ($showReasonLabel)
                    <x-filament::badge color="gray" size="sm">
                        {{ $reasonLabel }}
                    </x-filament::badge>
                @endif
            </div>

            <div class="space-y-1">
                <div class="text-sm font-semibold text-gray-950 dark:text-white">
                    {{ $headline }}
                </div>
                <div class="text-sm text-gray-700 dark:text-gray-200">
                    {{ (string) ($help['short_explanation'] ?? '') }}
                </div>
            </div>

            @if (is_string($help['safe_next_action'] ?? null) && trim((string) ($help['safe_next_action'] ?? '')) !== '')
                <x-filament::callout
                    color="info"
                    heading="Safe next action"
                    :description="(string) $help['safe_next_action']"
                />
            @endif

            @if ($steps !== [])
                <ul class="list-disc space-y-1 pl-5 text-sm text-gray-700 dark:text-gray-200">
                    @foreach ($steps as $step)
                        @if (is_string($step) && trim($step) !== '')
                            <li>{{ $step }}</li>
                        @endif
                    @endforeach
                </ul>
            @endif

            @if ($links !== [])
                <div class="flex flex-wrap gap-2">
                    @foreach ($links as $link)
                        @php
                            $linkLabel = is_string($link['label'] ?? null) && trim((string) ($link['label'] ?? '')) !== ''
                                ? (string) $link['label']
                                : 'Open';
                            $linkUrl = is_string($link['url'] ?? null) && trim((string) ($link['url'] ?? '')) !== ''
                                ? (string) $link['url']
                                : null;
                            $behavior = $linkUrl !== null
                                ? $linkBehavior->describe($linkLabel, $linkUrl)
                                : null;
                            $testId = 'contextual-help-link-'.\Illuminate\Support\Str::slug($linkLabel);
                        @endphp

                        @if ($linkUrl)
                            <x-filament::button
                                tag="a"
                                :href="$linkUrl"
                                size="sm"
                                color="primary"
                                :target="(bool) ($behavior['opens_in_new_tab'] ?? false) ? '_blank' : null"
                                :rel="(bool) ($behavior['opens_in_new_tab'] ?? false) ? 'noopener noreferrer' : null"
                                :data-testid="$testId"
                            >
                                {{ $linkLabel }}
                            </x-filament::button>
                        @endif
                    @endforeach
                </div>
            @endif
        </div>
    </div>
@endif