<?php
// ============================================================
// includes/sidebar_helper.php
// ============================================================
function buildSidebar(string $active, string $role): void {
    $user = currentUser();
    $menus = [
        'admin' => [
            ['icon'=>'bi-speedometer2',   'label'=>'لوحة التحكم',      'href'=>'dashboard.php',   'key'=>'dashboard'],
            ['icon'=>'bi-people',          'label'=>'المستخدمون',        'href'=>'users.php',       'key'=>'users'],
            ['icon'=>'bi-person-check',    'label'=>'مراجعة المحامين',   'href'=>'lawyers.php',     'key'=>'lawyers'],
            ['icon'=>'bi-pencil-square',    'label'=>'طلبات تعديل الملفات','href'=>'profile-requests.php','key'=>'profile-requests'],
            ['icon'=>'bi-bank',             'label'=>'الولايات والنقابات', 'href'=>'bar-associations.php','key'=>'bar_associations'],
            ['icon'=>'bi-chat-square-text', 'label'=>'الشكاوي',           'href'=>'complaints.php',  'key'=>'complaints'],
            ['icon'=>'bi-bar-chart-line',  'label'=>'التقارير',          'href'=>'reports.php',     'key'=>'reports'],
            ['icon'=>'bi-shield-check',    'label'=>'سجل التدقيق',       'href'=>'audit.php',       'key'=>'audit'],
        ],
        'clerk' => [
            ['icon'=>'bi-speedometer2',        'label'=>'لوحة التحكم',  'href'=>'dashboard.php',   'key'=>'dashboard'],
            ['icon'=>'bi-folder2-open',         'label'=>'القضايا',      'href'=>'cases.php',       'key'=>'cases'],
            ['icon'=>'bi-calendar3',            'label'=>'الجلسات',      'href'=>'sessions.php',    'key'=>'sessions'],
            ['icon'=>'bi-journal-text',         'label'=>'المحاضر',      'href'=>'minutes.php',     'key'=>'minutes'],
            ['icon'=>'bi-award',                'label'=>'الأحكام',      'href'=>'judgments.php',   'key'=>'judgments'],
            ['icon'=>'bi-arrow-repeat',         'label'=>'الطعون',       'href'=>'appeals.php',     'key'=>'appeals'],
            ['icon'=>'bi-file-earmark-arrow-up','label'=>'الوثائق',      'href'=>'documents.php',   'key'=>'documents'],
            ['icon'=>'bi-chat-square-text',     'label'=>'الشكاوي',      'href'=>'complaints.php',  'key'=>'complaints'],
        ],
        'lawyer' => [
            ['icon'=>'bi-speedometer2',         'label'=>'لوحة التحكم',  'href'=>'dashboard.php',  'key'=>'dashboard'],
            ['icon'=>'bi-folder2-open',          'label'=>'قضاياي',       'href'=>'my-cases.php',   'key'=>'my_cases'],
            ['icon'=>'bi-file-earmark-arrow-up', 'label'=>'وثائقي',       'href'=>'documents.php',  'key'=>'documents'],
            ['icon'=>'bi-chat-square-text',      'label'=>'الشكاوي',      'href'=>'complaints.php', 'key'=>'complaints'],
            ['icon'=>'bi-person-gear',           'label'=>'ملفي الشخصي',  'href'=>'profile.php',    'key'=>'profile'],
        ],
    ];
    $roleLabels = ['admin'=>'مدير النظام','clerk'=>'أمين الضبط','lawyer'=>'محامي'];
    $menu = $menus[$role] ?? [];
    $roleLabel = $roleLabels[$role] ?? $role;

    $items = '';
    $logoutUrl = BASE_URL . '/includes/logout.php';
    foreach ($menu as $item) {
        $cls = ($active === $item['key']) ? 'active' : '';
        $items .= "<li><a href='{$item['href']}' class='sidebar-link {$cls}'><i class='{$item['icon']}'></i><span>{$item['label']}</span></a></li>";
    }
    $init = mb_substr($user['name'],0,1,'UTF-8');
    $depth = ['admin'=>'../../','clerk'=>'../../','lawyer'=>'../../'][$role] ?? '../../';
    echo <<<HTML
<div class="sidebar-brand">
  <div class="brand-logo">
    <svg width="22" height="22" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
      <line x1="16" y1="3" x2="16" y2="29" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="26" y2="7" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="2" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="10" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <path d="M1.5 16 Q6 19.5 10.5 16" stroke="#0A1628" stroke-width="2" fill="none" stroke-linecap="round"/>
      <line x1="26" y1="7" x2="22" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <line x1="26" y1="7" x2="30" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <path d="M21.5 16 Q26 19.5 30.5 16" stroke="#0A1628" stroke-width="2" fill="none" stroke-linecap="round"/>
      <line x1="10" y1="29" x2="22" y2="29" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
    </svg>
  </div>
  <div class="brand-text"><span class="brand-name">قضيتي</span><span class="brand-sub">نظام القضايا</span></div>
</div>
<nav class="sidebar-nav"><ul class="nav-list">{$items}</ul></nav>
<div class="sidebar-footer">
  <div class="user-info">
    <div class="user-avatar">{$init}</div>
    <div class="user-meta">
      <div class="user-name">{$user['name']}</div>
      <div class="user-role">{$roleLabel}</div>
    </div>
  </div>
  <a href="$logoutUrl" class="logout-btn" title="خروج"><i class="bi bi-box-arrow-right"></i></a>
</div>
HTML;
}