<ul class="navbar-nav active">

    @if(auth()->user()?->isPlatformUser())
        @php $activeWorkspace = app(\App\Models\Solicitor\Solicitor::class); @endphp
        <li class="nav-item d-flex align-items-center me-2">
            <span class="badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 px-2 py-1">
                <i class="mdi mdi-office-building-outline me-1"></i>
                {{ $activeWorkspace?->name ?? 'No Workspace' }}
            </span>
            <a wire:navigate href="{{ route('workspace.switch') }}"
               class="btn btn-sm btn-link text-muted ps-1 pe-0"
               title="Switch workspace">
                <i class="mdi mdi-swap-horizontal"></i>
            </a>
        </li>
        <li class="nav-item"><hr class="vr mx-2 opacity-25"></li>
    @endif

    @can('dashboard.view')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('dashboard') }}">Dashboard</a>
        </li>
    @endcan

    @can('tasks.admin')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('task-hub.index') }}">Task Hub</a>
        </li>
    @endcan

    @can('workboard.view')
        @php
            $workboardCategories = \App\Models\WorkboardCategory::withCount(['availableItems'])->latest()->get();
            $workboardTotal = $workboardCategories->sum('available_items_count');
        @endphp
        <li class="nav-item dropdown">
            <a wire:navigate class="nav-link dropdown-toggle arrow-none" href="" role="button">
                Workboard
                @if($workboardTotal > 0)
                    <span class="badge bg-danger ms-1">{{ $workboardTotal }}</span>
                @endif
                <div class="arrow-down"></div>
            </a>
            <div class="dropdown-menu">
                @foreach($workboardCategories as $category)
                    <a wire:navigate href="{{ route('workboard.category', $category) }}" class="dropdown-item d-flex justify-content-between align-items-center">
                        {{ $category->name }}
                        @if($category->available_items_count > 0)
                            <span class="badge bg-primary ms-2">{{ $category->available_items_count }}</span>
                        @endif
                    </a>
                @endforeach
                @if($workboardCategories->isNotEmpty())
                    <div class="dropdown-divider"></div>
                @endif
                <a wire:navigate href="{{ route('workboard.index') }}" class="dropdown-item">View All</a>
            </div>
        </li>
    @endcan

    @can('inbox.view')
        @php
            $inboxUnread = \App\Models\Solicitor\SolicitorEmailSender::where('solicitor_id', auth()->user()->solicitor_id)
                ->withCount(['inboundMessages as unread_count' => fn($q) => $q->whereNull('read_at')])
                ->get()
                ->sum('unread_count');

            $triagePending = \App\Models\Contact\Hub\ContactMessage::inbound()
                ->where('solicitor_id', auth()->user()->solicitor_id)
                ->whereIn('triage_status', ['pending', 'in_progress'])
                ->whereIn('triage_queue', ['dsar_evidence', 'dsar_updates', 'dsar_needs_research', 'portal_response', 'unknown'])
                ->count();
        @endphp
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('inbox.index') }}">
                Inbox
                @if($inboxUnread > 0)
                    <span class="badge bg-danger ms-1">{{ $inboxUnread }}</span>
                @endif
            </a>
        </li>
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('inbox.triage') }}">
                Triage
                @if($triagePending > 0)
                    <span class="badge bg-warning text-dark ms-1">{{ $triagePending }}</span>
                @endif
            </a>
        </li>
    @endcan

    @can('clients.view')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('clients.index') }}">Clients</a>
        </li>
    @endcan

    @can('solicitors.view')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('solicitors.index') }}">Solicitors</a>
        </li>
    @endcan

    @can('institutions.view')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('institutions.index') }}">Institutions</a>
        </li>
    @endcan

    <li class="nav-item dropdown">
        <a wire:navigate class="nav-link dropdown-toggle arrow-none" href="" role="button">
            DSARs <div class="arrow-down"></div>
        </a>
        <div class="dropdown-menu">
            @can('dsars.view')
                <a wire:navigate href="{{ route('dsars.outstanding') }}" class="dropdown-item">Outstanding DSARs</a>
            @endcan
            @can('dsars.types.view')
                <a wire:navigate href="{{ route('dsars.types') }}" class="dropdown-item">DSAR Types</a>
            @endcan
        </div>
    </li>

    @can('print-queue.view')
        <li class="nav-item">
            <a wire:navigate class="nav-link" href="{{ route('print-queue.index') }}">Print Queue</a>
        </li>
    @endcan

    {{-- Reports --}}
    @can('reports.view')
        <li class="nav-item dropdown">
            <a wire:navigate class="nav-link dropdown-toggle arrow-none" role="button">
                Reports <div class="arrow-down"></div>
            </a>
            <div class="dropdown-menu">
                @can('reports.tranches.view')
                    <a wire:navigate href="{{ route('reports.tranche.index') }}" class="dropdown-item">Tranche Report</a>
                @endcan
                @can('reports.status')
                    <a wire:navigate href="{{ route('reports.status') }}" class="dropdown-item">Status Report</a>
                @endcan
                @can('reports.status.days')
                    <a wire:navigate href="{{ route('reports.status.days') }}" class="dropdown-item">Days In Status Report</a>
                @endcan
                @can('dsars.report')
                    <a wire:navigate href="{{ route('dsars.report') }}" class="dropdown-item">DSAR Report</a>
                    <a wire:navigate href="{{ route('dsars.report.client') }}" class="dropdown-item">DSAR Report by Client</a>
                @endcan
                @can('reports.latest-actions.view')
                    <a wire:navigate href="{{ route('reports.latest-actions') }}" class="dropdown-item">Latest Actions</a>
                @endcan
                @can('reports.system-changes.view')
                    <a wire:navigate href="{{ route('reports.system-changes') }}" class="dropdown-item">System Changes</a>
                @endcan
                @can('reports.trends')
                    <a href="{{ route('reports.trends') }}" class="dropdown-item">Throughput Trends</a>
                @endcan
                @can('reports.trends')
                    <a href="{{ route('reports.pipeline-kpis') }}" class="dropdown-item">Pipeline KPIs</a>
                @endcan
                @can('reports.trends')
                    <a href="{{ route('reports.kpis') }}" class="dropdown-item">KPIs</a>
                @endcan
                @can('reports.wip.tracking')
                    <a href="{{ route('reports.wip.tracking') }}" class="dropdown-item">WIP Tracking</a>
                @endcan
                @can('reports.wip.view')
                    <a href="{{ route('reports.wip.campaign-tranche') }}" class="dropdown-item">
                        WIP Campaign & Tranche Report
                    </a>
                @endcan
                @can('reports.estimated-value.tranches')
                    <a href="{{ route('reports.estimated-value-by-tranche') }}" class="dropdown-item">
                        Tranche Value Estimation
                    </a>
                @endcan
                @can('reports.agent-availability.view')
                    <a wire:navigate href="{{ route('reports.agent-availability') }}" class="dropdown-item">Agent Availability</a>
                @endcan
            </div>
        </li>
    @endcan

    {{-- Admin: Tranches, Lead Sources, Monitoring, Communications, Users, Settings --}}
    @canany(['tranches.view', 'sources.view', 'monitoring.view',
             'templates.view', 'templates.create', 'templates.edit',
             'communications.logs.view.own', 'communications.logs.view.all', 'communications.send.bulk',
             'users.view', 'roles.view', 'permissions.view',
             'statuses.view', 'tags.view', 'doc-types.view', 'key-dates.view',
             'workboard.manage'])
        <li class="nav-item dropdown">
            <a wire:navigate class="nav-link dropdown-toggle arrow-none" href="#" role="button">
                Admin <div class="arrow-down"></div>
            </a>
            <div class="dropdown-menu">
                @can('tranches.view')
                    <a wire:navigate href="{{ route('tranches.index') }}" class="dropdown-item">Tranches</a>
                @endcan
                @can('sources.view')
                    <a wire:navigate href="{{ route('sources.index') }}" class="dropdown-item">Lead Sources</a>
                @endcan

                @can('monitoring.view')
                    <a wire:navigate href="{{ route('monitoring.index') }}" class="dropdown-item">Monitoring</a>
                @endcan

                @canany(['templates.view', 'templates.create', 'templates.edit',
                         'communications.logs.view.own', 'communications.logs.view.all',
                         'communications.send.bulk'])
                    <h6 class="dropdown-header">Communications</h6>
                    @canany(['templates.view', 'templates.create', 'templates.edit'])
                        <a wire:navigate href="{{ route('templates.index') }}" class="dropdown-item">Templates</a>
                    @endcanany
                    @can('communications.send.bulk')
                        <a wire:navigate href="{{ route('communications.bulk-email') }}" class="dropdown-item">Bulk Email Station</a>
                    @endcan
                    @canany(['communications.logs.view.own', 'communications.logs.view.all'])
                        <a wire:navigate href="{{ route('communications.log') }}" class="dropdown-item">Communications Log</a>
                        <a wire:navigate href="{{ route('dialler.dashboard') }}" class="dropdown-item">Dialler Dashboard</a>
                    @endcanany
                @endcanany

                @can('esign.settings.manage')
                    <h6 class="dropdown-header">E-Signatures</h6>
                    <a wire:navigate href="{{ route('esign.settings') }}" class="dropdown-item">Contracts &amp; eSign</a>
                @endcan

                @can('users.view')
                    <h6 class="dropdown-header">Users</h6>
                    <a wire:navigate href="{{ route('admin.users.index') }}" class="dropdown-item">Users</a>
                    @can('roles.view')
                        <a wire:navigate href="{{ route('admin.roles.index') }}" class="dropdown-item">Roles</a>
                    @endcan
                    @can('permissions.view')
                        <a wire:navigate href="{{ route('admin.permissions.index') }}" class="dropdown-item">Permission Centre</a>
                    @endcan
                @endcan

                @canany(['statuses.view', 'tags.view', 'doc-types.view', 'key-dates.view', 'workboard.manage'])
                    <h6 class="dropdown-header">Settings</h6>
                    @can('statuses.view')
                        <a wire:navigate href="{{ route('statuses.index') }}" class="dropdown-item">Claim Statuses</a>
                    @endcan
                    @can('tags.view')
                        <a wire:navigate href="{{ route('tags.index') }}" class="dropdown-item">Tags</a>
                    @endcan
                    @can('doc-types.view')
                        <a wire:navigate href="{{ route('doc-types.index') }}" class="dropdown-item">Document Types</a>
                    @endcan
                    @can('key-dates.view')
                        <a wire:navigate href="{{ route('key-dates.index') }}" class="dropdown-item">Key Dates</a>
                    @endcan
                    @can('workboard.manage')
                        <a wire:navigate href="{{ route('workboard.categories.index') }}" class="dropdown-item">Workboard Categories</a>
                    @endcan
                @endcanany
            </div>
        </li>
    @endcanany

</ul>
