@php
    $requestRecord = $bloodBankRequest;
    $payload = $requestRecord?->payload ?? [];
    $issuePayload = $bloodBankIssue?->payload ?? [];
    $inventoryPayload = $bloodBankInventory?->payload ?? [];
    $urgency = (string) ($payload['urgency'] ?? '');
    $isHighUrgency = str_contains(strtolower($urgency), 'urgent')
        || str_contains(strtolower($urgency), 'emergency')
        || str_contains(strtolower($urgency), 'massive');
@endphp

<section class="rounded-2xl border border-slate-200 bg-white p-5">
    <div class="flex flex-wrap items-start justify-between gap-3">
        <div>
            <h3 class="text-sm font-semibold text-slate-900">Blood Bank overview</h3>
            <p class="mt-0.5 text-xs text-slate-500">Open request, urgency, cross-match status, and issue summary.</p>
        </div>
        <div class="flex flex-wrap gap-3 text-sm">
            <a href="{{ route('care.specialty.blood-bank.print', $workspaceVisit) }}" target="_blank" class="font-medium text-indigo-600">Print summary</a>
            <a href="{{ route('care.specialty.blood-bank.reports') }}" class="font-medium text-indigo-600">BB reports</a>
        </div>
    </div>

    <div class="mt-4 grid gap-3 sm:grid-cols-3">
        <div class="rounded-xl border border-slate-100 bg-slate-50 px-3 py-3">
            <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Request</p>
            <p class="mt-1 text-sm font-semibold text-slate-900">{{ $payload['product'] ?? 'No request' }}</p>
            <p class="mt-1 text-xs text-slate-500">
                {{ isset($payload['units']) ? $payload['units'].' unit(s)' : '—' }}
                · {{ $payload['patient_blood_group'] ?? 'Group —' }}
            </p>
        </div>
        <div class="rounded-xl border border-slate-100 bg-slate-50 px-3 py-3">
            <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Urgency / cross-match</p>
            <p @class([
                'mt-1 text-sm font-semibold',
                'text-rose-700' => $isHighUrgency,
                'text-slate-900' => ! $isHighUrgency,
            ])>
                {{ $urgency !== '' ? $urgency : 'Not set' }}
            </p>
            <p class="mt-1 text-xs text-slate-500">{{ $payload['crossmatch_status'] ?? 'Cross-match not started' }}</p>
        </div>
        <div class="rounded-xl border border-slate-100 bg-slate-50 px-3 py-3">
            <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Issued</p>
            <p class="mt-1 text-sm font-semibold text-slate-900">
                {{ $issuePayload['units_issued'] ?? $payload['issued_units'] ?? '0' }} unit(s)
            </p>
            <p class="mt-1 text-xs text-slate-500">
                {{ $issuePayload['product'] ?? ($payload['product'] ?? '—') }}
            </p>
        </div>
    </div>

    <dl class="mt-4 grid gap-3 text-sm sm:grid-cols-2">
        <div>
            <dt class="text-slate-500">Indication</dt>
            <dd class="font-medium text-slate-900">{{ $payload['indication'] ?? '—' }}</dd>
        </div>
        <div>
            <dt class="text-slate-500">Inventory flag</dt>
            <dd class="font-medium text-slate-900">
                @if (! empty($inventoryPayload['low_stock_alert']))
                    <span class="text-amber-800">Low stock flagged</span>
                @else
                    {{ isset($inventoryPayload['packed_rbc_units']) ? 'Stock noted' : '—' }}
                @endif
            </dd>
        </div>
        <div>
            <dt class="text-slate-500">Queue</dt>
            <dd class="font-medium text-slate-900">
                {{ $workspaceVisit->appointment?->queue_ticket_number ?? '—' }}
                @if ($workspaceVisit->appointment?->queue_ticket_status)
                    <span class="text-slate-500">({{ $workspaceVisit->appointment->queue_ticket_status }})</span>
                @endif
            </dd>
        </div>
        <div>
            <dt class="text-slate-500">Checked in</dt>
            <dd class="font-medium text-slate-900">{{ $workspaceVisit->checked_in_at?->format('d M Y H:i') ?? '—' }}</dd>
        </div>
    </dl>

    @if (! empty($clinicalAlerts))
        <div class="mt-4 space-y-1.5 border-t border-slate-100 pt-4">
            <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Alerts</p>
            @foreach ($clinicalAlerts as $alert)
                <p class="text-sm {{ ($alert['severity'] ?? '') === 'critical' ? 'text-rose-700' : 'text-amber-800' }}">
                    {{ $alert['message'] ?? '' }}
                </p>
            @endforeach
        </div>
    @endif
</section>
