<?php
session_start();
require_once('../config.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'student') {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// Get ONLY enrolled courses
$stmt = $pdo->prepare("
SELECT c.* 
FROM courses c
JOIN enrollments e ON e.course_id = c.id
WHERE e.student_id = ? AND e.status = 'enrolled'
");
$stmt->execute([$user_id]);
$courses = $stmt->fetchAll();
?>

<div class="container py-5">
<h3>My Learning Units</h3>

<?php foreach($courses as $c): ?>

<div class="card p-3 mb-4">

<h5><?= htmlspecialchars($c['course_name']); ?></h5>

<?php
$stmt = $pdo->prepare("SELECT * FROM modules WHERE course_id=?");
$stmt->execute([$c['id']]);
$modules = $stmt->fetchAll();
?>

<?php foreach($modules as $m): ?>

<div class="mt-3">
<h6 class="fw-bold text-primary"><?= htmlspecialchars($m['module_name']); ?></h6>

<?php
$stmt = $pdo->prepare("SELECT * FROM units WHERE module_id=?");
$stmt->execute([$m['id']]);
$units = $stmt->fetchAll();
?>

<ul class="list-group mb-2">
<?php foreach($units as $u): ?>
<li class="list-group-item">
<?= htmlspecialchars($u['unit_name']); ?>
</li>
<?php endforeach; ?>
</ul>

</div>

<?php endforeach; ?>

</div>

<?php endforeach; ?>
</div>
