@props(['groupKey', 'badges', 'recent'])

<div class="erp-mega-section-title">{{ __('nav.mega.signals') }}</div>
@php
    $signalLines = [];
    foreach ($badges as $routeName => $count) {
        if ($routeName === 'operations.workflow-inbox.index') {
            continue;
        }
        $show = match ($groupKey) {
            'dashboard' => true,
            'procurement' => str_contains($routeName, 'procurement.'),
            'sales' => str_contains($routeName, 'sales.'),
            'operations' => true,
            default => false,
        };
        if ($show) {
            $signalLines[] = ['route' => $routeName, 'count' => $count];
        }
    }
    if ($groupKey === 'operations' && isset($badges['operations.workflow-inbox.index'])) {
        $signalLines[] = [
            'route' => 'operations.workflow-inbox.index',
            'count' => $badges['operations.workflow-inbox.index'],
        ];
    }
@endphp
@if ($signalLines === [])
    <p class="text-muted small mb-0 erp-mega-muted">{{ __('nav.mega.no_signals') }}</p>
@else
    <ul class="list-unstyled small mb-3">
        @foreach ($signalLines as $line)
            @php
                $r = $line['route'];
                $href = \Illuminate\Support\Facades\Route::has($r) ? route($r) : '#';
            @endphp
            <li class="mb-1">
                <a href="{{ $href }}" class="mega-signal-link">
                    <span class="badge bg-secondary me-1">{{ $line['count'] }}</span>
                    @switch($r)
                        @case('procurement.purchase-orders.index')
                            {{ __('dashboard.action_center.widgets.pending_po_drafts') }}
                            @break
                        @case('procurement.goods-receipts.index')
                            {{ __('dashboard.action_center.widgets.draft_goods_receipts') }}
                            @break
                        @case('sales.orders.index')
                            {{ __('dashboard.action_center.widgets.draft_sales_orders') }}
                            @break
                        @case('operations.workflow-inbox.index')
                            {{ __('nav.mega.workflow_queue') }}
                            @break
                        @default
                            {{ \App\Modules\Navigation\Support\NavigationLabelResolver::signalRouteLabel($r) }}
                    @endswitch
                </a>
            </li>
        @endforeach
    </ul>
@endif

@if ($groupKey === 'dashboard' && $recent !== [])
    <div class="erp-mega-section-title">{{ __('nav.mega.recent') }}</div>
    <ul class="list-unstyled small mb-0">
        @foreach ($recent as $row)
            @if (is_array($row) && ! empty($row['url']))
                @php
                    $recentLabel = ! empty($row['route'])
                        ? \App\Modules\Navigation\Support\NavigationLabelResolver::recentRouteLabel((string) $row['route'])
                        : ($row['label'] ?? $row['url']);
                @endphp
                <li class="mb-1 text-truncate">
                    <a href="{{ $row['url'] }}" class="mega-recent-link"
                        title="{{ $recentLabel }}">{{ $recentLabel }}</a>
                </li>
            @endif
        @endforeach
    </ul>
@endif
