<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/student-auth.php';
require_once __DIR__ . '/../includes/student-course-context.php';
$pdo=db(); requireStudent();
$student=currentStudent($pdo); if(!$student){studentLogout();redirect('login.php');}
$courseId=(int)($_GET['course_id'] ?? getSelectedStudentCourseId($pdo,(int)$student['id']) ?? 0);
if($courseId<=0 || !isStudentEnrolledInCourse($pdo,(int)$student['id'],$courseId)) redirect('student/my-courses.php');
setSelectedStudentCourse($courseId);
$course=getStudentCourse($pdo,(int)$student['id'],$courseId);
$sessions=safeFetchAll($pdo,"SELECT * FROM core_sessions WHERE course_id=? ORDER BY session_date DESC,session_time DESC",[$courseId]);
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Course Sessions</title><style>
*{box-sizing:border-box}body{margin:0;font-family:Inter,Arial,sans-serif;background:#f4f7fb;color:#0f172a}.page{max-width:1200px;margin:auto;padding:32px}.top{display:flex;justify-content:space-between;gap:20px;align-items:center;margin-bottom:28px}.top h1{font-size:clamp(30px,5vw,48px);margin:0}.back{padding:13px 18px;border-radius:14px;background:#e0e7ff;color:#3730a3;text-decoration:none;font-weight:800}.hero{padding:32px;border-radius:28px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;margin-bottom:24px}.hero p{color:#dbeafe}.grid{display:grid;gap:18px}.card{background:#fff;border-radius:24px;padding:24px;display:grid;grid-template-columns:1fr auto;gap:20px;box-shadow:0 16px 40px rgba(15,23,42,.07)}.meta{display:flex;flex-wrap:wrap;gap:10px;color:#64748b;font-size:14px}.badge{display:inline-flex;padding:7px 11px;border-radius:999px;background:#eff6ff;color:#1d4ed8;font-weight:800}.join{align-self:center;text-decoration:none;padding:13px 18px;border-radius:14px;background:#2563eb;color:#fff;font-weight:900}.join.disabled{background:#cbd5e1;pointer-events:none}.empty{background:#fff;padding:40px;border-radius:24px;text-align:center;color:#64748b}@media(max-width:700px){.page{padding:20px}.card{grid-template-columns:1fr}.top{align-items:flex-start}.join{text-align:center}}
</style></head><body><main class="page"><div class="top"><div><h1>Course Sessions</h1><p><?=e($course['title'] ?? '')?></p></div><a class="back" href="dashboard.php?course_id=<?=$courseId?>">Dashboard</a></div><section class="hero"><h2>Live lessons and recordings</h2><p>Join scheduled sessions and review lesson resources from one place.</p></section><div class="grid">
<?php foreach($sessions as $session): $start=strtotime(($session['session_date']??'').' '.($session['session_time']??'')); $canJoin=!empty($session['zoom_link']) && in_array($session['status']??'scheduled',['scheduled','active'],true) && $start >= time()-7200; ?>
<article class="card"><div><span class="badge"><?=e(safeUcfirst($session['status']??'scheduled'))?></span><h2><?=e($session['title']??'Session')?></h2><div class="meta"><span><?=e(formatDateNice($session['session_date']??null))?></span><span><?=e(formatTimeNice($session['session_time']??null))?></span></div><?php if(!empty($session['teacher_notes'])):?><p><?=nl2br(e($session['teacher_notes']))?></p><?php endif;?></div><div><?php if($canJoin):?><a class="join" target="_blank" rel="noopener" href="<?=e($session['zoom_link'])?>">Join session</a><?php elseif(!empty($session['lesson_link'])):?><a class="join" target="_blank" rel="noopener" href="<?=e($session['lesson_link'])?>">Lesson resource</a><?php else:?><span class="join disabled">Unavailable</span><?php endif;?></div></article>
<?php endforeach;?><?php if(!$sessions):?><div class="empty">No sessions have been scheduled for this course yet.</div><?php endif;?></div></main></body></html>
