<?php
// /elms/includes/admin_sidebar_roleaware.php
if (session_status() === PHP_SESSION_NONE) session_start();
define('APP_ROOT', dirname(__DIR__));
require_once APP_ROOT . '/includes/capabilities.php'; // provides can()

$ACTIVE_MENU = $ACTIVE_MENU ?? ''; // layout can set this

$MENU = [
  ['key'=>'dashboard','label'=>'Dashboard','href'=>'/elms/admin/dashboard.php','icon'=>'speedometer2','cap'=>null],

  // Teaching/coordination surface
  ['key'=>'batches','label'=>'Batches','href'=>'/elms/admin/batches/','icon'=>'layers','cap'=>'view_batches'],
  ['key'=>'courses','label'=>'Courses','href'=>'/elms/admin/courses/','icon'=>'journal-code','cap'=>'view_courses'],
  ['key'=>'assessments','label'=>'Assessments','href'=>'/elms/admin/assessments/','icon'=>'ui-checks-grid','cap'=>'view_assessments'],
  ['key'=>'content','label'=>'LMS Content','href'=>'/elms/admin/content/','icon'=>'collection-play','cap'=>'view_content'],

  // Ops (optional for coordinator)
  ['key'=>'attendance','label'=>'Attendance','href'=>'/elms/admin/attendance/','icon'=>'clipboard-check','cap'=>'view_attendance'],
  ['key'=>'leave','label'=>'Leave','href'=>'/elms/admin/leave/','icon'=>'calendar2-week','cap'=>'view_leave'],

  // Finance (hidden for coordinator by default)
  ['key'=>'payment_manager','label'=>'Payment Manager','href'=>'/elms/admin/finance/payment_manager.php','icon'=>'cash-coin','cap'=>'view_payment_manager'],

  // Communication
  ['key'=>'announcements','label'=>'Announcements','href'=>'/elms/admin/announcements/','icon'=>'megaphone','cap'=>'announce'],

  // Admin only
  ['key'=>'users','label'=>'Users','href'=>'/elms/admin/users/','icon'=>'people','cap'=>'view_users'],
  ['key'=>'dashboard_manager','label'=>'Dashboard Manager','href'=>'/elms/admin/dashboard_manager.php','icon'=>'sliders2','cap'=>'manage_dashboard'],
];

function render_menu_item(array $it, string $active): void {
  if (!empty($it['cap']) && !can($it['cap'])) return;
  $isActive = ($active === ($it['key'] ?? ''));
  $a = $isActive ? ' active' : '';
  $icon = htmlspecialchars($it['icon'] ?? 'dot', ENT_QUOTES);
  $href = htmlspecialchars($it['href'], ENT_QUOTES);
  $label = htmlspecialchars($it['label'], ENT_QUOTES);
  echo "<li class='nav-item'><a class='nav-link$a' href='$href'><i class='bi bi-$icon me-2'></i>$label</a></li>";
}
?>
<nav class="sidebar border-end">
  <ul class="nav flex-column py-3">
    <?php foreach ($MENU as $item) render_menu_item($item, $ACTIVE_MENU); ?>
  </ul>
</nav>
