@php
    $state = $getState();
    $state = is_array($state) ? $state : [];

    $connectionState = is_string($state['state'] ?? null) ? (string) $state['state'] : 'missing';
    $ctaUrl = is_string($state['cta_url'] ?? null) ? (string) $state['cta_url'] : '#';
    $needsDefaultConnection = (bool) ($state['needs_default_connection'] ?? false);

    $displayName = is_string($state['display_name'] ?? null) ? (string) $state['display_name'] : null;
    $provider = is_string($state['provider'] ?? null) ? (string) $state['provider'] : null;
    $lifecycle = is_string($state['lifecycle'] ?? null) ? (string) $state['lifecycle'] : null;
    $isEnabled = $state['is_enabled'] ?? null;
    $consentStatus = is_string($state['consent_status'] ?? null) ? (string) $state['consent_status'] : null;
    $verificationStatus = is_string($state['verification_status'] ?? null) ? (string) $state['verification_status'] : null;
    $lastCheck = is_string($state['last_health_check_at'] ?? null) ? (string) $state['last_health_check_at'] : null;
    $lastErrorReason = is_string($state['last_error_reason_code'] ?? null) ? (string) $state['last_error_reason_code'] : null;
    $targetScopeSummary = is_string($state['target_scope_summary'] ?? null) ? (string) $state['target_scope_summary'] : null;

    $isMissing = $connectionState === 'missing';
    $lifecycleSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::BooleanEnabled, $isEnabled ?? $lifecycle);
    $consentSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderConsentStatus, $consentStatus);
    $verificationSpec = \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderVerificationStatus, $verificationStatus);
@endphp

<div class="space-y-3 rounded-md border border-gray-200 bg-white p-4 shadow-sm">
    <div class="flex items-start justify-between gap-4">
        <div>
            <div class="text-sm font-semibold text-gray-800">Provider connection</div>
            @if ($isMissing)
                <div class="mt-1 text-sm text-amber-700">Needs action: no provider connection is configured.</div>
            @elseif ($needsDefaultConnection)
                <div class="mt-1 text-sm text-amber-700">Needs action: set a default provider connection.</div>
            @else
                <div class="mt-1 text-sm text-gray-700">{{ $displayName ?? 'Unnamed connection' }}</div>
            @endif
        </div>

        <a href="{{ $ctaUrl }}" class="inline-flex items-center rounded-md border border-gray-300 px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50">
            Open Provider Connections
        </a>
    </div>

    @unless ($isMissing)
        @if ($needsDefaultConnection && $displayName)
            <div class="text-sm text-gray-700">
                Current connection: <span class="font-medium">{{ $displayName }}</span>
            </div>
        @endif

        <dl class="grid grid-cols-1 gap-2 text-sm text-gray-700 sm:grid-cols-2">
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Provider</dt>
                <dd>{{ $provider ?? 'n/a' }}</dd>
            </div>
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Target scope</dt>
                <dd>{{ $targetScopeSummary ?? 'n/a' }}</dd>
            </div>
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Lifecycle</dt>
                <dd>
                    <x-filament::badge :color="$lifecycleSpec->color" :icon="$lifecycleSpec->icon" size="sm">
                        {{ $lifecycleSpec->label }}
                    </x-filament::badge>
                </dd>
            </div>
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Consent</dt>
                <dd>
                    <x-filament::badge :color="$consentSpec->color" :icon="$consentSpec->icon" size="sm">
                        {{ $consentSpec->label }}
                    </x-filament::badge>
                </dd>
            </div>
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Verification</dt>
                <dd>
                    <x-filament::badge :color="$verificationSpec->color" :icon="$verificationSpec->icon" size="sm">
                        {{ $verificationSpec->label }}
                    </x-filament::badge>
                </dd>
            </div>
            <div>
                <dt class="text-xs uppercase tracking-wide text-gray-500">Last check</dt>
                <dd>{{ $lastCheck ?? 'n/a' }}</dd>
            </div>
        </dl>

        <div class="space-y-2 rounded-md border border-gray-200 bg-gray-50 p-3 text-sm text-gray-700">
            <div class="text-xs font-semibold uppercase tracking-wide text-gray-500">Diagnostics</div>
            @if ($lastErrorReason)
                <div class="rounded-md border border-amber-300 bg-amber-50 p-2 text-xs text-amber-800">
                    Last error reason: {{ $lastErrorReason }}
                </div>
            @endif
        </div>
    @endunless
</div>
