<?php
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) session_start();

require_once __DIR__ . '/../includes/student_course_helpers.php';

$studentId = (int)($_SESSION['student']['id'] ?? $_SESSION['user']['id'] ?? 0);
$active    = $_SESSION['active_course_id'] ?? 'all';
$courses   = $studentId ? elms_student_courses($mysqli, $studentId) : [];

function chip(string $label, string $cid, string $active): void {
  $is   = ($active === $cid) ? 'btn-primary' : 'btn-outline-primary';
  $next = $_SERVER['REQUEST_URI'] ?? '/elms/student/dashboard.php';
  $href = '/elms/student/course_select.php?course_id='.urlencode($cid).'&next='.urlencode($next);
  echo '<a class="btn btn-sm '.$is.' me-2 mb-2" href="'.esc($href).'">'.esc($label).'</a>';
}
?>
<div class="d-flex flex-wrap align-items-center mb-3">
  <?php chip('All', 'all', $active); ?>
  <?php foreach ($courses as $c): chip($c['course_name'], (string)$c['course_id'], $active); endforeach; ?>
</div>
