<?php
// /elms/student/_program_switcher.inc.php
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) session_start();

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/rms_bridge.php';

$programs = rms_cache_student_programs($mysqli); // [{program_id, program_name, cohort_key, enrolled_at}, ...]
$active   = $_SESSION['active_program_id'] ?? 'all';

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