<?php
/**
 * Kenyan Pension & Life Insurance Management System
 * Role-Based Menu Configuration
 * 
 * @package Includes
 * @author Senior Fintech Engineer
 */

/**
 * Get menu configuration for Kenyan Pension & Life Insurance System
 * 
 * @param string $userRole User role name
 * @return array Menu configuration array
 */
function getPensionMenuConfig($userRole = 'guest') {
    // Normalize role name
    $userRole = strtoupper(trim($userRole));
    
    // Common menu items for all logged-in users
    $commonMenus = [
        [
            'title' => 'Main Menu',
            'type' => 'header'
        ],
        [
            'title' => 'Dashboard',
            'icon' => 'ri-dashboard-line',
            'url' => 'index.php',
            'type' => 'link'
        ]
    ];
    
    // Role-specific menu configurations
    $roleMenus = [];
    
    // ============================================
    // ADMIN / SUPER ADMIN MENU
    // ============================================
    $roleMenus['ADMIN'] = [
        // Member Management
        [
            'title' => 'Member Management',
            'type' => 'header'
        ],
        [
            'title' => 'Members',
            'icon' => 'ri-user-line',
            'type' => 'dropdown',
            'id' => 'membersMenu',
            'items' => [
                ['title' => 'All Members', 'url' => 'members.php'],
                ['title' => 'Add Member', 'url' => 'members.php?action=create'],
                ['title' => 'Member Search', 'url' => 'members.php?search=1'],
                ['title' => 'Member Statements', 'url' => 'member_statements.php'],
                ['title' => 'Export Members', 'url' => 'export_members.php']
            ]
        ],
        [
            'title' => 'Beneficiaries',
            'icon' => 'ri-group-line',
            'type' => 'dropdown',
            'id' => 'beneficiariesMenu',
            'items' => [
                ['title' => 'All Beneficiaries', 'url' => 'beneficiaries.php'],
                ['title' => 'Manage Beneficiaries', 'url' => 'manage_beneficiaries.php']
            ]
        ],
        
        // Pension Scheme Management
        [
            'title' => 'Pension Schemes',
            'type' => 'header'
        ],
        [
            'title' => 'Schemes',
            'icon' => 'ri-building-line',
            'type' => 'dropdown',
            'id' => 'schemesMenu',
            'items' => [
                ['title' => 'All Schemes', 'url' => 'schemes.php'],
                ['title' => 'Add Scheme', 'url' => 'schemes.php?action=create'],
                ['title' => 'Scheme Rules', 'url' => 'scheme_rules.php'],
                ['title' => 'Scheme Reports', 'url' => 'scheme_reports.php']
            ]
        ],
        [
            'title' => 'Employers',
            'icon' => 'ri-building-2-line',
            'type' => 'dropdown',
            'id' => 'employersMenu',
            'items' => [
                ['title' => 'All Employers', 'url' => 'employers.php'],
                ['title' => 'Add Employer', 'url' => 'employers.php?action=create'],
                ['title' => 'Employer Remittances', 'url' => 'employer_remittances.php']
            ]
        ],
        [
            'title' => 'Trustees',
            'icon' => 'ri-shield-user-line',
            'type' => 'dropdown',
            'id' => 'trusteesMenu',
            'items' => [
                ['title' => 'All Trustees', 'url' => 'trustees.php'],
                ['title' => 'Add Trustee', 'url' => 'trustees.php?action=create'],
                ['title' => 'Trustee Assignments', 'url' => 'trustee_assignments.php']
            ]
        ],
        
        // Contributions & Fund Management
        [
            'title' => 'Contributions & Funds',
            'type' => 'header'
        ],
        [
            'title' => 'Contributions',
            'icon' => 'ri-file-add-line',
            'type' => 'dropdown',
            'id' => 'contributionsMenu',
            'items' => [
                ['title' => 'All Contributions', 'url' => 'contributions.php'],
                ['title' => 'Post Contribution', 'url' => 'contributions.php?action=create'],
                ['title' => 'Bulk Upload', 'url' => 'bulk_upload_contributions.php'],
                ['title' => 'Pending Contributions', 'url' => 'contributions.php?status=PENDING'],
                ['title' => 'Contribution Reports', 'url' => 'contribution_reports.php']
            ]
        ],
        [
            'title' => 'Fund Returns',
            'icon' => 'ri-line-chart-line',
            'type' => 'dropdown',
            'id' => 'fundReturnsMenu',
            'items' => [
                ['title' => 'All Returns', 'url' => 'fund_returns.php'],
                ['title' => 'Allocate Returns', 'url' => 'allocate_fund_returns.php'],
                ['title' => 'Return Reports', 'url' => 'fund_return_reports.php']
            ]
        ],
        [
            'title' => 'Fund Balances',
            'icon' => 'ri-wallet-line',
            'type' => 'dropdown',
            'id' => 'fundBalancesMenu',
            'items' => [
                ['title' => 'Member Balances', 'url' => 'member_balances.php'],
                ['title' => 'Scheme Balances', 'url' => 'scheme_balances.php'],
                ['title' => 'Balance Reconciliation', 'url' => 'balance_reconciliation.php']
            ]
        ],
        
        // Withdrawals & Benefits
        [
            'title' => 'Withdrawals & Benefits',
            'type' => 'header'
        ],
        [
            'title' => 'Withdrawals',
            'icon' => 'ri-money-dollar-circle-line',
            'type' => 'dropdown',
            'id' => 'withdrawalsMenu',
            'items' => [
                ['title' => 'All Withdrawals', 'url' => 'withdrawals.php'],
                ['title' => 'Pending Withdrawals', 'url' => 'withdrawals.php?status=PENDING'],
                ['title' => 'Process Withdrawal', 'url' => 'process_withdrawal.php'],
                ['title' => 'Withdrawal Reports', 'url' => 'withdrawal_reports.php']
            ]
        ],
        [
            'title' => 'Retirement Benefits',
            'icon' => 'ri-calendar-check-line',
            'type' => 'dropdown',
            'id' => 'retirementMenu',
            'items' => [
                ['title' => 'Retirement Requests', 'url' => 'retirement_requests.php'],
                ['title' => 'Process Retirement', 'url' => 'process_retirement.php'],
                ['title' => 'Annuity Management', 'url' => 'annuity_management.php']
            ]
        ],
        
        // Life Insurance
        [
            'title' => 'Life Insurance',
            'type' => 'header'
        ],
        [
            'title' => 'Policies',
            'icon' => 'ri-file-shield-line',
            'type' => 'dropdown',
            'id' => 'policiesMenu',
            'items' => [
                ['title' => 'All Policies', 'url' => 'policies.php'],
                ['title' => 'Add Policy', 'url' => 'policies.php?action=create'],
                ['title' => 'Policy Products', 'url' => 'policy_products.php'],
                ['title' => 'Policy Rules', 'url' => 'policy_rules.php'],
                ['title' => 'Lapsed Policies', 'url' => 'policies.php?status=LAPSED']
            ]
        ],
        [
            'title' => 'Premiums',
            'icon' => 'ri-bank-card-line',
            'type' => 'dropdown',
            'id' => 'premiumsMenu',
            'items' => [
                ['title' => 'All Premiums', 'url' => 'premiums.php'],
                ['title' => 'Post Premium', 'url' => 'premiums.php?action=create'],
                ['title' => 'Overdue Premiums', 'url' => 'premiums.php?status=OVERDUE'],
                ['title' => 'Premium Reports', 'url' => 'premium_reports.php']
            ]
        ],
        [
            'title' => 'Claims',
            'icon' => 'ri-file-list-3-line',
            'type' => 'dropdown',
            'id' => 'claimsMenu',
            'items' => [
                ['title' => 'All Claims', 'url' => 'claims.php'],
                ['title' => 'Pending Claims', 'url' => 'claims.php?status=PENDING'],
                ['title' => 'Death Claims', 'url' => 'claims.php?type=DEATH'],
                ['title' => 'Maturity Claims', 'url' => 'claims.php?type=MATURITY'],
                ['title' => 'Surrender Requests', 'url' => 'surrender_requests.php'],
                ['title' => 'Process Claim', 'url' => 'process_claim.php']
            ]
        ],
        [
            'title' => 'Bonuses',
            'icon' => 'ri-gift-line',
            'type' => 'dropdown',
            'id' => 'bonusesMenu',
            'items' => [
                ['title' => 'All Bonuses', 'url' => 'bonuses.php'],
                ['title' => 'Allocate Bonus', 'url' => 'allocate_bonus.php'],
                ['title' => 'Bonus Reports', 'url' => 'bonus_reports.php']
            ]
        ],
        
        // Payments & Payouts
        [
            'title' => 'Payments & Payouts',
            'type' => 'header'
        ],
        [
            'title' => 'Payouts',
            'icon' => 'ri-secure-payment-line',
            'type' => 'dropdown',
            'id' => 'payoutsMenu',
            'items' => [
                ['title' => 'All Payouts', 'url' => 'payouts.php'],
                ['title' => 'Pending Payouts', 'url' => 'payouts.php?status=PENDING'],
                ['title' => 'Process Payout', 'url' => 'process_payout.php'],
                ['title' => 'M-Pesa Payments', 'url' => 'mpesa_payments.php'],
                ['title' => 'Bank Transfers', 'url' => 'bank_transfers.php']
            ]
        ],
        
        // Reporting & Compliance
        [
            'title' => 'Reporting & Compliance',
            'type' => 'header'
        ],
        [
            'title' => 'Reports',
            'icon' => 'ri-file-chart-line',
            'type' => 'dropdown',
            'id' => 'reportsMenu',
            'items' => [
                ['title' => 'Member Statements', 'url' => 'member_statements.php'],
                ['title' => 'Scheme Reports', 'url' => 'scheme_reports.php'],
                ['title' => 'Financial Reports', 'url' => 'financial_reports.php'],
                ['title' => 'RBA Reports', 'url' => 'rba_reports.php'],
                ['title' => 'Audit Reports', 'url' => 'audit_reports.php']
            ]
        ],
        
        // System Administration
        [
            'title' => 'System Administration',
            'type' => 'header'
        ],
        [
            'title' => 'Users & Roles',
            'icon' => 'ri-group-line',
            'type' => 'dropdown',
            'id' => 'usersMenu',
            'items' => [
                ['title' => 'All Users', 'url' => 'users.php'],
                ['title' => 'Add User', 'url' => 'users.php?action=create'],
                ['title' => 'Roles & Permissions', 'url' => 'roles.php'],
                ['title' => 'User Activity', 'url' => 'user_activity.php']
            ]
        ],
        [
            'title' => 'Audit Logs',
            'icon' => 'ri-file-list-line',
            'url' => 'audit_logs.php',
            'type' => 'link'
        ],
        [
            'title' => 'System Settings',
            'icon' => 'ri-settings-3-line',
            'url' => 'settings.php',
            'type' => 'link'
        ],
        [
            'title' => 'AI Features',
            'icon' => 'ri-robot-line',
            'type' => 'dropdown',
            'id' => 'aiMenu',
            'items' => [
                ['title' => 'AI Assistant', 'url' => 'ai_chatbot.php'],
                ['title' => 'Lapse Prediction', 'url' => 'ai_lapse_prediction.php']
            ]
        ],
        [
            'title' => 'eTIMS Integration',
            'icon' => 'ri-file-invoice-line',
            'url' => 'etims_integration.php',
            'type' => 'link'
        ]
    ];
    
    // ============================================
    // TRUSTEE MENU
    // ============================================
    $roleMenus['TRUSTEE'] = [
        // Member Management
        [
            'title' => 'Member Management',
            'type' => 'header'
        ],
        [
            'title' => 'Members',
            'icon' => 'ri-user-line',
            'type' => 'dropdown',
            'id' => 'membersMenu',
            'items' => [
                ['title' => 'All Members', 'url' => 'members.php'],
                ['title' => 'Member Statements', 'url' => 'member_statements.php']
            ]
        ],
        
        // Scheme Management
        [
            'title' => 'Scheme Management',
            'type' => 'header'
        ],
        [
            'title' => 'My Schemes',
            'icon' => 'ri-building-line',
            'url' => 'schemes.php',
            'type' => 'link'
        ],
        [
            'title' => 'Scheme Reports',
            'icon' => 'ri-file-chart-line',
            'url' => 'scheme_reports.php',
            'type' => 'link'
        ],
        
        // Approvals
        [
            'title' => 'Approvals',
            'type' => 'header'
        ],
        [
            'title' => 'Withdrawal Approvals',
            'icon' => 'ri-checkbox-circle-line',
            'type' => 'dropdown',
            'id' => 'approvalsMenu',
            'items' => [
                ['title' => 'Pending Withdrawals', 'url' => 'withdrawals.php?status=PENDING'],
                ['title' => 'Retirement Approvals', 'url' => 'retirement_requests.php?status=PENDING']
            ]
        ],
        
        // Reporting
        [
            'title' => 'Reporting',
            'type' => 'header'
        ],
        [
            'title' => 'Reports',
            'icon' => 'ri-file-chart-line',
            'type' => 'dropdown',
            'id' => 'reportsMenu',
            'items' => [
                ['title' => 'Scheme Reports', 'url' => 'scheme_reports.php'],
                ['title' => 'Financial Reports', 'url' => 'financial_reports.php'],
                ['title' => 'RBA Reports', 'url' => 'rba_reports.php']
            ]
        ]
    ];
    
    // ============================================
    // EMPLOYER MENU
    // ============================================
    $roleMenus['EMPLOYER'] = [
        // Member Management
        [
            'title' => 'Member Management',
            'type' => 'header'
        ],
        [
            'title' => 'My Employees',
            'icon' => 'ri-user-line',
            'url' => 'members.php',
            'type' => 'link'
        ],
        
        // Contributions
        [
            'title' => 'Contributions',
            'type' => 'header'
        ],
        [
            'title' => 'Contributions',
            'icon' => 'ri-file-add-line',
            'type' => 'dropdown',
            'id' => 'contributionsMenu',
            'items' => [
                ['title' => 'All Contributions', 'url' => 'contributions.php'],
                ['title' => 'Post Contribution', 'url' => 'contributions.php?action=create'],
                ['title' => 'Bulk Upload', 'url' => 'bulk_upload_contributions.php'],
                ['title' => 'Contribution Schedule', 'url' => 'contribution_schedule.php']
            ]
        ],
        [
            'title' => 'Remittances',
            'icon' => 'ri-bank-line',
            'type' => 'dropdown',
            'id' => 'remittancesMenu',
            'items' => [
                ['title' => 'All Remittances', 'url' => 'remittances.php'],
                ['title' => 'Make Remittance', 'url' => 'remittances.php?action=create'],
                ['title' => 'Remittance History', 'url' => 'remittance_history.php']
            ]
        ],
        
        // Reports
        [
            'title' => 'Reports',
            'type' => 'header'
        ],
        [
            'title' => 'Employer Reports',
            'icon' => 'ri-file-chart-line',
            'type' => 'dropdown',
            'id' => 'reportsMenu',
            'items' => [
                ['title' => 'Contribution Reports', 'url' => 'contribution_reports.php'],
                ['title' => 'Employee Statements', 'url' => 'employee_statements.php'],
                ['title' => 'Remittance Reports', 'url' => 'remittance_reports.php']
            ]
        ]
    ];
    
    // ============================================
    // MEMBER MENU
    // ============================================
    $roleMenus['MEMBER'] = [
        // My Account
        [
            'title' => 'My Account',
            'type' => 'header'
        ],
        [
            'title' => 'My Dashboard',
            'icon' => 'ri-dashboard-line',
            'url' => 'member_portal.php',
            'type' => 'link'
        ],
        [
            'title' => 'My Profile',
            'icon' => 'ri-user-line',
            'url' => 'member_profile.php',
            'type' => 'link'
        ],
        
        // Pension Account
        [
            'title' => 'Pension Account',
            'type' => 'header'
        ],
        [
            'title' => 'Account Balance',
            'icon' => 'ri-wallet-line',
            'url' => 'member_portal.php#balance',
            'type' => 'link'
        ],
        [
            'title' => 'Contribution History',
            'icon' => 'ri-file-list-line',
            'url' => 'member_contributions.php',
            'type' => 'link'
        ],
        [
            'title' => 'Statement',
            'icon' => 'ri-file-text-line',
            'url' => 'member_statement.php',
            'type' => 'link'
        ],
        
        // Withdrawals
        [
            'title' => 'Withdrawals',
            'type' => 'header'
        ],
        [
            'title' => 'Request Withdrawal',
            'icon' => 'ri-money-dollar-circle-line',
            'url' => 'request_withdrawal.php',
            'type' => 'link'
        ],
        [
            'title' => 'My Withdrawals',
            'icon' => 'ri-history-line',
            'url' => 'my_withdrawals.php',
            'type' => 'link'
        ],
        
        // Life Insurance
        [
            'title' => 'Life Insurance',
            'type' => 'header'
        ],
        [
            'title' => 'My Policies',
            'icon' => 'ri-file-shield-line',
            'url' => 'my_policies.php',
            'type' => 'link'
        ],
        [
            'title' => 'Premium Payments',
            'icon' => 'ri-bank-card-line',
            'url' => 'my_premiums.php',
            'type' => 'link'
        ],
        [
            'title' => 'File Claim',
            'icon' => 'ri-file-list-3-line',
            'url' => 'file_claim.php',
            'type' => 'link'
        ],
        
        // Beneficiaries
        [
            'title' => 'Beneficiaries',
            'type' => 'header'
        ],
        [
            'title' => 'My Beneficiaries',
            'icon' => 'ri-group-line',
            'url' => 'my_beneficiaries.php',
            'type' => 'link'
        ],
        [
            'title' => 'Add Beneficiary',
            'icon' => 'ri-user-add-line',
            'url' => 'add_beneficiary.php',
            'type' => 'link'
        ],
        
        // AI Assistant
        [
            'title' => 'AI Assistant',
            'type' => 'header'
        ],
        [
            'title' => 'AI Chatbot',
            'icon' => 'ri-robot-line',
            'url' => 'ai_chatbot.php',
            'type' => 'link'
        ]
    ];
    
    // ============================================
    // INSURANCE OFFICER MENU
    // ============================================
    $roleMenus['INSURANCE OFFICER'] = [
        // Policies
        [
            'title' => 'Policy Management',
            'type' => 'header'
        ],
        [
            'title' => 'Policies',
            'icon' => 'ri-file-shield-line',
            'type' => 'dropdown',
            'id' => 'policiesMenu',
            'items' => [
                ['title' => 'All Policies', 'url' => 'policies.php'],
                ['title' => 'Add Policy', 'url' => 'policies.php?action=create'],
                ['title' => 'Lapsed Policies', 'url' => 'policies.php?status=LAPSED']
            ]
        ],
        [
            'title' => 'Policy Products',
            'icon' => 'ri-product-hunt-line',
            'url' => 'policy_products.php',
            'type' => 'link'
        ],
        
        // Premiums
        [
            'title' => 'Premium Management',
            'type' => 'header'
        ],
        [
            'title' => 'Premiums',
            'icon' => 'ri-bank-card-line',
            'type' => 'dropdown',
            'id' => 'premiumsMenu',
            'items' => [
                ['title' => 'All Premiums', 'url' => 'premiums.php'],
                ['title' => 'Post Premium', 'url' => 'premiums.php?action=create'],
                ['title' => 'Overdue Premiums', 'url' => 'premiums.php?status=OVERDUE']
            ]
        ],
        
        // Claims
        [
            'title' => 'Claims Management',
            'type' => 'header'
        ],
        [
            'title' => 'Claims',
            'icon' => 'ri-file-list-3-line',
            'type' => 'dropdown',
            'id' => 'claimsMenu',
            'items' => [
                ['title' => 'All Claims', 'url' => 'claims.php'],
                ['title' => 'Pending Claims', 'url' => 'claims.php?status=PENDING'],
                ['title' => 'Process Claim', 'url' => 'process_claim.php'],
                ['title' => 'Surrender Requests', 'url' => 'surrender_requests.php']
            ]
        ],
        
        // Reports
        [
            'title' => 'Reports',
            'type' => 'header'
        ],
        [
            'title' => 'Insurance Reports',
            'icon' => 'ri-file-chart-line',
            'type' => 'dropdown',
            'id' => 'reportsMenu',
            'items' => [
                ['title' => 'Policy Reports', 'url' => 'policy_reports.php'],
                ['title' => 'Premium Reports', 'url' => 'premium_reports.php'],
                ['title' => 'Claims Reports', 'url' => 'claims_reports.php']
            ]
        ]
    ];
    
    // ============================================
    // AGENT MENU
    // ============================================
    $roleMenus['AGENT'] = [
        // Policies
        [
            'title' => 'Policy Management',
            'type' => 'header'
        ],
        [
            'title' => 'My Policies',
            'icon' => 'ri-file-shield-line',
            'url' => 'agent_policies.php',
            'type' => 'link'
        ],
        [
            'title' => 'New Policy',
            'icon' => 'ri-file-add-line',
            'url' => 'policies.php?action=create',
            'type' => 'link'
        ],
        
        // Clients
        [
            'title' => 'Client Management',
            'type' => 'header'
        ],
        [
            'title' => 'My Clients',
            'icon' => 'ri-user-line',
            'url' => 'agent_clients.php',
            'type' => 'link'
        ],
        
        // Commissions
        [
            'title' => 'Commissions',
            'type' => 'header'
        ],
        [
            'title' => 'My Commissions',
            'icon' => 'ri-money-dollar-circle-line',
            'url' => 'agent_commissions.php',
            'type' => 'link'
        ],
        
        // Reports
        [
            'title' => 'Reports',
            'type' => 'header'
        ],
        [
            'title' => 'Agent Reports',
            'icon' => 'ri-file-chart-line',
            'url' => 'agent_reports.php',
            'type' => 'link'
        ]
    ];
    
    // ============================================
    // MY ACCOUNT (Common for all roles)
    // ============================================
    $myAccountMenu = [
        [
            'title' => 'My Account',
            'type' => 'header'
        ],
        [
            'title' => 'My Profile',
            'icon' => 'ri-user-line',
            'url' => 'profile.php',
            'type' => 'link'
        ],
        [
            'title' => 'Change Password',
            'icon' => 'ri-lock-password-line',
            'url' => 'change_password.php',
            'type' => 'link'
        ],
        [
            'title' => 'Logout',
            'icon' => 'ri-logout-box-line',
            'url' => 'logout.php',
            'type' => 'link',
            'class' => 'text-danger'
        ]
    ];
    
    // Combine menus based on role
    if ($userRole === 'ADMIN' || $userRole === 'SUPER ADMIN' || $userRole === 'SUPERADMIN') {
        $menu = array_merge($commonMenus, $roleMenus['ADMIN'], $myAccountMenu);
    } elseif (isset($roleMenus[$userRole])) {
        $menu = array_merge($commonMenus, $roleMenus[$userRole], $myAccountMenu);
    } else {
        // Default/guest menu
        $menu = array_merge($commonMenus, $myAccountMenu);
    }
    
    return $menu;
}
