@php
    use Illuminate\Support\Carbon;
@endphp

<div class="card border-0 shadow-sm h-100">
    <div class="card-header bg-white fw-semibold" style="color: {{ config('theme.colors.text') }};">
        {{ __('operational_ui.workflow_inbox.section_procurement') }}
    </div>
    <div class="card-body small">
        @if ($procurementInbox === null)
            <p class="text-muted mb-0">{{ __('operational_ui.workflow_inbox.empty_procurement') }}</p>
        @else
            <p class="text-muted mb-2">{{ __('operational_ui.workflow_inbox.escalation_note') }}</p>

            <div class="fw-semibold mb-1">{{ __('operational_ui.workflow_inbox.pending_po') }}</div>
            <ul class="list-unstyled mb-3">
                @forelse ($procurementInbox['pendingPos'] as $po)
                    @php
                        $hours = $po->updated_at ? Carbon::parse($po->updated_at)->diffInHours(Carbon::now()) : 0;
                        $priority = $hours >= 48 ? 'danger' : ($hours >= 24 ? 'warning' : 'muted');
                    @endphp
                    <li class="mb-2 d-flex flex-wrap justify-content-between gap-2 align-items-center">
                        <a href="{{ route('procurement.purchase-orders.show', $po) }}">{{ $po->po_number }}</a>
                        <span class="d-flex align-items-center gap-2">
                            <x-ui.operational.status-badge
                                :label="__('operational_ui.workflow_inbox.aging_label').': '.$hours.'h'"
                                :variant="$priority"
                            />
                        </span>
                    </li>
                @empty
                    <li class="text-muted">—</li>
                @endforelse
            </ul>

            <div class="fw-semibold mb-1">{{ __('operational_ui.workflow_inbox.stale_validated') }}</div>
            <ul class="list-unstyled mb-3">
                @forelse ($procurementInbox['staleValidated'] as $gr)
                    @php
                        $hours = $gr->updated_at ? Carbon::parse($gr->updated_at)->diffInHours(Carbon::now()) : 0;
                    @endphp
                    <li class="mb-2 d-flex flex-wrap justify-content-between gap-2 align-items-center">
                        <a href="{{ route('procurement.goods-receipts.show', $gr) }}">#{{ $gr->id }}</a>
                        <x-ui.operational.status-badge
                            :label="__('operational_ui.workflow_inbox.priority_high')"
                            :variant="$hours >= (int) ($procurementInbox['validatedStaleHours'] ?? 24) ? 'danger' : 'warning'"
                        />
                    </li>
                @empty
                    <li class="text-muted">—</li>
                @endforelse
            </ul>

            <div class="fw-semibold mb-1">{{ __('operational_ui.inbox.failed_receipts') }}</div>
            <ul class="list-unstyled mb-0">
                @forelse ($procurementInbox['failedReceipts'] as $gr)
                    <li class="mb-1">
                        <a href="{{ route('procurement.goods-receipts.show', $gr) }}">#{{ $gr->id }}</a>
                    </li>
                @empty
                    <li class="text-muted">—</li>
                @endforelse
            </ul>
        @endif
    </div>
</div>
