<?php
if (session_status() !== PHP_SESSION_ACTIVE) @session_start();

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php')) {
    require_once BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php';
}

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/easychair_translate.php')) {
    require_once BASE_PATH . '/app/helpers/easychair_translate.php';
}
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/permissions.php')) {
    require_once BASE_PATH . '/app/helpers/permissions.php';
}

if (!function_exists('ec_role_layout_role_code')) {
    function ec_role_layout_role_code(): string {
        return (string)($_SESSION['conference_role'] ?? $_SESSION['role'] ?? $_SESSION['user_role'] ?? '');
    }
}
if (!function_exists('ec_role_layout_t')) {
    function ec_role_layout_t(string $key, string $fallback): string {
        return function_exists('ec_t') ? ec_t($key, $fallback) : $fallback;
    }
}
if (!function_exists('ec_role_layout_role_label')) {
    function ec_role_layout_role_label(string $role): string {
        return function_exists('ec_role_t') ? ec_role_t($role) : ucwords(str_replace('_',' ', $role));
    }
}
if (!function_exists('ec_role_layout_all_groups')) {
    function ec_role_layout_all_groups(): array {
        return [
            ['group'=>'core','label_key'=>'menu.core','label'=>'Core','items'=>[
                ['url'=>'/easychair/admin/dashboard','icon'=>'fa-gauge-high','label_key'=>'menu.dashboard','label'=>'Dashboard','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','organizer','section_editor']],
                ['url'=>'/easychair/admin/online-users','icon'=>'fa-users-viewfinder','label_key'=>'menu.online_users','label'=>'Online Users','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/user-scope-audit','icon'=>'fa-users-gear','label_key'=>'menu.user_scope_audit','label'=>'User Scope Audit','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/access-audit','icon'=>'fa-shield-halved','label_key'=>'menu.access_audit','label'=>'Access Audit','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/scope-debug','icon'=>'fa-bug','label_key'=>'menu.scope_debug','label'=>'Scope Debug','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
            ]],
            ['group'=>'setup','label_key'=>'menu.conference_setup','label'=>'Conference Setup','items'=>[
                ['url'=>'/easychair/admin/conferences','icon'=>'fa-folder-open','label_key'=>'menu.conferences','label'=>'Conferences','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/organizations','icon'=>'fa-building','label_key'=>'menu.organizations','label'=>'Organizations','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/conference-home','icon'=>'fa-palette','label_key'=>'menu.smart_homepage','label'=>'Smart Homepage','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
                ['url'=>'/easychair/admin/branding','icon'=>'fa-image','label_key'=>'menu.branding','label'=>'Branding & Logos','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
                ['url'=>'/easychair/admin/committee-images','icon'=>'fa-images','label_key'=>'menu.committee_images','label'=>'Committee & Participant Images','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
[
    'url'=>'/easychair/admin/conference-speakers',
    'icon'=>'fa-microphone-lines',
    'label'=>'Conference Speakers',
'label_key'=>'menu.conference_speakers',
  'roles' => ['super_admin','org_admin','admin']
],
[
    'url'=>'/easychair/admin/conference-home-builder',
    'icon'=>'fa-panorama',
    'label'=>'Homepage Builder',
'label_key'=>'menu.homepage_builder',
  'roles' => ['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/conference-themes',
    'icon'=>'fa-layer-group',
    'label'=>'Conference Themes',
'label_key'=>'menu.conference_themes',
  'roles' => ['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/homepage-sections',
    'icon'=>'fa-layer-group',
    'label'=>'Homepage Sections',
'label_key'=>'menu.homepage_sections',
  'roles' => ['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/hero-slides',
    'icon'=>'fa-images',
    'label'=>'Hero Slides',
    'label_key'=>'menu.hero_slides',
    'roles' => ['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/venues',
    'icon'=>'fa-location-dot',
    'label'=>'Conference Venues',
    'label_key'=>'menu.conference_venues',
    'roles' => ['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/homepage-content',
    'icon'=>'fa-pen-to-square',
    'label'=>'Homepage Content',
    'label_key'=>'menu.homepage_content',
    'roles'=>['super_admin','org_admin','admin']
],


[
    'url'=>'/easychair/admin/homepage-design',
    'icon'=>'fa-palette',
    'label'=>'Homepage Designer',
    'label_key'=>'menu.homepage_designer',
    'roles'=>['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/conference-sponsors',
    'icon'=>'fa-handshake',
    'label'=>'Conference Sponsors',
    'label_key'=>'menu.conference_sponsors',
    'roles'=>['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/homepage-studio',
    'icon'=>'fa-globe',
    'label'=>'Homepage Studio',
    'label_key'=>'menu.homepage_studio',
    'roles'=>['super_admin','org_admin','admin']
],

[
    'url'=>'/easychair/admin/conference-home/sections',
    'icon'=>'fa-globe',
    'label'=>'Homepage Sections',
    'label_key'=>'menu.homepage_sections',
    'roles'=>['super_admin','org_admin','admin']
],


                ['url'=>'/easychair/admin/conference-business','icon'=>'fa-calendar-days','label_key'=>'menu.conference_business','label'=>'Conference Business','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
                ['url'=>'/easychair/admin/certificates','icon'=>'fa-certificate','label_key'=>'menu.certificates','label'=>'Certificates','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
            ]],
            ['group'=>'submissions','label_key'=>'menu.submissions','label'=>'Submissions','items'=>[
                ['url'=>'/easychair/admin/submission-search','icon'=>'fa-magnifying-glass','label_key'=>'menu.submission_search','label'=>'Submission Search','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','section_editor']],
                ['url'=>'/easychair/admin/submissions','icon'=>'fa-inbox','label_key'=>'menu.submissions','label'=>'Submissions','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','section_editor']],
                ['url'=>'/easychair/admin/decisions','icon'=>'fa-gavel','label_key'=>'menu.decisions','label'=>'Decisions','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/production','icon'=>'fa-industry','label_key'=>'menu.production','label'=>'Production / Camera-ready','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
                ['url'=>'/easychair/admin/issues','icon'=>'fa-book','label_key'=>'menu.issues','label'=>'Issues / Proceedings','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
            ]],
            ['group'=>'reviewing','label_key'=>'menu.reviewing','label'=>'Reviewing','items'=>[
                ['url'=>'/easychair/admin/reviewer-assignments','icon'=>'fa-user-plus','label_key'=>'menu.reviewer_assignment','label'=>'Reviewer Assignment','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','section_editor']],
                ['url'=>'/easychair/admin/reviewers','icon'=>'fa-user-check','label_key'=>'menu.reviewers','label'=>'Reviewers','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair']],
                ['url'=>'/easychair/admin/reviewer-expertise','icon'=>'fa-brain','label_key'=>'menu.reviewer_expertise','label'=>'Reviewer Expertise','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair']],
                ['url'=>'/easychair/admin/review-form','icon'=>'fa-clipboard-list','label_key'=>'menu.review_form','label'=>'Review Form','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/review-monitor','icon'=>'fa-chart-line','label_key'=>'menu.review_monitor','label'=>'Review Monitor','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair']],
            ]],
            ['group'=>'workflow','label_key'=>'menu.workflow','label'=>'Workflow','items'=>[
                ['url'=>'/easychair/admin/workflow','icon'=>'fa-diagram-project','label_key'=>'menu.workflow','label'=>'Workflow','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/submission-stage-rules','icon'=>'fa-list-ol','label_key'=>'menu.submission_stage_rules','label'=>'Submission Stage Rules','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/workflow-notifications','icon'=>'fa-bell','label_key'=>'menu.workflow_notifications','label'=>'Workflow Notifications','roles'=>['super_admin','org_admin','admin','conference_chair']],
            ]],
            ['group'=>'email','label_key'=>'menu.email_system','label'=>'Mail & System','items'=>[
                ['url'=>'/easychair/admin/conference-mail','icon'=>'fa-envelope-circle-check','label_key'=>'menu.conference_mail','label'=>'Conference Mail','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/email-templates','icon'=>'fa-envelope-open-text','label_key'=>'menu.email_templates','label'=>'Email Templates','roles'=>['super_admin','org_admin','admin','conference_chair']],
                ['url'=>'/easychair/admin/org-smtp','icon'=>'fa-server','label_key'=>'menu.org_smtp','label'=>'Organization SMTP','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/test-email','icon'=>'fa-paper-plane','label_key'=>'menu.test_email','label'=>'Test Email','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/notification-broadcast','icon'=>'fa-bullhorn','label_key'=>'menu.notification_broadcast','label'=>'Notification Broadcast','roles'=>['super_admin','org_admin','admin','conference_chair','organizer']],
                ['url'=>'/easychair/admin/notification-digest','icon'=>'fa-newspaper','label_key'=>'menu.notification_digest','label'=>'Notification Digest','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/notification-audit','icon'=>'fa-bell-circle-check','label_key'=>'menu.notification_audit','label'=>'Notification Audit','roles'=>['super_admin','org_admin','admin']],
            ]],
 /*           ['group'=>'rbac','label_key'=>'menu.rbac','label'=>'Users & Permissions','items'=>[
                ['url'=>'/easychair/admin/users','icon'=>'fa-users','label_key'=>'menu.users','label'=>'Users','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/roles','icon'=>'fa-user-shield','label_key'=>'menu.roles','label'=>'Roles','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/conference-role-permissions','icon'=>'fa-user-lock','label_key'=>'menu.conference_role_permissions','label'=>'Conference Role Permissions','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/conference-business-permissions','icon'=>'fa-shield-halved','label_key'=>'menu.conference_business_permissions','label'=>'Conference Business Permissions','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/custom-rights','icon'=>'fa-key','label_key'=>'menu.custom_rights','label'=>'Custom Rights','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/rbac-scope','icon'=>'fa-copy','label_key'=>'menu.rbac_scope','label'=>'RBAC Scope Manager','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/rbac-matrix','icon'=>'fa-sliders','label_key'=>'menu.rbac_matrix','label'=>'RBAC Matrix','roles'=>['super_admin']],
            ]], */

[
    'group' => 'rbac',

    'label_key' => 'menu.rbac',

    'label' => 'Users & Permissions',

    'items' => [

        [
            'url' => '/easychair/admin/users',
            'icon' => 'fa-users',
            'label_key' => 'menu.users',
            'label' => 'Users',
            'roles' => ['super_admin','org_admin','admin']
        ],

        [
            'url' => '/easychair/admin/roles',
            'icon' => 'fa-user-shield',
            'label_key' => 'menu.roles',
            'label' => 'Roles',
            'roles' => ['super_admin']
        ],

[
    'url' => '/easychair/admin/security',
    'icon' => 'fa-shield',
    'label' => 'Security Monitor',
    'roles' => ['super_admin']
],

        [
            'url' => '/easychair/admin/conference-role-permissions',
            'icon' => 'fa-user-lock',
            'label_key' => 'menu.conference_role_permissions',
            'label' => 'Conference Role Permissions',
            'roles' => ['super_admin','org_admin','admin']
        ],

        [
            'url' => '/easychair/admin/conference-business-permissions',
            'icon' => 'fa-shield-halved',
            'label_key' => 'menu.conference_business_permissions',
            'label' => 'Conference Business Permissions',
            'roles' => ['super_admin','org_admin','admin']
        ],

        [
            'url' => '/easychair/admin/custom-rights',
            'icon' => 'fa-key',
            'label_key' => 'menu.custom_rights',
            'label' => 'Custom Rights',
            'roles' => ['super_admin']
        ],

        [
            'url' => '/easychair/admin/rbac-scope',
            'icon' => 'fa-copy',
            'label_key' => 'menu.rbac_scope',
            'label' => 'RBAC Scope Manager',
            'roles' => ['super_admin','org_admin','admin']
        ],

        [
            'url' => '/easychair/admin/rbac-matrix',
            'icon' => 'fa-sliders',
            'label_key' => 'menu.rbac_matrix',
            'label' => 'RBAC Matrix',
            'roles' => ['super_admin']
        ],




    ]
],

            ['group'=>'settings','label_key'=>'menu.settings','label'=>'Settings','items'=>[
                ['url'=>'/easychair/admin/dictionary','icon'=>'fa-language','label_key'=>'menu.dictionary','label'=>'Dictionary','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/dictionary-scan','icon'=>'fa-magnifying-glass','label_key'=>'menu.dictionary_scan','label'=>'Dictionary Scanner','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/dictionary-replace','icon'=>'fa-wand-magic-sparkles','label_key'=>'menu.dictionary_replace','label'=>'Dictionary Replace','roles'=>['super_admin']],
                ['url'=>'/easychair/admin/profile-completion','icon'=>'fa-chart-pie','label_key'=>'menu.profile_completion','label'=>'Profile Completion','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/admin/profile-fields','icon'=>'fa-list-check','label_key'=>'menu.profile_fields','label'=>'Profile Fields','roles'=>['super_admin','org_admin','admin']],
                ['url'=>'/easychair/profile','icon'=>'fa-user-graduate','label_key'=>'menu.my_profile','label'=>'My Academic Profile','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','organizer','section_editor']],
                ['url'=>'/easychair/notifications','icon'=>'fa-bell','label_key'=>'menu.my_notifications','label'=>'My Notifications','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','organizer','section_editor']],
                ['url'=>'/easychair/select-conference?manual=1','icon'=>'fa-building-columns','label_key'=>'menu.change_conference','label'=>'Change Conference','roles'=>['super_admin']],

[
    'url'=>'/easychair/select-role?manual=1',
    'icon'=>'fa-building-columns',
    'label_key'=>'menu.change_conference',
    'label'=>'Change Role',
    'roles'=>[
        'super_admin',
        'conference_chair',
        'track_chair',
        'organizer',
        'reviewer',
        'author'
    ]
],


                ['url'=>'/easychair/logout','icon'=>'fa-right-from-bracket','label_key'=>'menu.logout','label'=>'Logout','roles'=>['super_admin','org_admin','admin','conference_chair','track_chair','organizer','section_editor']],
            ]],
        ];
    }
}
if (!function_exists('ec_role_layout_filtered_groups')) {
    function ec_role_layout_filtered_groups(?string $roleCode=null): array {
        $roleCode = $roleCode ?: ec_role_layout_role_code();
        //$isSuper = $roleCode === 'super_admin';
$isSuper = in_array(
    strtolower($roleCode),
    ['super_admin', 'superadmin'],
    true
);        
$groups = [];
        foreach (ec_role_layout_all_groups() as $group) {
            $items = [];
            foreach (($group['items'] ?? []) as $item) {
                $roles = $item['roles'] ?? [];
                $url = (string)($item['url'] ?? '#');
                if (!$isSuper && $roles && !in_array($roleCode, $roles, true)) continue;
                if (!$isSuper && function_exists('ec_can_menu') && !ec_can_menu($url)) continue;
                $item['label'] = ec_role_layout_t((string)($item['label_key'] ?? ''), (string)($item['label'] ?? ''));
                $items[] = $item;
            }
            if ($items) {
                $group['label'] = ec_role_layout_t((string)($group['label_key'] ?? ''), (string)($group['label'] ?? ''));
                $group['items'] = $items;
                $groups[] = $group;
            }
        }
        return $groups;
    }
}
