<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-dark-primary elevation-4">

  <a href="#" class="brand-link">
    <img 
      src="../../<?= htmlspecialchars($staff_photo) ?>" 
      alt="Staff Image" 
      title="<?= htmlspecialchars($staff_role) ?>" 
      class="brand-image img-circle elevation-3" 
      style="opacity: .8"
    >

    <div class="d-inline-block">
      <span class="brand-text font-weight-bold d-block">
        <?= htmlspecialchars($staff_name) ?>
      </span>
    </div>
  </a>

  <div class="sidebar">

    <?php
    // Normalize role
    $normalized_role = strtolower(trim(str_replace(['_', '-'], ' ', $staff_role)));

    // Automatically treat any role containing "teacher" as full access
    $is_teacher = (
        strpos($normalized_role, 'teacher') !== false || 
        strpos($normalized_role, 'class teacher') !== false || 
        strpos($normalized_role, 'subject teacher') !== false
    );

    // Super roles that should also have full access
    $full_roles = [
      'admin',
      'super admin',
      'cbt admin',
      'exam officer'
    ];

    // Final decision: who has full access?
    $has_full_access = $is_teacher || in_array($normalized_role, $full_roles);
    ?>

    <nav class="mt-2">
      <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu">

        <li class="nav-item">
          <a href="dashboard.php" class="nav-link active">
            <i class="nav-icon fas fa-tachometer-alt"></i>
            <p>Dashboard</p>
          </a>
        </li>

        <?php if ($has_full_access): ?>
          <!-- ===========================
               FULL ACCESS SIDEBAR
               Admin + Super Admin + CBT + Exam Officer +
               Class Teachers + Subject Teachers
          ============================ -->

          <li class="nav-item">
            <a href="manage_exams.php" class="nav-link">
              <i class="nav-icon fas fa-tasks"></i>
              <p>Manage Exams</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="exam_instructions.php" class="nav-link">
              <i class="nav-icon fas fa-info-circle"></i>
              <p>Exam Instructions</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="manage_questions.php" class="nav-link">
              <i class="nav-icon fas fa-question-circle"></i>
              <p>Question Bank</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="admin_review_theory.php" class="nav-link">
              <i class="nav-icon fas fa-file-signature"></i>
              <p>Review Theory</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="view_students.php" class="nav-link">
              <i class="nav-icon fas fa-users"></i>
              <p>View Students</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="students_results.php" class="nav-link">
              <i class="nav-icon fas fa-file-alt"></i>
              <p>View Results</p>
            </a>
          </li>

          <!-- RESET attempts — Only super roles should have access, not teachers -->
          <?php if (in_array($normalized_role, $full_roles)): ?>
            <li class="nav-item">
              <a href="reset_exam.php" class="nav-link">
                <i class="nav-icon fas fa-sync-alt text-info"></i>
                <p>Reset Attempts</p>
              </a>
            </li>

            <li class="nav-item">
              <a href="flagged_students.php" class="nav-link">
                <i class="nav-icon fas fa-user-shield text-warning"></i>
                <p>Flagged Students</p>
              </a>
            </li>
          <?php endif; ?>

        <?php else: ?>
          <!-- ===========================
               LIMITED ACCESS SIDEBAR
               (NON-TEACHER, NON-ADMIN)
          ============================ -->

          <li class="nav-item">
            <a href="manage_questions.php" class="nav-link">
              <i class="nav-icon fas fa-question-circle"></i>
              <p>Question Bank</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="exam_instructions.php" class="nav-link">
              <i class="nav-icon fas fa-info-circle"></i>
              <p>Exam Instructions</p>
            </a>
          </li>

          <li class="nav-item">
            <a href="students_results.php" class="nav-link">
              <i class="nav-icon fas fa-file-alt"></i>
              <p>View Results</p>
            </a>
          </li>

        <?php endif; ?>

      </ul>
    </nav>
  </div>
</aside>
