<?php
/**
 * ISCMS / KEBS menu configuration
 *
 * SINGLE SOURCE OF TRUTH FOR MENU & ROLE PAGE ASSIGNMENT
 * ======================================================
 * - Menu structure follows the ERDs: iscms_schema_core.html (organizations, users, roles,
 *   products, applications, audit_logs), iscms_schema_inspection_lab.html (inspections,
 *   findings, lab_test_requests, samples, results), iscms_schema_certs_compliance.html
 *   (certifications, permits, enforcement, recalls) and ISCMS.sql table names.
 * - The "Super Admin" role menu in $roleMenus['Super Admin'] is the canonical list of ALL
 *   assignable pages. Super Admin users always see this full menu (see layouts/sidebar.php).
 * - Other roles use menu_items + role_permissions from the database; keys here
 *   (e.g. Hospital Admin, ENT Consultant) are legacy role_name values in user_roles only.
 *   Submenus are remapped to ISCMS workflows for each key.
 * - To make new pages assignable to roles: add them to the Super Admin section below
 *   (under the appropriate header/dropdown). Then run sync_menu_from_config.php once.
 *   That script inserts/updates menu_items (SECTIONs and PAGEs) from this config so
 *   role_permissions can reference them. After sync, use Role Permissions to grant
 *   the new pages to the right roles.
 *
 * WORKFLOW
 * --------
 * 1. Add or rename pages in the Super Admin array below (under the right section).
 * 2. Run: php sync_menu_from_config.php  (or open it as Super Admin in browser).
 * 3. In Role Permissions, assign the new/updated menu items to the desired roles.
 *
 * ROLES
 * -----
 * - Super Admin: Always gets the full menu from getMenuConfig('Super Admin'). No DB lookup.
 * - All other roles: Menu built from menu_items + role_permissions (see layouts/sidebar.php).
 */

/**
 * Get menu configuration based on user role.
 * For 'Super Admin' returns the full canonical menu (all assignable pages).
 * For other role names returns role-specific menus if defined (DB role_name keys unchanged).
 */
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' => [
            // Canonical: iscms_schema_core / _inspection_lab / _certs_compliance + ISCMS.sql
            [
                'title' => 'ISCMS core registry',
                'type' => 'header'
            ],
            [
                'title' => 'Registries',
                'icon' => 'ph-buildings',
                'type' => 'dropdown',
                'id' => 'iscmsRegistriesMenu',
                'items' => [
                    ['title' => 'Organizations', 'url' => 'organizations.php'],
                    ['title' => 'Departments', 'url' => 'departments.php'],
                    ['title' => 'Users', 'url' => 'list_users.php'],
                    ['title' => 'Add user', 'url' => 'add_user.php'],
                    ['title' => 'Roles', 'url' => 'user_roles.php'],
                    ['title' => 'Role permissions', 'url' => 'role_permissions.php'],
                    ['title' => 'Permissions catalogue', 'url' => 'permissions.php'],
                    ['title' => 'Product categories', 'url' => 'product_categories.php'],
                    ['title' => 'Products', 'url' => 'products.php'],
                ]
            ],
            [
                'title' => 'Standards and fees',
                'icon' => 'ph-seal-check',
                'type' => 'dropdown',
                'id' => 'iscmsStandardsMenu',
                'items' => [
                    ['title' => 'Standard categories', 'url' => 'standard_categories.php'],
                    ['title' => 'Standards', 'url' => 'standards.php'],
                    ['title' => 'Product standards', 'url' => 'product_standards.php'],
                    ['title' => 'Certificate types', 'url' => 'certificate_types.php'],
                    ['title' => 'Fee schedules', 'url' => 'fee_schedules.php'],
                ]
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'iscmsApplicationsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                    ['title' => 'Workflow templates', 'url' => 'workflow_templates.php'],
                    ['title' => 'Application queue', 'url' => 'queue_dashboard.php'],
                    ['title' => 'Viewed queue', 'url' => 'viewed_queue.php'],
                ]
            ],
            [
                'title' => 'Certification',
                'icon' => 'ph-certificate',
                'type' => 'dropdown',
                'id' => 'iscmsCertificationMenu',
                'items' => [
                    ['title' => 'Certifications', 'url' => 'certifications.php'],
                    ['title' => 'Certification history', 'url' => 'certification_history.php'],
                    ['title' => 'Certification renewals', 'url' => 'certification_renewals.php'],
                    ['title' => 'Certificate documents', 'url' => 'certificate_documents.php'],
                ]
            ],
            [
                'title' => 'Inspections',
                'icon' => 'ph-clipboard-text',
                'type' => 'dropdown',
                'id' => 'iscmsInspectionsMenu',
                'items' => [
                    ['title' => 'Inspection checklists (templates)', 'url' => 'inspection_checklists.php'],
                    ['title' => 'Checklist items', 'url' => 'checklist_items.php'],
                    ['title' => 'Inspections', 'url' => 'inspections.php'],
                    ['title' => 'Checklist responses', 'url' => 'inspection_checklist_responses.php'],
                    ['title' => 'Inspection findings', 'url' => 'inspection_findings.php'],
                    ['title' => 'Inspection documents', 'url' => 'inspection_documents.php'],
                ]
            ],
            [
                'title' => 'Laboratory',
                'icon' => 'ph-flask',
                'type' => 'dropdown',
                'id' => 'iscmsLaboratoryMenu',
                'items' => [
                    ['title' => 'Laboratories', 'url' => 'laboratories.php'],
                    ['title' => 'Lab test requests', 'url' => 'lab_test_requests.php'],
                    ['title' => 'Lab samples', 'url' => 'lab_samples.php'],
                    ['title' => 'Lab test results', 'url' => 'lab_test_results.php'],
                    ['title' => 'Test result parameters', 'url' => 'test_result_parameters.php'],
                    ['title' => 'Lab documents', 'url' => 'lab_documents.php'],
                ]
            ],
            [
                'title' => 'Permits and shipments',
                'icon' => 'ph-truck',
                'type' => 'dropdown',
                'id' => 'iscmsPermitsMenu',
                'items' => [
                    ['title' => 'Import and export permits', 'url' => 'import_export_permits.php'],
                    ['title' => 'Shipment verifications', 'url' => 'shipment_verifications.php'],
                    ['title' => 'Permit documents', 'url' => 'permit_documents.php'],
                ]
            ],
            [
                'title' => 'Enforcement and surveillance',
                'icon' => 'ph-warning',
                'type' => 'dropdown',
                'id' => 'iscmsEnforcementMenu',
                'items' => [
                    ['title' => 'Non-conformances', 'url' => 'non_conformances.php'],
                    ['title' => 'Enforcement notices', 'url' => 'enforcement_notices.php'],
                    ['title' => 'Enforcement actions', 'url' => 'enforcement_actions.php'],
                    ['title' => 'Product recalls', 'url' => 'product_recalls.php'],
                    ['title' => 'Recall follow-ups', 'url' => 'recall_follow_ups.php'],
                    ['title' => 'Complaints', 'url' => 'complaints.php'],
                ]
            ],
            [
                'title' => 'System',
                'type' => 'header'
            ],
            [
                'title' => 'Settings and audit',
                'icon' => 'ph-gear',
                'type' => 'dropdown',
                'id' => 'iscmsSystemMenu',
                'items' => [
                    ['title' => 'System settings', 'url' => 'system_settings.php'],
                    ['title' => 'Audit logs', 'url' => 'audit_logs.php'],
                    ['title' => 'Activity log', 'url' => 'activity_log.php'],
                    ['title' => 'Backup and restore', 'url' => 'backup_restore.php'],
                    ['title' => 'Database export', 'url' => 'database_export.php'],
                ]
            ],
        ],
        'Hospital Admin' => [
            [
                'title' => 'Organization administration',
                'type' => 'header'
            ],
            [
                'title' => 'Registries',
                'icon' => 'ph-buildings',
                'type' => 'dropdown',
                'id' => 'haRegistriesMenu',
                'items' => [
                    ['title' => 'Organizations', 'url' => 'organizations.php'],
                    ['title' => 'Departments', 'url' => 'departments.php'],
                    ['title' => 'Users', 'url' => 'list_users.php'],
                    ['title' => 'Add user', 'url' => 'add_user.php'],
                    ['title' => 'Roles', 'url' => 'user_roles.php'],
                    ['title' => 'Role permissions', 'url' => 'role_permissions.php'],
                    ['title' => 'Permissions catalogue', 'url' => 'permissions.php'],
                ]
            ],
            [
                'title' => 'Standards',
                'icon' => 'ph-seal-check',
                'type' => 'dropdown',
                'id' => 'haStandardsMenu',
                'items' => [
                    ['title' => 'Standard categories', 'url' => 'standard_categories.php'],
                    ['title' => 'Standards', 'url' => 'standards.php'],
                    ['title' => 'Product standards', 'url' => 'product_standards.php'],
                ]
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'haApplicationsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                ]
            ],
            [
                'title' => 'Certification',
                'icon' => 'ph-certificate',
                'type' => 'dropdown',
                'id' => 'haCertMenu',
                'items' => [
                    ['title' => 'Certifications', 'url' => 'certifications.php'],
                    ['title' => 'Certification history', 'url' => 'certification_history.php'],
                ]
            ],
        ],
        'ENT Consultant' => [
            [
                'title' => 'Lead inspector',
                'type' => 'header'
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'liAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                    ['title' => 'Workflow templates', 'url' => 'workflow_templates.php'],
                ]
            ],
            [
                'title' => 'Inspections',
                'icon' => 'ph-clipboard-text',
                'type' => 'dropdown',
                'id' => 'liInspMenu',
                'items' => [
                    ['title' => 'Inspections', 'url' => 'inspections.php'],
                    ['title' => 'Checklist responses', 'url' => 'inspection_checklist_responses.php'],
                    ['title' => 'Inspection findings', 'url' => 'inspection_findings.php'],
                    ['title' => 'Inspection documents', 'url' => 'inspection_documents.php'],
                ]
            ],
            [
                'title' => 'Laboratory',
                'icon' => 'ph-flask',
                'type' => 'dropdown',
                'id' => 'liLabMenu',
                'items' => [
                    ['title' => 'Laboratories', 'url' => 'laboratories.php'],
                    ['title' => 'Lab test requests', 'url' => 'lab_test_requests.php'],
                    ['title' => 'Lab samples', 'url' => 'lab_samples.php'],
                    ['title' => 'Lab test results', 'url' => 'lab_test_results.php'],
                ]
            ],
        ],
        'ENT Doctor' => [
            [
                'title' => 'Inspector',
                'type' => 'header'
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'inspAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                ]
            ],
            [
                'title' => 'Inspections',
                'icon' => 'ph-clipboard-text',
                'type' => 'dropdown',
                'id' => 'inspFieldMenu',
                'items' => [
                    ['title' => 'Inspections', 'url' => 'inspections.php'],
                    ['title' => 'Inspection findings', 'url' => 'inspection_findings.php'],
                    ['title' => 'Inspection documents', 'url' => 'inspection_documents.php'],
                ]
            ],
        ],
        'Resident Doctor' => [
            [
                'title' => 'Inspector (trainee)',
                'type' => 'header'
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'trAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                ]
            ],
            [
                'title' => 'Inspections',
                'icon' => 'ph-clipboard-text',
                'type' => 'dropdown',
                'id' => 'trInspMenu',
                'items' => [
                    ['title' => 'Inspections', 'url' => 'inspections.php'],
                    ['title' => 'Checklist responses', 'url' => 'inspection_checklist_responses.php'],
                ]
            ],
        ],
        'Head Nurse' => [
            [
                'title' => 'Compliance and enforcement',
                'type' => 'header'
            ],
            [
                'title' => 'Non-conformances and enforcement',
                'icon' => 'ph-warning',
                'type' => 'dropdown',
                'id' => 'coNcMenu',
                'items' => [
                    ['title' => 'Non-conformances', 'url' => 'non_conformances.php'],
                    ['title' => 'Enforcement notices', 'url' => 'enforcement_notices.php'],
                    ['title' => 'Enforcement actions', 'url' => 'enforcement_actions.php'],
                ]
            ],
            [
                'title' => 'Recalls and complaints',
                'icon' => 'ph-broadcast',
                'type' => 'dropdown',
                'id' => 'coRecallMenu',
                'items' => [
                    ['title' => 'Product recalls', 'url' => 'product_recalls.php'],
                    ['title' => 'Recall follow-ups', 'url' => 'recall_follow_ups.php'],
                    ['title' => 'Complaints', 'url' => 'complaints.php'],
                ]
            ],
        ],
        'ENT Nurse' => [
            [
                'title' => 'Intake and records',
                'type' => 'header'
            ],
            [
                'title' => 'Applications desk',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'aoAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                    ['title' => 'Workflow templates', 'url' => 'workflow_templates.php'],
                    ['title' => 'Application queue', 'url' => 'queue_dashboard.php'],
                    ['title' => 'Viewed queue', 'url' => 'viewed_queue.php'],
                ]
            ],
            [
                'title' => 'Documents',
                'icon' => 'ph-folder',
                'type' => 'dropdown',
                'id' => 'aoDocsMenu',
                'items' => [
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Certificate documents', 'url' => 'certificate_documents.php'],
                    ['title' => 'Permit documents', 'url' => 'permit_documents.php'],
                ]
            ],
        ],
        'Audiologist' => [
            [
                'title' => 'Laboratory analyst',
                'type' => 'header'
            ],
            [
                'title' => 'Laboratory',
                'icon' => 'ph-flask',
                'type' => 'dropdown',
                'id' => 'laLabMenu',
                'items' => [
                    ['title' => 'Laboratories', 'url' => 'laboratories.php'],
                    ['title' => 'Lab test requests', 'url' => 'lab_test_requests.php'],
                    ['title' => 'Lab samples', 'url' => 'lab_samples.php'],
                    ['title' => 'Lab test results', 'url' => 'lab_test_results.php'],
                    ['title' => 'Test result parameters', 'url' => 'test_result_parameters.php'],
                    ['title' => 'Lab documents', 'url' => 'lab_documents.php'],
                ]
            ],
        ],
        'Lab Technician' => [
            [
                'title' => 'Laboratory',
                'type' => 'header'
            ],
            [
                'title' => 'Lab workflow',
                'icon' => 'ph-test-tube',
                'type' => 'dropdown',
                'id' => 'ltReqMenu',
                'items' => [
                    ['title' => 'Lab test requests', 'url' => 'lab_test_requests.php'],
                    ['title' => 'Lab samples', 'url' => 'lab_samples.php'],
                    ['title' => 'Lab test results', 'url' => 'lab_test_results.php'],
                    ['title' => 'Test result parameters', 'url' => 'test_result_parameters.php'],
                    ['title' => 'Lab documents', 'url' => 'lab_documents.php'],
                ]
            ],
        ],
        'Front Office' => [
            [
                'title' => 'Reception and queue',
                'type' => 'header'
            ],
            [
                'title' => 'Queue',
                'icon' => 'ph-rows',
                'type' => 'dropdown',
                'id' => 'foQueueMenu',
                'items' => [
                    ['title' => 'Application queue', 'url' => 'queue_dashboard.php'],
                    ['title' => 'Viewed queue', 'url' => 'viewed_queue.php'],
                ]
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'foAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                ]
            ],
        ],
        'Billing Clerk' => [
            [
                'title' => 'Fees',
                'type' => 'header'
            ],
            [
                'title' => 'Fee schedules',
                'icon' => 'ph-currency-dollar',
                'type' => 'dropdown',
                'id' => 'bcFeesMenu',
                'items' => [
                    ['title' => 'Fee schedules', 'url' => 'fee_schedules.php'],
                ]
            ],
        ],
        'Medical Records' => [
            [
                'title' => 'Records and documents',
                'type' => 'header'
            ],
            [
                'title' => 'Documents',
                'icon' => 'ph-folder',
                'type' => 'dropdown',
                'id' => 'mrDocsMenu',
                'items' => [
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Certificate documents', 'url' => 'certificate_documents.php'],
                    ['title' => 'Inspection documents', 'url' => 'inspection_documents.php'],
                    ['title' => 'Lab documents', 'url' => 'lab_documents.php'],
                    ['title' => 'Permit documents', 'url' => 'permit_documents.php'],
                ]
            ],
            [
                'title' => 'Audit',
                'icon' => 'ph-list-checks',
                'type' => 'dropdown',
                'id' => 'mrAuditMenu',
                'items' => [
                    ['title' => 'Audit logs', 'url' => 'audit_logs.php'],
                ]
            ],
        ],
        'Pharmacist' => [
            [
                'title' => 'Permits and products',
                'type' => 'header'
            ],
            [
                'title' => 'Trade compliance',
                'icon' => 'ph-truck',
                'type' => 'dropdown',
                'id' => 'phPermMenu',
                'items' => [
                    ['title' => 'Import and export permits', 'url' => 'import_export_permits.php'],
                    ['title' => 'Shipment verifications', 'url' => 'shipment_verifications.php'],
                    ['title' => 'Permit documents', 'url' => 'permit_documents.php'],
                ]
            ],
            [
                'title' => 'Products',
                'icon' => 'ph-package',
                'type' => 'dropdown',
                'id' => 'phProdMenu',
                'items' => [
                    ['title' => 'Products', 'url' => 'products.php'],
                    ['title' => 'Product categories', 'url' => 'product_categories.php'],
                ]
            ],
        ],
        'Chief Pharmacist' => [
            [
                'title' => 'Permits and products',
                'type' => 'header'
            ],
            [
                'title' => 'Trade compliance',
                'icon' => 'ph-truck',
                'type' => 'dropdown',
                'id' => 'cpPermMenu',
                'items' => [
                    ['title' => 'Import and export permits', 'url' => 'import_export_permits.php'],
                    ['title' => 'Shipment verifications', 'url' => 'shipment_verifications.php'],
                    ['title' => 'Permit documents', 'url' => 'permit_documents.php'],
                ]
            ],
            [
                'title' => 'Products and standards',
                'icon' => 'ph-seal-check',
                'type' => 'dropdown',
                'id' => 'cpProdMenu',
                'items' => [
                    ['title' => 'Products', 'url' => 'products.php'],
                    ['title' => 'Product standards', 'url' => 'product_standards.php'],
                ]
            ],
        ],
        'Locum Pharmacist' => [
            [
                'title' => 'Permits (limited)',
                'type' => 'header'
            ],
            [
                'title' => 'Permits',
                'icon' => 'ph-truck',
                'type' => 'dropdown',
                'id' => 'lpPermMenu',
                'items' => [
                    ['title' => 'Import and export permits', 'url' => 'import_export_permits.php'],
                    ['title' => 'Shipment verifications', 'url' => 'shipment_verifications.php'],
                ]
            ],
        ],
        'Locum Doctor' => [
            [
                'title' => 'Inspector (locum)',
                'type' => 'header'
            ],
            [
                'title' => 'Applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'ldAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                ]
            ],
            [
                'title' => 'Inspections',
                'icon' => 'ph-clipboard-text',
                'type' => 'dropdown',
                'id' => 'ldInspMenu',
                'items' => [
                    ['title' => 'Inspections', 'url' => 'inspections.php'],
                    ['title' => 'Inspection findings', 'url' => 'inspection_findings.php'],
                ]
            ],
        ],
        'Locum Nurse' => [
            [
                'title' => 'Intake (locum)',
                'type' => 'header'
            ],
            [
                'title' => 'Applications desk',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'lnAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                    ['title' => 'Status history', 'url' => 'application_status_history.php'],
                    ['title' => 'Comments', 'url' => 'application_comments.php'],
                    ['title' => 'Workflow templates', 'url' => 'workflow_templates.php'],
                ]
            ],
        ],
        'Patient' => [
            [
                'title' => 'Applicant portal',
                'type' => 'header'
            ],
            [
                'title' => 'My applications',
                'icon' => 'ph-file-text',
                'type' => 'dropdown',
                'id' => 'ptAppsMenu',
                'items' => [
                    ['title' => 'Applications', 'url' => 'applications.php'],
                    ['title' => 'Application documents', 'url' => 'application_documents.php'],
                ]
            ],
            [
                'title' => 'Feedback',
                'icon' => 'ph-chat-circle',
                'type' => 'dropdown',
                'id' => 'ptComplMenu',
                'items' => [
                    ['title' => 'Complaints', 'url' => 'complaints.php'],
                ]
            ],
        ],

    // Account menu (common for all roles)
    $accountMenu = [
        [
            'title' => 'My Account',
            'type' => 'header'
        ],
        ['title' => 'Messaging', 'icon' => 'ri-chat-3-line', 'url' => 'messaging.php', 'type' => 'link'],
        [
            '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;
}

/**
 * Returns a flat list of all assignable pages from the Super Admin menu.
 * Each element: ['section' => string, 'title' => string, 'url' => string, 'icon' => string]
 * Use for sync_menu_from_config, or to display "all available pages" in Role Permissions.
 */
function getSuperAdminMenuFlattened() {
    $menu = getMenuConfig('Super Admin');
    $flat = [];
    $currentSection = '';
    foreach ($menu as $item) {
        $type = $item['type'] ?? '';
        $title = $item['title'] ?? '';
        if ($type === 'header') {
            $currentSection = $title;
            continue;
        }
        if ($type === 'link') {
            $url = $item['url'] ?? '';
            if ($url && !in_array($title, ['Dashboard', 'Logout', 'My Profile', 'Change Password', 'Notifications', 'Help & Support'], true)) {
                $flat[] = [
                    'section' => $currentSection,
                    'title'   => $title,
                    'url'     => $url,
                    'icon'    => $item['icon'] ?? 'ph-file'
                ];
            }
            continue;
        }
        if ($type === 'dropdown' && isset($item['items'])) {
            $icon = $item['icon'] ?? 'ph-folder';
            foreach ($item['items'] as $sub) {
                $url = $sub['url'] ?? '';
                $subTitle = $sub['title'] ?? $url;
                if ($url && $subTitle) {
                    $flat[] = [
                        'section' => $currentSection,
                        'title'   => $subTitle,
                        'url'     => $url,
                        'icon'    => $icon
                    ];
                }
            }
        }
    }
    return $flat;
}

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

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

/**
 * Check if dropdown has active item
 */
if (!function_exists('hasActiveItem')) {
    function hasActiveItem($items, $currentPage = null) {
        if ($currentPage === null) {
            $currentPage = getCurrentPage();
        }
        foreach ($items as $item) {
            if (isset($item['url']) && basename($item['url']) === $currentPage) {
                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') {
            $filteredMenu[] = $menuItem;
        } elseif ($menuItem['type'] === 'dropdown') {
            $filteredItems = [];
            foreach ($menuItem['items'] as $item) {
                if (isset($permissions[$item['url']]) && $permissions[$item['url']]['can_view']) {
                    $filteredItems[] = $item;
                }
            }
            if (!empty($filteredItems)) {
                $menuItem['items'] = $filteredItems;
                $filteredMenu[] = $menuItem;
            }
        } elseif ($menuItem['type'] === 'link') {
            if (isset($permissions[$menuItem['url']]) && $permissions[$menuItem['url']]['can_view']) {
                $filteredMenu[] = $menuItem;
            }
        } else {
            $filteredMenu[] = $menuItem;
        }
    }
    
    return $filteredMenu;
}
?>