@php
    $user = Auth::user();
    $can = function ($permission) use ($user) {
        return $user && method_exists($user, 'hasNgbvdPermission') && $user->hasNgbvdPermission($permission);
    };
    $canAny = function ($permissions) use ($can) {
        foreach ((array) $permissions as $permission) { if ($can($permission)) { return true; } }
        return false;
    };
    $isActive = function ($patterns) {
        foreach ((array) $patterns as $pattern) { if (request()->is($pattern)) { return 'active'; } }
        return '';
    };
@endphp
<aside class="ngbvd-sidebar" aria-label="Main navigation">
    <div class="ngbvd-sidebar-brand">
        <img src="{{ asset('/assets/images/mglsd.png') }}" alt="MGLSD Logo">
        <div>
            <div class="brand-title">NGBVD</div>
            <span class="brand-subtitle">Ministry of Gender, Labour and Social Development</span>
        </div>
    </div>
    <div class="ngbvd-sidebar-body">
        @if($canAny(['dashboard.view','case_intake.create','case_intake.update','profiled_cases.view','analysis.view','reports.view']))
            <div class="ngbvd-nav-section">
                <div class="ngbvd-nav-section-title">Workspace</div>
                @if($can('dashboard.view'))
                    <a class="ngbvd-nav-link {{ $isActive(['dashboard', 'home']) }}" href="{{ url('/dashboard') }}">
                        <span class="ngbvd-nav-icon"><i class="ti-home"></i></span><span class="ngbvd-nav-text">Dashboard</span>
                    </a>
                @endif

                @if($canAny(['case_intake.create','case_intake.update','profiled_cases.view','case_intake.import','case_intake.fgm']))
                    <div class="ngbvd-nav-group">
                        <button class="ngbvd-nav-toggle {{ $isActive(['dataentry*']) }}" type="button" data-ngbvd-nav-toggle>
                            <span class="ngbvd-nav-icon"><i class="ti-write"></i></span><span class="ngbvd-nav-text">Survivor Data</span><span class="ngbvd-nav-caret">›</span>
                        </button>
                        <div class="ngbvd-nav-submenu">
                            @if($can('case_intake.create'))<a class="{{ $isActive(['dataentry', 'dataentry/index']) }}" href="{{ url('/dataentry') }}">Data Entry</a>@endif
                            @if($can('case_intake.fgm'))<a class="{{ $isActive(['dataentry/fgm']) }}" href="{{ url('/dataentry/fgm') }}">FGM</a>@endif
                            @if($can('profiled_cases.view'))<a class="{{ $isActive(['dataentry/list']) }}" href="{{ url('/dataentry/list') }}">Profiled Cases</a>@endif
                            @if($can('case_intake.import'))<a class="{{ $isActive(['dataentry/import']) }}" href="{{ url('/dataentry/import') }}">Offline Import</a>@endif
                        </div>
                    </div>
                @endif

                @if($canAny(['analysis.view','analysis.mine','analysis.region','analysis.indicator','reports.view']))
                    <div class="ngbvd-nav-group">
                        <button class="ngbvd-nav-toggle {{ $isActive(['analysis*']) }}" type="button" data-ngbvd-nav-toggle>
                            <span class="ngbvd-nav-icon"><i class="ti-bar-chart"></i></span><span class="ngbvd-nav-text">Analysis & Reports</span><span class="ngbvd-nav-caret">›</span>
                        </button>
                        <div class="ngbvd-nav-submenu">
                            @if($can('analysis.view'))<a class="{{ $isActive(['analysis']) }}" href="{{ url('/analysis') }}">Incident Analysis</a>@endif
                            @if($can('analysis.mine') || $can('reports.view'))<a class="{{ $isActive(['analysis/mine']) }}" href="{{ url('/analysis/mine') }}">My Reports</a>@endif
                            @if($can('analysis.region') || $can('reports.view'))<a class="{{ $isActive(['analysis/region']) }}" href="{{ url('/analysis/region') }}">District Report</a>@endif
                            @if($can('analysis.indicator') || $can('reports.view'))<a class="{{ $isActive(['analysis/indicator*']) }}" href="{{ url('/analysis/indicator/' . encrypt('gender')) }}">Specific Indicator</a>@endif
                        </div>
                    </div>
                @endif
            </div>
        @endif

        @if($canAny(['settings.view','settings.update','settings.theme','settings.manage_keys','resources.view','resources.upload','location_settings.view','indicator_settings.view','service_providers.view']))
            <div class="ngbvd-nav-section">
                <div class="ngbvd-nav-section-title">Administration</div>
                @if($canAny(['settings.view','settings.update','settings.theme']))<a class="ngbvd-nav-link {{ $isActive(['settings*']) }}" href="{{ url('/settings') }}"><span class="ngbvd-nav-icon"><i class="ti-settings"></i></span><span class="ngbvd-nav-text">About & Theme</span></a>@endif
                @if($canAny(['resources.view','resources.upload']))<a class="ngbvd-nav-link {{ $isActive(['resources*']) }}" href="{{ url('/resources') }}"><span class="ngbvd-nav-icon"><i class="ti-folder"></i></span><span class="ngbvd-nav-text">Resources</span></a>@endif
                @if($can('location_settings.view'))
                    <div class="ngbvd-nav-group"><button class="ngbvd-nav-toggle {{ $isActive(['location*']) }}" type="button" data-ngbvd-nav-toggle><span class="ngbvd-nav-icon"><i class="ti-location-pin"></i></span><span class="ngbvd-nav-text">Location Settings</span><span class="ngbvd-nav-caret">›</span></button><div class="ngbvd-nav-submenu">
                        <a href="{{ url('/location/nationality') }}">Nationalities</a><a href="{{ url('/location/regions') }}">Regions</a><a href="{{ url('/location/sub-regions') }}">Sub-Regions</a><a href="{{ url('/location/districts') }}">Districts</a><a href="{{ url('/location/sub-county') }}">Sub Counties</a><a href="{{ url('/location/parish') }}">Parishes</a><a href="{{ url('/location/list/region') }}">Location Listing</a>
                    </div></div>
                @endif
                @if($can('indicator_settings.view'))
                    <div class="ngbvd-nav-group"><button class="ngbvd-nav-toggle {{ $isActive(['indicator*']) }}" type="button" data-ngbvd-nav-toggle><span class="ngbvd-nav-icon"><i class="ti-direction-alt"></i></span><span class="ngbvd-nav-text">Indicator Settings</span><span class="ngbvd-nav-caret">›</span></button><div class="ngbvd-nav-submenu">
                        <a href="{{ url('/indicator/incidents') }}">Incident Categories</a><a href="{{ url('/indicator/agegroups') }}">Age Groups</a><a href="{{ url('/indicator/gender') }}">Gender</a><a href="{{ url('/indicator/religion') }}">Religion</a><a href="{{ url('/indicator/relations') }}">Relations</a><a href="{{ url('/indicator/timeofday') }}">Time of Day</a><a href="{{ url('/indicator/maritalstatus') }}">Marital Status</a><a href="{{ url('/indicator/nationalstatus') }}">National Status</a><a href="{{ url('/indicator/disability') }}">Disability Category</a><a href="{{ url('/indicator/list') }}">List Indicators</a>
                    </div></div>
                @endif
                @if($can('service_providers.view'))
                    <div class="ngbvd-nav-group"><button class="ngbvd-nav-toggle {{ $isActive(['partner*']) }}" type="button" data-ngbvd-nav-toggle><span class="ngbvd-nav-icon"><i class="ti-heart"></i></span><span class="ngbvd-nav-text">Service Providers</span><span class="ngbvd-nav-caret">›</span></button><div class="ngbvd-nav-submenu"><a href="{{ url('/partner/category') }}">Categories</a><a href="{{ url('/partner/list') }}">New Service Provider</a><a href="{{ url('/partner/services') }}">Services</a></div></div>
                @endif
                @if($can('settings.manage_keys'))<a class="ngbvd-nav-link {{ $isActive(['config*']) }}" href="{{ url('/config/keys') }}"><span class="ngbvd-nav-icon"><i class="ti-key"></i></span><span class="ngbvd-nav-text">System Keys</span></a>@endif
            </div>
        @endif

        @if($canAny(['api_integrations.view','api_integrations.view_api_urls','api_integrations.view_logs','api_integrations.manage_clients','api_integrations.ubos_push','audit_logs.view']))
            <div class="ngbvd-nav-section">
                <div class="ngbvd-nav-section-title">Governance & APIs</div>
                @if($canAny(['api_integrations.view','api_integrations.view_api_urls','api_integrations.view_logs','api_integrations.manage_clients']))
                    <div class="ngbvd-nav-group">
                        <button class="ngbvd-nav-toggle {{ $isActive(['integrations*']) }}" type="button" data-ngbvd-nav-toggle>
                            <span class="ngbvd-nav-icon"><i class="ti-plug"></i></span><span class="ngbvd-nav-text">API Integrations</span><span class="ngbvd-nav-caret">›</span>
                        </button>
                        <div class="ngbvd-nav-submenu">
                            @if($can('api_integrations.view'))<a class="{{ $isActive(['integrations']) }}" href="{{ url('/integrations') }}">Overview</a>@endif
                            @if($can('api_integrations.view_api_urls'))<a href="{{ url('/integrations/api-urls') }}">API URLs</a>@endif
                            @if($can('api_integrations.view_logs'))<a class="{{ $isActive(['integrations/logs*']) }}" href="{{ url('/integrations/logs') }}">Sharing Logs</a>@endif
                            @if($can('api_integrations.manage_clients'))<a class="{{ $isActive(['integrations/api-clients*']) }}" href="{{ url('/integrations/api-clients') }}">API Clients</a>@endif
                        </div>
                    </div>
                @endif
                @if($can('audit_logs.view'))<a class="ngbvd-nav-link {{ $isActive(['audit/logs*']) }}" href="{{ url('/audit/logs') }}"><span class="ngbvd-nav-icon"><i class="ti-clipboard"></i></span><span class="ngbvd-nav-text">System Audit Logs</span></a>@endif
            </div>
        @endif

        @if($canAny(['users.view','users.create','users.update','roles.view','roles.configure']))
            <div class="ngbvd-nav-section">
                <div class="ngbvd-nav-section-title">Users</div>
                <div class="ngbvd-nav-group"><button class="ngbvd-nav-toggle {{ $isActive(['users*','roles*']) }}" type="button" data-ngbvd-nav-toggle><span class="ngbvd-nav-icon"><i class="ti-user"></i></span><span class="ngbvd-nav-text">User Management</span><span class="ngbvd-nav-caret">›</span></button><div class="ngbvd-nav-submenu">
                    @if($canAny(['users.create','users.update']))<a href="{{ url('/users/index') }}">Add Users</a>@endif
                    @if($can('users.view'))<a href="{{ url('/users/viewusers') }}">View Users</a>@endif
                    @if($canAny(['roles.view','roles.configure']))<a href="{{ url('/roles/list') }}">Roles & Permissions</a>@endif
                </div></div>
            </div>
        @endif

        @if(isset($licenseAlert) && $licenseAlert)
            <div class="alert alert-warning mt-3 mb-0 ngbvd-license-alert"><strong>Warning:</strong> {{ $licenseAlert['message'] }}</div>
        @endif
    </div>
</aside>
