<?php
/**
 * ENT Hospital Menu Configuration
 * Define different menu structures for different user roles
 * Based on the ENT Hospital Management System schema
 */

/**
 * Get menu configuration based on user role
 */
function getMenuConfig($userRole = 'guest') {
    
    // Common menu items for all logged-in users
    $commonMenus = [
        [
            'title' => 'Main Menu',
            'type' => 'header'
        ],
        [
            'title' => 'Dashboard',
            'icon' => 'ph-chart-line',
            'url' => 'index.php',
            'type' => 'link'
        ]
    ];
    
    // Role-specific menu configurations
    $roleMenus = [
        'Super Admin' => [
            // Hospital Management
            [
                'title' => 'Hospital Management',
                'type' => 'header'
            ],
            [
                'title' => 'Hospital Queue',
                'icon' => 'ph-buildings',
                'type' => 'dropdown',
                'id' => 'branchesMenu',
                'items' => [
                    ['title' => 'Queue Dashboard', 'url' => 'queue_dashboard.php']
                ]
            ],
            
            [
                'title' => 'Payments Allocation',
                'icon' => 'ph-buildings',
                'type' => 'dropdown',
                'id' => 'allocationMenu',
                'items' => [
                    ['title' => 'List Allocations', 'url' => 'list_payment_allocations.php'],
                    ['title' => 'List Bulk Payments', 'url' => 'list_bulk_payments.php'],
                    ['title' => 'Doctor Payment Batches', 'url' => 'doctor_payment_batches.php']
                ]
            ],
            
            // User Management
            [
                'title' => 'User Management',
                'type' => 'header'
            ],
            [
                'title' => 'System Users',
                'icon' => 'ph-users',
                'type' => 'dropdown',
                'id' => 'usersMenu',
                'items' => [
                    ['title' => 'View All Users', 'url' => 'list_users.php'],
                    ['title' => 'Add User', 'url' => 'add_user.php'],
                    ['title' => 'User Roles', 'url' => 'user_roles.php'],
                    ['title' => 'Role Permissions', 'url' => 'role_permissions.php']
                ]
            ],
            [
                'title' => 'Medical Staff',
                'icon' => 'ph-stethoscope',
                'type' => 'dropdown',
                'id' => 'medicalStaffMenu',
                'items' => [
                    ['title' => 'ENT Doctors', 'url' => 'list_doctors.php'],
                    ['title' => 'Add Doctor', 'url' => 'add_doctor.php'],
                    ['title' => 'Nurses', 'url' => 'list_nurses.php'],
                    ['title' => 'Add Nurse', 'url' => 'add_nurse.php'],
                    ['title' => 'Lab Technicians', 'url' => 'list_technicians.php'],
                    ['title' => 'Add Technician', 'url' => 'add_technician.php']
                ]
            ],
            
            // Patient Management
            [
                'title' => 'Patient Management',
                'type' => 'header'
            ],
            [
                'title' => 'Patients',
                'icon' => 'ph-address-book',
                'type' => 'dropdown',
                'id' => 'patientsMenu',
                'items' => [
                    ['title' => 'All Patients', 'url' => 'list_patients.php'],
                    ['title' => 'Register Patient', 'url' => 'register_patient.php'],
                    ['title' => 'Patient Search', 'url' => 'patient_search.php'],
                    ['title' => 'Medical Records', 'url' => 'medical_records.php'],
                    ['title' => 'Patient History', 'url' => 'patient_history.php'],
                    ['title' => 'Patient Vitals', 'url' => 'patient_vitals.php'],
                    ['title' => 'Patient Import', 'url' => 'patient_import.php']
                ]
            ],
            [
                'title' => 'Insurance',
                'icon' => 'ph-shield-check',
                'type' => 'dropdown',
                'id' => 'insuranceMenu',
                'items' => [
                    ['title' => 'Insurance Providers', 'url' => 'list_insurance_providers.php'],
                    ['title' => 'Add Provider', 'url' => 'add_insurance_provider.php'],
                    ['title' => 'Claims Management', 'url' => 'insurance_claims.php']
                ]
            ],
            
            // Appointment Management
            [
                'title' => 'Appointments',
                'type' => 'header'
            ],
            [
                'title' => 'Appointment Management',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'appointmentsMenu',
                'items' => [
                    ['title' => 'All Appointments', 'url' => 'list_appointments.php'],
                    ['title' => 'Schedule Appointment', 'url' => 'schedule_appointment.php'],
                    ['title' => 'Doctor Schedules', 'url' => 'doctor_schedules.php'],
                    ['title' => 'Appointment Slots', 'url' => 'appointment_slots.php'],
                    ['title' => 'Online Bookings', 'url' => 'online_bookings.php']
                ]
            ],

            // Clinical Services
            [
                'title' => 'Clinical Services',
                'type' => 'header'
            ],
            [
                'title' => 'Consultations',
                'icon' => 'ph-user-focus',
                'type' => 'dropdown',
                'id' => 'consultationsMenu',
                'items' => [
                    ['title' => 'All Consultations', 'url' => 'list_consultations.php'],
                    ['title' => 'New Consultation', 'url' => 'new_consultation.php'],
                    ['title' => 'Follow-up Visits', 'url' => 'followup_visits.php'],
                    ['title' => 'Telemedicine', 'url' => 'telemedicine.php'],
                    ['title' => 'Create PreAuth', 'url' => 'create_preauthorization.php'],
                    ['title' => 'List PreAuths', 'url' => 'list_preauthorizations.php']
                ]
            ],
            [
                'title' => 'ENT Procedures',
                'icon' => 'ph-activity',
                'type' => 'dropdown',
                'id' => 'proceduresMenu',
                'items' => [
                    ['title' => 'All Procedures', 'url' => 'list_procedures.php'],
                    ['title' => 'Schedule Procedure', 'url' => 'schedule_procedure.php'],
                    ['title' => 'Procedure Types', 'url' => 'procedure_types.php'],
                    ['title' => 'Add Procedure Type', 'url' => 'add_procedure_type.php'],
                    ['title' => 'Surgery Schedule', 'url' => 'surgery_schedule.php'],
                    ['title' => 'Manage ICD10', 'url' => 'manage_icd10_codes.php']
                ]
            ],
            
            // Pharmacy Services
            [
                'title' => 'Pharmacy Services',
                'type' => 'header'
            ],
            [
                'title' => 'Pharmacy',
                'icon' => 'ph-pill',
                'type' => 'dropdown',
                'id' => 'pharmacyMenu',
                'items' => [
                    ['title' => 'Pharmacy Dashboard', 'url' => 'pharmacy_dashboard.php'],
                    ['title' => 'Pharmacy Dispensing', 'url' => 'pharmacy_dispense.php'],
                    ['title' => 'Pharmacy Inventory', 'url' => 'pharmacy_inventory.php']
                ]
            ],
            
            // Laboratory Services
            [
                'title' => 'Laboratory Services',
                'type' => 'header'
            ],
            [
                'title' => 'Lab Orders',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'labOrdersMenu',
                'items' => [
                    ['title' => 'All Lab Orders', 'url' => 'list_lab_orders.php'],
                    ['title' => 'Create Lab Order', 'url' => 'create_lab_order.php'],
                    ['title' => 'Pending Tests', 'url' => 'pending_tests.php'],
                    ['title' => 'Test Results', 'url' => 'test_results.php'],
                    ['title' => 'Lab Reports', 'url' => 'lab_reports.php']
                ]
            ],
            [
                'title' => 'ENT Tests',
                'icon' => 'ph-ear',
                'type' => 'dropdown',
                'id' => 'entTestsMenu',
                'items' => [
                    ['title' => 'Audiometry', 'url' => 'audiometry_tests.php'],
                    ['title' => 'Vestibular Tests', 'url' => 'vestibular_tests.php'],
                    ['title' => 'Endoscopy', 'url' => 'endoscopy_tests.php'],
                    ['title' => 'Imaging Studies', 'url' => 'imaging_studies.php'],
                    ['title' => 'Allergy Testing', 'url' => 'allergy_tests.php'],
                    ['title' => 'Sleep Studies', 'url' => 'sleep_studies.php']
                ]
            ],
            [
                'title' => 'Test Management',
                'icon' => 'ph-gear',
                'type' => 'dropdown',
                'id' => 'testManagementMenu',
                'items' => [
                    ['title' => 'Test Types', 'url' => 'test_types.php'],
                    ['title' => 'Add Test Type', 'url' => 'add_test_type.php'],
                    ['title' => 'Equipment Management', 'url' => 'equipment_management.php']
                ]
            ],
            
            // Financial Management Section for Administrator
            [
                'title' => 'Financial Management',
                'type' => 'header'
            ],
            [
                'title' => 'Dashboard',
                'icon' => 'ph-chart-line-up',
                'url' => 'financial_dashboard.php',
                'type' => 'link'
            ],
            [
                'title' => 'Billing & Invoices',
                'icon' => 'ph-receipt',
                'type' => 'dropdown',
                'id' => 'billingInvoicesMenu',
                'items' => [
                    ['title' => 'All Invoices', 'url' => 'list_invoices.php'],
                    ['title' => 'Create Invoice', 'url' => 'create_invoice.php'],
                    ['title' => 'Pending Invoices', 'url' => 'pending_invoices.php'],
                    ['title' => 'Overdue Invoices', 'url' => 'overdue_invoices.php'],
                    ['title' => 'Draft Invoices', 'url' => 'draft_invoices.php'],
                    ['title' => 'Service Pricing', 'url' => 'service_pricing.php'],
                    ['title' => 'Tax Configuration', 'url' => 'tax_configuration.php']
                ]
            ],
            [
                'title' => 'Payment Processing',
                'icon' => 'ph-money',
                'type' => 'dropdown',
                'id' => 'paymentProcessingMenu',
                'items' => [
                    ['title' => 'All Payments', 'url' => 'list_payments.php'],
                    ['title' => 'Record Payment', 'url' => 'record_payment.php'],
                    ['title' => 'Bulk Payments', 'url' => 'bulk_payments.php'],
                    ['title' => 'Payment Reconciliation', 'url' => 'payment_reconciliation.php'],
                    ['title' => 'Refunds & Credits', 'url' => 'refunds.php'],
                    ['title' => 'Payment Methods', 'url' => 'payment_methods.php']
                ]
            ],
            [
                'title' => 'M-Pesa Integration',
                'icon' => 'ph-device-mobile',
                'type' => 'dropdown',
                'id' => 'mpesaMenu',
                'items' => [
                    ['title' => 'M-Pesa Transactions', 'url' => 'mpesa_transactions.php'],
                    ['title' => 'STK Push History', 'url' => 'mpesa_stk_history.php'],
                    ['title' => 'C2B Payments', 'url' => 'mpesa_c2b.php'],
                    ['title' => 'B2C Disbursements', 'url' => 'mpesa_b2c.php'],
                    ['title' => 'M-Pesa Reconciliation', 'url' => 'mpesa_reconciliation.php'],
                    ['title' => 'M-Pesa Settings', 'url' => 'system_settings.php#mpesa']
                ]
            ],
            [
                'title' => 'Insurance Management',
                'icon' => 'ph-shield-check',
                'type' => 'dropdown',
                'id' => 'insuranceFinancialMenu',
                'items' => [
                    ['title' => 'Insurance Claims', 'url' => 'insurance_claims.php'],
                    ['title' => 'Submit Claims', 'url' => 'submit_insurance_claims.php'],
                    ['title' => 'Track Claims', 'url' => 'track_insurance_claims.php'],
                    ['title' => 'Claims Reconciliation', 'url' => 'insurance_reconciliation.php'],
                    ['title' => 'Pre-authorization', 'url' => 'insurance_preauth.php'],
                    ['title' => 'Insurance Providers', 'url' => 'insurance_providers.php'],
                    ['title' => 'Provider Settlements', 'url' => 'insurance_settlements.php']
                ]
            ],
            [
                'title' => 'Accounting & Ledger',
                'icon' => 'ph-book-open',
                'type' => 'dropdown',
                'id' => 'accountingMenu',
                'items' => [
                    ['title' => 'General Ledger', 'url' => 'general_ledger.php'],
                    ['title' => 'Chart of Accounts', 'url' => 'chart_of_accounts.php'],
                    ['title' => 'Journal Entries', 'url' => 'journal_entries.php'],
                    ['title' => 'Trial Balance', 'url' => 'trial_balance.php'],
                    ['title' => 'Account Reconciliation', 'url' => 'account_reconciliation.php']
                ]
            ],
            [
                'title' => 'Staff Commissions',
                'icon' => 'ph-wallet',
                'type' => 'dropdown',
                'id' => 'commissionsMenu',
                'items' => [
                    ['title' => 'Commission Dashboard', 'url' => 'commission_dashboard.php'],
                    ['title' => 'Commission Tracking', 'url' => 'staff_commissions.php'],
                    ['title' => 'Pending Commissions', 'url' => 'pending_commissions.php'],
                    ['title' => 'Commission Payments', 'url' => 'commission_payments.php'],
                    ['title' => 'Commission Reports', 'url' => 'commission_reports.php'],
                    ['title' => 'Commission Rules', 'url' => 'commission_rules.php']
                ]
            ],
            [
                'title' => 'eTIMS Integration',
                'icon' => 'ph-shield-checkered',
                'type' => 'dropdown',
                'id' => 'etimsMenu',
                'items' => [
                    ['title' => 'eTIMS Dashboard', 'url' => 'etims_dashboard.php'],
                    ['title' => 'Tax Invoices', 'url' => 'etims_invoices.php'],
                    ['title' => 'eTIMS Submissions', 'url' => 'etims_submissions.php'],
                    ['title' => 'Credit Notes', 'url' => 'etims_credit_notes.php'],
                    ['title' => 'Item Registration', 'url' => 'etims_items.php'],
                    ['title' => 'eTIMS Settings', 'url' => 'system_settings.php#etims']
                ]
            ],
            [
                'title' => 'Financial Reports',
                'icon' => 'ph-chart-bar',
                'type' => 'dropdown',
                'id' => 'financialReportsMenu',
                'items' => [
                    ['title' => 'Revenue Reports', 'url' => 'revenue_reports.php'],
                    ['title' => 'Income Statement', 'url' => 'income_statement.php'],
                    ['title' => 'Balance Sheet', 'url' => 'balance_sheet.php'],
                    ['title' => 'Cash Flow Statement', 'url' => 'cash_flow_statement.php'],
                    ['title' => 'Aging Reports', 'url' => 'aging_reports.php'],
                    ['title' => 'Daily Revenue', 'url' => 'daily_revenue.php'],
                    ['title' => 'Monthly Reports', 'url' => 'monthly_reports.php'],
                    ['title' => 'Payment Analysis', 'url' => 'payment_analysis.php'],
                    ['title' => 'Outstanding Balances', 'url' => 'outstanding_balances.php'],
                    ['title' => 'Tax Reports', 'url' => 'tax_reports.php']
                ]
            ],
            [
                'title' => 'Expenses & Petty Cash',
                'icon' => 'ph-coins',
                'type' => 'dropdown',
                'id' => 'expensesMenu',
                'items' => [
                    ['title' => 'All Expenses', 'url' => 'list_expenses.php'],
                    ['title' => 'Record Expense', 'url' => 'record_expense.php'],
                    ['title' => 'Petty Cash', 'url' => 'petty_cash.php'],
                    ['title' => 'Expense Categories', 'url' => 'expense_categories.php'],
                    ['title' => 'Expense Reports', 'url' => 'expense_reports.php']
                ]
            ],
            
            // Communication
            [
                'title' => 'Communication',
                'type' => 'header'
            ],
            [
                'title' => 'Chatbot',
                'icon' => 'ph-bell',
                'type' => 'dropdown',
                'id' => 'chatbotMenu',
                'items' => [
                    ['title' => 'Chatbot', 'url' => 'ent_chatbot.php']
                ]
            ],
            [
                'title' => 'Notifications',
                'icon' => 'ph-bell',
                'type' => 'dropdown',
                'id' => 'notificationsMenu',
                'items' => [
                    ['title' => 'Send SMS', 'url' => 'send_sms.php'],
                    ['title' => 'Send Email', 'url' => 'send_email.php'],
                    ['title' => 'Communication Log', 'url' => 'communication_log.php'],
                    ['title' => 'Templates', 'url' => 'communication_templates.php'],
                    ['title' => 'Appointment Reminders', 'url' => 'appointment_reminders.php']
                ]
            ],
            [
                'title' => 'Document Sharing',
                'icon' => 'ph-share-fat',
                'type' => 'dropdown',
                'id' => 'documentSharingMenu',
                'items' => [
                    ['title' => 'Medical Documents', 'url' => 'medical_documents.php'],
                    ['title' => 'Secure Sharing', 'url' => 'secure_document_sharing.php'],
                    ['title' => 'Share History', 'url' => 'document_share_history.php']
                ]
            ],
            
            // Reports & Analytics
            [
                'title' => 'Reports & Analytics',
                'type' => 'header'
            ],
            [
                'title' => 'Reports',
                'icon' => 'ph-chart-line',
                'type' => 'dropdown',
                'id' => 'reportsMenu',
                'items' => [
                    ['title' => 'Patient Reports', 'url' => 'patient_reports.php'],
                    ['title' => 'Financial Reports', 'url' => 'financial_reports.php'],
                    ['title' => 'Doctor Performance', 'url' => 'doctor_performance.php'],
                    ['title' => 'Department Reports', 'url' => 'department_reports.php'],
                    ['title' => 'Custom Reports', 'url' => 'custom_reports.php']
                ]
            ],
            
            // System Administration
            [
                'title' => 'System Administration',
                'type' => 'header'
            ],
            [
                'title' => 'System Settings',
                'icon' => 'ph-gear',
                'type' => 'dropdown',
                'id' => 'settingsMenu',
                'items' => [
                    ['title' => 'Hospital Settings', 'url' => 'system_settings.php'],
                    ['title' => 'SMART Test Configuration', 'url' => 'test_smart_connection.php'],
                    ['title' => 'Backup & Restore', 'url' => 'backup_restore.php'],
                    ['title' => 'Activity Log', 'url' => 'activity_log.php']
                ]
            ]
        ],
        
        'Hospital Admin' => [
            // Patient Management
            [
                'title' => 'Patient Management',
                'type' => 'header'
            ],
            [
                'title' => 'Patients',
                'icon' => 'ph-address-book',
                'type' => 'dropdown',
                'id' => 'patientsMenu',
                'items' => [
                    ['title' => 'All Patients', 'url' => 'list_patients.php'],
                    ['title' => 'Register Patient', 'url' => 'register_patient.php'],
                    ['title' => 'Patient Search', 'url' => 'patient_search.php'],
                    ['title' => 'Medical Records', 'url' => 'medical_records.php']
                ]
            ],
            
            // Staff Management
            [
                'title' => 'Staff Management',
                'type' => 'header'
            ],
            [
                'title' => 'Medical Staff',
                'icon' => 'ph-stethoscope',
                'type' => 'dropdown',
                'id' => 'medicalStaffMenu',
                'items' => [
                    ['title' => 'ENT Doctors', 'url' => 'list_doctors.php'],
                    ['title' => 'Nurses', 'url' => 'list_nurses.php'],
                    ['title' => 'Lab Technicians', 'url' => 'list_technicians.php'],
                    ['title' => 'Staff Schedules', 'url' => 'staff_schedules.php']
                ]
            ],
            
            // Appointments
            [
                'title' => 'Appointments',
                'type' => 'header'
            ],
            [
                'title' => 'Appointment Management',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'appointmentsMenu',
                'items' => [
                    ['title' => 'All Appointments', 'url' => 'list_appointments.php'],
                    ['title' => 'Schedule Appointment', 'url' => 'schedule_appointment.php'],
                    ['title' => 'Doctor Schedules', 'url' => 'doctor_schedules.php'],
                    ['title' => 'Online Bookings', 'url' => 'online_bookings.php']
                ]
            ],
            
            // Financial Management
            [
                'title' => 'Financial Management',
                'type' => 'header'
            ],
            [
                'title' => 'Billing',
                'icon' => 'ph-receipt',
                'type' => 'dropdown',
                'id' => 'billingMenu',
                'items' => [
                    ['title' => 'All Invoices', 'url' => 'list_invoices.php'],
                    ['title' => 'Create Invoice', 'url' => 'create_invoice.php'],
                    ['title' => 'Pending Payments', 'url' => 'pending_payments.php']
                ]
            ],
            [
                'title' => 'Payments',
                'icon' => 'ph-money',
                'type' => 'dropdown',
                'id' => 'paymentsMenu',
                'items' => [
                    ['title' => 'All Payments', 'url' => 'list_payments.php'],
                    ['title' => 'Record Payment', 'url' => 'record_payment.php'],
                    ['title' => 'M-Pesa Transactions', 'url' => 'mpesa_transactions.php']
                ]
            ],
            
            // Reports
            [
                'title' => 'Reports',
                'type' => 'header'
            ],
            [
                'title' => 'Hospital Reports',
                'icon' => 'ph-chart-line',
                'type' => 'dropdown',
                'id' => 'reportsMenu',
                'items' => [
                    ['title' => 'Patient Reports', 'url' => 'patient_reports.php'],
                    ['title' => 'Financial Reports', 'url' => 'financial_reports.php'],
                    ['title' => 'Staff Performance', 'url' => 'staff_performance.php']
                ]
            ]
        ],
        
        'ENT Consultant' => [
            // Clinical Practice
            [
                'title' => 'Clinical Practice',
                'type' => 'header'
            ],
            [
                'title' => 'My Patients',
                'icon' => 'ph-address-book',
                'type' => 'dropdown',
                'id' => 'myPatientsMenu',
                'items' => [
                    ['title' => 'Today\'s Patients', 'url' => 'todays_patients.php'],
                    ['title' => 'All My Patients', 'url' => 'my_patients.php'],
                    ['title' => 'Patient Search', 'url' => 'patient_search.php'],
                    ['title' => 'Medical History', 'url' => 'patient_medical_history.php']
                ]
            ],
            [
                'title' => 'Consultations',
                'icon' => 'ph-user-focus',
                'type' => 'dropdown',
                'id' => 'consultationsMenu',
                'items' => [
                    ['title' => 'Active Consultations', 'url' => 'active_consultations.php'],
                    ['title' => 'New Consultation', 'url' => 'new_consultation.php'],
                    ['title' => 'Follow-up Visits', 'url' => 'followup_visits.php'],
                    ['title' => 'Consultation History', 'url' => 'consultation_history.php']
                ]
            ],
            [
                'title' => 'ENT Procedures',
                'icon' => 'ph-activity',
                'type' => 'dropdown',
                'id' => 'proceduresMenu',
                'items' => [
                    ['title' => 'My Procedures', 'url' => 'my_procedures.php'],
                    ['title' => 'Schedule Procedure', 'url' => 'schedule_procedure.php'],
                    ['title' => 'Surgery Schedule', 'url' => 'my_surgery_schedule.php'],
                    ['title' => 'Procedure History', 'url' => 'procedure_history.php']
                ]
            ],
            
            // Appointments
            [
                'title' => 'Appointments',
                'type' => 'header'
            ],
            [
                'title' => 'My Schedule',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'scheduleMenu',
                'items' => [
                    ['title' => 'Today\'s Appointments', 'url' => 'todays_appointments.php'],
                    ['title' => 'My Appointments', 'url' => 'my_appointments.php'],
                    ['title' => 'Availability Slots', 'url' => 'my_availability.php'],
                    ['title' => 'Appointment History', 'url' => 'appointment_history.php']
                ]
            ],
            
            // Laboratory
            [
                'title' => 'Laboratory',
                'type' => 'header'
            ],
            [
                'title' => 'Lab Orders',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'labMenu',
                'items' => [
                    ['title' => 'Order Tests', 'url' => 'order_tests.php'],
                    ['title' => 'My Lab Orders', 'url' => 'my_lab_orders.php'],
                    ['title' => 'Test Results', 'url' => 'test_results.php'],
                    ['title' => 'Pending Results', 'url' => 'pending_results.php']
                ]
            ],
            
            // Documentation
            [
                'title' => 'Medical Documentation',
                'type' => 'header'
            ],
            [
                'title' => 'Medical Records',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'recordsMenu',
                'items' => [
                    ['title' => 'Patient Documents', 'url' => 'patient_documents.php'],
                    ['title' => 'Upload Document', 'url' => 'upload_document.php'],
                    ['title' => 'Share Medical Data', 'url' => 'share_medical_data.php'],
                    ['title' => 'Document History', 'url' => 'document_history.php']
                ]
            ],
            
            // Performance
            [
                'title' => 'Performance',
                'type' => 'header'
            ],
            [
                'title' => 'My Performance',
                'icon' => 'ph-chart-bar',
                'type' => 'dropdown',
                'id' => 'performanceMenu',
                'items' => [
                    ['title' => 'Patient Statistics', 'url' => 'my_patient_stats.php'],
                    ['title' => 'Revenue Summary', 'url' => 'my_revenue.php'],
                    ['title' => 'Commission Tracker', 'url' => 'my_commissions.php']
                ]
            ]
        ],
        
        'ENT Doctor' => [
            // Clinical Practice
            [
                'title' => 'Clinical Practice',
                'type' => 'header'
            ],
            [
                'title' => 'My Patients',
                'icon' => 'ph-address-book',
                'type' => 'dropdown',
                'id' => 'myPatientsMenu',
                'items' => [
                    ['title' => 'Today\'s Patients', 'url' => 'todays_patients.php'],
                    ['title' => 'All My Patients', 'url' => 'my_patients.php'],
                    ['title' => 'Patient Search', 'url' => 'patient_search.php']
                ]
            ],
            [
                'title' => 'Consultations',
                'icon' => 'ph-user-focus',
                'type' => 'dropdown',
                'id' => 'consultationsMenu',
                'items' => [
                    ['title' => 'New Consultation', 'url' => 'new_consultation.php'],
                    ['title' => 'Active Consultations', 'url' => 'active_consultations.php'],
                    ['title' => 'Follow-up Visits', 'url' => 'followup_visits.php']
                ]
            ],
            
            // Appointments
            [
                'title' => 'Appointments',
                'type' => 'header'
            ],
            [
                'title' => 'My Schedule',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'scheduleMenu',
                'items' => [
                    ['title' => 'Today\'s Appointments', 'url' => 'todays_appointments.php'],
                    ['title' => 'My Appointments', 'url' => 'my_appointments.php'],
                    ['title' => 'Set Availability', 'url' => 'set_availability.php']
                ]
            ],
            
            // Laboratory
            [
                'title' => 'Laboratory',
                'type' => 'header'
            ],
            [
                'title' => 'Lab Orders',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'labMenu',
                'items' => [
                    ['title' => 'Order Tests', 'url' => 'order_tests.php'],
                    ['title' => 'My Lab Orders', 'url' => 'my_lab_orders.php'],
                    ['title' => 'Test Results', 'url' => 'test_results.php']
                ]
            ],
            
            // Medical Records
            [
                'title' => 'Medical Records',
                'type' => 'header'
            ],
            [
                'title' => 'Documentation',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'recordsMenu',
                'items' => [
                    ['title' => 'Patient Documents', 'url' => 'patient_documents.php'],
                    ['title' => 'Upload Document', 'url' => 'upload_document.php']
                ]
            ]
        ],
        
        'Resident Doctor' => [
            // Clinical Practice (Limited)
            [
                'title' => 'Clinical Practice',
                'type' => 'header'
            ],
            [
                'title' => 'My Patients',
                'icon' => 'ph-address-book',
                'type' => 'dropdown',
                'id' => 'myPatientsMenu',
                'items' => [
                    ['title' => 'Assigned Patients', 'url' => 'assigned_patients.php'],
                    ['title' => 'Patient History', 'url' => 'patient_history.php']
                ]
            ],
            [
                'title' => 'Consultations',
                'icon' => 'ph-user-focus',
                'url' => 'supervised_consultations.php',
                'type' => 'link'
            ],
            
            // Learning & Training
            [
                'title' => 'Training',
                'type' => 'header'
            ],
            [
                'title' => 'Clinical Training',
                'icon' => 'ph-graduation-cap',
                'type' => 'dropdown',
                'id' => 'trainingMenu',
                'items' => [
                    ['title' => 'Training Log', 'url' => 'training_log.php'],
                    ['title' => 'Case Studies', 'url' => 'case_studies.php'],
                    ['title' => 'Supervisor Feedback', 'url' => 'supervisor_feedback.php']
                ]
            ]
        ],
        
        'Head Nurse' => [
            // Ward Management
            [
                'title' => 'Ward Management',
                'type' => 'header'
            ],
            [
                'title' => 'Patient Care',
                'icon' => 'ph-heart',
                'type' => 'dropdown',
                'id' => 'patientCareMenu',
                'items' => [
                    ['title' => 'Ward Patients', 'url' => 'ward_patients.php'],
                    ['title' => 'Patient Vitals', 'url' => 'patient_vitals.php'],
                    ['title' => 'Medication Schedule', 'url' => 'medication_schedule.php'],
                    ['title' => 'Nursing Notes', 'url' => 'nursing_notes.php']
                ]
            ],
            [
                'title' => 'Nursing Staff',
                'icon' => 'ph-users',
                'type' => 'dropdown',
                'id' => 'nursingStaffMenu',
                'items' => [
                    ['title' => 'Staff Roster', 'url' => 'nursing_roster.php'],
                    ['title' => 'Shift Management', 'url' => 'shift_management.php'],
                    ['title' => 'Staff Performance', 'url' => 'nursing_performance.php']
                ]
            ],
            
            // Procedures
            [
                'title' => 'Nursing Procedures',
                'type' => 'header'
            ],
            [
                'title' => 'Patient Procedures',
                'icon' => 'ph-activity',
                'type' => 'dropdown',
                'id' => 'proceduresMenu',
                'items' => [
                    ['title' => 'Pre-procedure Care', 'url' => 'pre_procedure_care.php'],
                    ['title' => 'Post-procedure Care', 'url' => 'post_procedure_care.php'],
                    ['title' => 'Recovery Monitoring', 'url' => 'recovery_monitoring.php']
                ]
            ]
        ],
        
        'ENT Nurse' => [
            // Patient Care
            [
                'title' => 'Patient Care',
                'type' => 'header'
            ],
            [
                'title' => 'My Patients',
                'icon' => 'ph-heart',
                'type' => 'dropdown',
                'id' => 'patientCareMenu',
                'items' => [
                    ['title' => 'Assigned Patients', 'url' => 'assigned_patients.php'],
                    ['title' => 'Patient Vitals', 'url' => 'patient_vitals.php'],
                    ['title' => 'Medication Administration', 'url' => 'medication_admin.php'],
                    ['title' => 'Nursing Notes', 'url' => 'nursing_notes.php']
                ]
            ],
            
            // ENT Procedures
            [
                'title' => 'ENT Procedures',
                'type' => 'header'
            ],
            [
                'title' => 'Procedure Assistance',
                'icon' => 'ph-activity',
                'type' => 'dropdown',
                'id' => 'procedureAssistMenu',
                'items' => [
                    ['title' => 'Scheduled Procedures', 'url' => 'scheduled_procedures.php'],
                    ['title' => 'Pre-procedure Prep', 'url' => 'pre_procedure_prep.php'],
                    ['title' => 'Post-procedure Care', 'url' => 'post_procedure_care.php']
                ]
            ],
            
            // Patient Education
            [
                'title' => 'Patient Education',
                'type' => 'header'
            ],
            [
                'title' => 'Education Materials',
                'icon' => 'ph-book-open',
                'url' => 'patient_education.php',
                'type' => 'link'
            ]
        ],
        
        'Audiologist' => [
            // Hearing Services
            [
                'title' => 'Hearing Services',
                'type' => 'header'
            ],
            [
                'title' => 'Audiometry Tests',
                'icon' => 'ph-ear',
                'type' => 'dropdown',
                'id' => 'audiometryMenu',
                'items' => [
                    ['title' => 'Pure Tone Audiometry', 'url' => 'pure_tone_audiometry.php'],
                    ['title' => 'Speech Audiometry', 'url' => 'speech_audiometry.php'],
                    ['title' => 'Tympanometry', 'url' => 'tympanometry.php'],
                    ['title' => 'OAE Testing', 'url' => 'oae_testing.php'],
                    ['title' => 'ABR Testing', 'url' => 'abr_testing.php']
                ]
            ],
            [
                'title' => 'Vestibular Tests',
                'icon' => 'ph-circles-three',
                'type' => 'dropdown',
                'id' => 'vestibularMenu',
                'items' => [
                    ['title' => 'Balance Assessment', 'url' => 'balance_assessment.php'],
                    ['title' => 'VNG Testing', 'url' => 'vng_testing.php'],
                    ['title' => 'Caloric Testing', 'url' => 'caloric_testing.php'],
                    ['title' => 'VEMP Testing', 'url' => 'vemp_testing.php']
                ]
            ],
            
            // Test Management
            [
                'title' => 'Test Management',
                'type' => 'header'
            ],
            [
                'title' => 'My Tests',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'myTestsMenu',
                'items' => [
                    ['title' => 'Scheduled Tests', 'url' => 'scheduled_tests.php'],
                    ['title' => 'Test Results', 'url' => 'my_test_results.php'],
                    ['title' => 'Test Reports', 'url' => 'test_reports.php'],
                    ['title' => 'Equipment Log', 'url' => 'equipment_log.php']
                ]
            ],
            
            // Hearing Aid Services
            [
                'title' => 'Hearing Aid Services',
                'type' => 'header'
            ],
            [
                'title' => 'Hearing Aids',
                'icon' => 'ph-speaker-high',
                'type' => 'dropdown',
                'id' => 'hearingAidsMenu',
                'items' => [
                    ['title' => 'Hearing Aid Fitting', 'url' => 'hearing_aid_fitting.php'],
                    ['title' => 'Follow-up Care', 'url' => 'hearing_aid_followup.php'],
                    ['title' => 'Maintenance Log', 'url' => 'hearing_aid_maintenance.php']
                ]
            ]
        ],
        
        'Lab Technician' => [
            // Laboratory Operations
            [
                'title' => 'Laboratory Operations',
                'type' => 'header'
            ],
            [
                'title' => 'Test Processing',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'testProcessingMenu',
                'items' => [
                    ['title' => 'Pending Tests', 'url' => 'pending_lab_tests.php'],
                    ['title' => 'In Progress', 'url' => 'inprogress_tests.php'],
                    ['title' => 'Complete Tests', 'url' => 'complete_tests.php'],
                    ['title' => 'Test Results Entry', 'url' => 'test_results_entry.php']
                ]
            ],
            [
                'title' => 'Sample Management',
                'icon' => 'ph-drop',
                'type' => 'dropdown',
                'id' => 'sampleMenu',
                'items' => [
                    ['title' => 'Sample Collection', 'url' => 'sample_collection.php'],
                    ['title' => 'Sample Tracking', 'url' => 'sample_tracking.php'],
                    ['title' => 'Sample Storage', 'url' => 'sample_storage.php']
                ]
            ],
            
            // ENT Specific Tests
            [
                'title' => 'ENT Laboratory Tests',
                'type' => 'header'
            ],
            [
                'title' => 'Microbiology',
                'icon' => 'ph-bacteria',
                'type' => 'dropdown',
                'id' => 'microbiologyMenu',
                'items' => [
                    ['title' => 'Culture Tests', 'url' => 'culture_tests.php'],
                    ['title' => 'Sensitivity Testing', 'url' => 'sensitivity_testing.php'],
                    ['title' => 'Gram Staining', 'url' => 'gram_staining.php']
                ]
            ],
            [
                'title' => 'Pathology',
                'icon' => 'ph-eye',
                'type' => 'dropdown',
                'id' => 'pathologyMenu',
                'items' => [
                    ['title' => 'Biopsy Processing', 'url' => 'biopsy_processing.php'],
                    ['title' => 'Histopathology', 'url' => 'histopathology.php'],
                    ['title' => 'Cytology', 'url' => 'cytology.php']
                ]
            ],
            
            // Quality Control
            [
                'title' => 'Quality Control',
                'type' => 'header'
            ],
            [
                'title' => 'Lab Quality',
                'icon' => 'ph-shield-check',
                'type' => 'dropdown',
                'id' => 'qualityMenu',
                'items' => [
                    ['title' => 'Quality Control', 'url' => 'quality_control.php'],
                    ['title' => 'Equipment Calibration', 'url' => 'equipment_calibration.php'],
                    ['title' => 'Lab Reports', 'url' => 'lab_performance_reports.php']
                ]
            ]
        ],
        
        'Receptionist' => [
            // Front Office Operations
            [
                'title' => 'Front Office',
                'type' => 'header'
            ],
            [
                'title' => 'Patient Registration',
                'icon' => 'ph-user-plus',
                'type' => 'dropdown',
                'id' => 'registrationMenu',
                'items' => [
                    ['title' => 'Walk-in Registration', 'url' => 'walkin_registration.php'],
                    ['title' => 'Insurance Registration', 'url' => 'insurance_registration.php'],
                    ['title' => 'Corporate Registration', 'url' => 'corporate_registration.php'],
                    ['title' => 'Patient Search', 'url' => 'patient_search.php'],
                    ['title' => 'Update Patient Info', 'url' => 'update_patient.php']
                ]
            ],
            [
                'title' => 'Appointments',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'appointmentsMenu',
                'items' => [
                    ['title' => 'Book Appointment', 'url' => 'book_appointment.php'],
                    ['title' => 'Today\'s Appointments', 'url' => 'todays_appointments.php'],
                    ['title' => 'Appointment Calendar', 'url' => 'appointment_calendar.php'],
                    ['title' => 'Reschedule/Cancel', 'url' => 'manage_appointments.php'],
                    ['title' => 'Online Bookings', 'url' => 'online_bookings.php']
                ]
            ],
            
            // Patient Services
            [
                'title' => 'Patient Services',
                'type' => 'header'
            ],
            [
                'title' => 'Check-in/Check-out',
                'icon' => 'ph-sign-in',
                'type' => 'dropdown',
                'id' => 'checkinMenu',
                'items' => [
                    ['title' => 'Patient Check-in', 'url' => 'patient_checkin.php'],
                    ['title' => 'Waiting Room', 'url' => 'waiting_room.php'],
                    ['title' => 'Patient Check-out', 'url' => 'patient_checkout.php']
                ]
            ],
            [
                'title' => 'Communications',
                'icon' => 'ph-phone',
                'type' => 'dropdown',
                'id' => 'communicationsMenu',
                'items' => [
                    ['title' => 'Send SMS', 'url' => 'send_patient_sms.php'],
                    ['title' => 'Send Email', 'url' => 'send_patient_email.php'],
                    ['title' => 'Appointment Reminders', 'url' => 'send_reminders.php']
                ]
            ],
            
            // Basic Billing
            [
                'title' => 'Basic Billing',
                'type' => 'header'
            ],
            [
                'title' => 'Payment Collection',
                'icon' => 'ph-money',
                'type' => 'dropdown',
                'id' => 'paymentMenu',
                'items' => [
                    ['title' => 'Collect Payment', 'url' => 'collect_payment.php'],
                    ['title' => 'Payment Receipts', 'url' => 'payment_receipts.php'],
                    ['title' => 'Daily Collections', 'url' => 'daily_collections.php']
                ]
            ]
        ],
        
        'Billing Clerk' => [
            // Billing Operations
            [
                'title' => 'Billing Operations',
                'type' => 'header'
            ],
            [
                'title' => 'Invoice Management',
                'icon' => 'ph-receipt',
                'type' => 'dropdown',
                'id' => 'invoiceMenu',
                'items' => [
                    ['title' => 'Create Invoice', 'url' => 'create_invoice.php'],
                    ['title' => 'All Invoices', 'url' => 'list_invoices.php'],
                    ['title' => 'Pending Invoices', 'url' => 'pending_invoices.php'],
                    ['title' => 'Overdue Invoices', 'url' => 'overdue_invoices.php']
                ]
            ],
            [
                'title' => 'Payment Processing',
                'icon' => 'ph-money',
                'type' => 'dropdown',
                'id' => 'paymentProcessingMenu',
                'items' => [
                    ['title' => 'Record Payment', 'url' => 'record_payment.php'],
                    ['title' => 'All Payments', 'url' => 'list_payments.php'],
                    ['title' => 'M-Pesa Transactions', 'url' => 'mpesa_transactions.php'],
                    ['title' => 'Payment Reconciliation', 'url' => 'payment_reconciliation.php']
                ]
            ],
            
            // Insurance Management
            [
                'title' => 'Insurance Management',
                'type' => 'header'
            ],
            [
                'title' => 'Insurance Claims',
                'icon' => 'ph-shield-check',
                'type' => 'dropdown',
                'id' => 'insuranceMenu',
                'items' => [
                    ['title' => 'Submit Claims', 'url' => 'submit_insurance_claims.php'],
                    ['title' => 'Track Claims', 'url' => 'track_insurance_claims.php'],
                    ['title' => 'Insurance Providers', 'url' => 'insurance_providers.php'],
                    ['title' => 'Pre-authorization', 'url' => 'insurance_preauth.php']
                ]
            ],
            
            // Financial Reports
            [
                'title' => 'Financial Reports',
                'type' => 'header'
            ],
            [
                'title' => 'Reports',
                'icon' => 'ph-chart-line',
                'type' => 'dropdown',
                'id' => 'financialReportsMenu',
                'items' => [
                    ['title' => 'Daily Revenue', 'url' => 'daily_revenue.php'],
                    ['title' => 'Monthly Reports', 'url' => 'monthly_reports.php'],
                    ['title' => 'Outstanding Balances', 'url' => 'outstanding_balances.php'],
                    ['title' => 'Payment Analysis', 'url' => 'payment_analysis.php']
                ]
            ]
        ],
        
        'Medical Records' => [
            // Records Management
            [
                'title' => 'Medical Records',
                'type' => 'header'
            ],
            [
                'title' => 'Patient Records',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'recordsMenu',
                'items' => [
                    ['title' => 'All Patient Records', 'url' => 'all_patient_records.php'],
                    ['title' => 'Medical History', 'url' => 'medical_history.php'],
                    ['title' => 'Document Upload', 'url' => 'document_upload.php'],
                    ['title' => 'Record Search', 'url' => 'record_search.php']
                ]
            ],
            [
                'title' => 'Document Management',
                'icon' => 'ph-folder',
                'type' => 'dropdown',
                'id' => 'documentMenu',
                'items' => [
                    ['title' => 'Medical Documents', 'url' => 'medical_documents.php'],
                    ['title' => 'Test Results', 'url' => 'test_results_archive.php'],
                    ['title' => 'Imaging Studies', 'url' => 'imaging_archive.php'],
                    ['title' => 'Reports Archive', 'url' => 'reports_archive.php']
                ]
            ],
            
            // Secure Sharing
            [
                'title' => 'Secure Sharing',
                'type' => 'header'
            ],
            [
                'title' => 'Document Sharing',
                'icon' => 'ph-share-fat',
                'type' => 'dropdown',
                'id' => 'sharingMenu',
                'items' => [
                    ['title' => 'Share Medical Data', 'url' => 'share_medical_data.php'],
                    ['title' => 'Secure Links', 'url' => 'secure_document_links.php'],
                    ['title' => 'Sharing History', 'url' => 'sharing_history.php'],
                    ['title' => 'Access Logs', 'url' => 'document_access_logs.php']
                ]
            ],
            
            // Compliance
            [
                'title' => 'Compliance',
                'type' => 'header'
            ],
            [
                'title' => 'Data Protection',
                'icon' => 'ph-lock',
                'type' => 'dropdown',
                'id' => 'complianceMenu',
                'items' => [
                    ['title' => 'Audit Trail', 'url' => 'records_audit_trail.php'],
                    ['title' => 'Data Retention', 'url' => 'data_retention.php'],
                    ['title' => 'Privacy Settings', 'url' => 'privacy_settings.php']
                ]
            ]
        ],
        
        'Patient' => [
            // My Health
            [
                'title' => 'My Health',
                'type' => 'header'
            ],
            [
                'title' => 'My Appointments',
                'icon' => 'ph-calendar',
                'type' => 'dropdown',
                'id' => 'myAppointmentsMenu',
                'items' => [
                    ['title' => 'Book Appointment', 'url' => 'patient_book_appointment.php'],
                    ['title' => 'My Appointments', 'url' => 'patient_appointments.php'],
                    ['title' => 'Appointment History', 'url' => 'patient_appointment_history.php'],
                    ['title' => 'Reschedule/Cancel', 'url' => 'patient_manage_appointments.php']
                ]
            ],
            [
                'title' => 'My Medical Records',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'myRecordsMenu',
                'items' => [
                    ['title' => 'Medical History', 'url' => 'patient_medical_history.php'],
                    ['title' => 'Test Results', 'url' => 'patient_test_results.php'],
                    ['title' => 'Prescriptions', 'url' => 'patient_prescriptions.php'],
                    ['title' => 'Visit Reports', 'url' => 'patient_visit_reports.php'],
                    ['title' => 'Download Records', 'url' => 'patient_download_records.php']
                ]
            ],
            
            // Billing & Payments
            [
                'title' => 'Billing & Payments',
                'type' => 'header'
            ],
            [
                'title' => 'My Bills',
                'icon' => 'ph-receipt',
                'type' => 'dropdown',
                'id' => 'myBillsMenu',
                'items' => [
                    ['title' => 'View Invoices', 'url' => 'patient_invoices.php'],
                    ['title' => 'Payment History', 'url' => 'patient_payment_history.php'],
                    ['title' => 'Make Payment', 'url' => 'patient_make_payment.php'],
                    ['title' => 'Outstanding Balance', 'url' => 'patient_outstanding_balance.php']
                ]
            ],
            
            // Communication
            [
                'title' => 'Communication',
                'type' => 'header'
            ],
            [
                'title' => 'Messages',
                'icon' => 'ph-chat-circle',
                'type' => 'dropdown',
                'id' => 'messagesMenu',
                'items' => [
                    ['title' => 'My Messages', 'url' => 'patient_messages.php'],
                    ['title' => 'Contact Hospital', 'url' => 'patient_contact.php'],
                    ['title' => 'Notifications', 'url' => 'patient_notifications.php']
                ]
            ],
            
            // Health Services
            [
                'title' => 'Health Services',
                'type' => 'header'
            ],
            [
                'title' => 'ENT Services',
                'icon' => 'ph-ear',
                'type' => 'dropdown',
                'id' => 'entServicesMenu',
                'items' => [
                    ['title' => 'Book Hearing Test', 'url' => 'patient_book_hearing_test.php'],
                    ['title' => 'Pre-appointment Info', 'url' => 'patient_preappointment_info.php'],
                    ['title' => 'Health Education', 'url' => 'patient_health_education.php']
                ]
            ]
        ]
    ];
    
    // Account menu (common for all roles)
    $accountMenu = [
        [
            'title' => 'My Account',
            'type' => 'header'
        ],
        [
            'title' => 'My Profile',
            'icon' => 'ph-user',
            'url' => 'profile.php',
            'type' => 'link'
        ],
        [
            'title' => 'Change Password',
            'icon' => 'ph-lock-key',
            'url' => 'change_password.php',
            'type' => 'link'
        ],
        [
            'title' => 'Notifications',
            'icon' => 'ph-bell',
            'url' => 'notifications.php',
            'type' => 'link'
        ],
        [
            'title' => 'Help & Support',
            'icon' => 'ph-question',
            'url' => 'help_support.php',
            'type' => 'link'
        ],
        [
            'title' => 'Logout',
            'icon' => 'ph-sign-out',
            'url' => 'logout.php',
            'type' => 'link',
            'class' => 'text-danger'
        ]
    ];
    
    // Combine menus
    $menu = $commonMenus;
    
    if (isset($roleMenus[$userRole])) {
        $menu = array_merge($menu, $roleMenus[$userRole]);
    }
    
    $menu = array_merge($menu, $accountMenu);
    
    return $menu;
}

/**
 * Get current page name for active menu highlighting
 */
function getCurrentPage() {
    return basename($_SERVER['PHP_SELF']);
}

/**
 * Check if menu item is active
 */
function isMenuActive($url, $currentPage = null) {
    if ($currentPage === null) {
        $currentPage = getCurrentPage();
    }
    
    return $currentPage === $url;
}

/**
 * Check if dropdown has active item
 */
function hasActiveItem($items, $currentPage = null) {
    if ($currentPage === null) {
        $currentPage = getCurrentPage();
    }
    
    foreach ($items as $item) {
        if (isset($item['url']) && $currentPage === $item['url']) {
            return true;
        }
    }
    
    return false;
}

/**
 * Get user role permissions for menu filtering
 */
function getUserPermissions($roleId, $conn) {
    $permissions = [];
    
    $sql = "SELECT mi.menu_url, rp.can_view, rp.can_create, rp.can_edit, rp.can_delete
            FROM role_permissions rp
            INNER JOIN menu_items mi ON rp.menu_id = mi.menu_id
            WHERE rp.role_id = ? AND mi.is_active = 1";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $roleId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        $permissions[$row['menu_url']] = [
            'can_view' => (bool)$row['can_view'],
            'can_create' => (bool)$row['can_create'],
            'can_edit' => (bool)$row['can_edit'],
            'can_delete' => (bool)$row['can_delete']
        ];
    }
    
    return $permissions;
}

/**
 * Filter menu items based on user permissions
 */
function filterMenuByPermissions($menu, $permissions) {
    $filteredMenu = [];
    
    foreach ($menu as $menuItem) {
        if ($menuItem['type'] === 'header') {
            // Always include headers
            $filteredMenu[] = $menuItem;
        } elseif ($menuItem['type'] === 'dropdown') {
            // Filter dropdown items
            $filteredItems = [];
            foreach ($menuItem['items'] as $item) {
                if (isset($permissions[$item['url']]) && $permissions[$item['url']]['can_view']) {
                    $filteredItems[] = $item;
                }
            }
            
            // Only include dropdown if it has accessible items
            if (!empty($filteredItems)) {
                $menuItem['items'] = $filteredItems;
                $filteredMenu[] = $menuItem;
            }
        } elseif ($menuItem['type'] === 'link') {
            // Check single menu item permission
            if (isset($permissions[$menuItem['url']]) && $permissions[$menuItem['url']]['can_view']) {
                $filteredMenu[] = $menuItem;
            }
        } else {
            // Include items without URLs (like account menu)
            $filteredMenu[] = $menuItem;
        }
    }
    
    return $filteredMenu;
}
?>