<?php
require_once('../config.php');
include('../header/header.php');

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

$course_id = $_GET['id'] ?? null;
if (!$course_id) {
    header("Location: dashboard.php");
    exit();
}

$stmt = $pdo->prepare("SELECT c.*, u.username as lecturer_name FROM courses c JOIN users u ON c.lecturer_id = u.id WHERE c.id = ?");
$stmt->execute([$course_id]);
$course = $stmt->fetch();

if (!$course) {
    echo "<div class='container py-5'><div class='alert alert-danger'>Course not found.</div></div>";
    include('../footer/footer.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];
$is_enrolled = false;

if ($role == 'student') {
    $stmt = $pdo->prepare("SELECT * FROM enrollments WHERE student_id = ? AND course_id = ?");
    $stmt->execute([$user_id, $course_id]);
    $is_enrolled = (bool)$stmt->fetch();
}
?>

<div class="container py-5">
    <div class="row">
        <div class="col-md-8">
            <h1 class="fw-bold mb-3"><?php echo htmlspecialchars($course['course_name']); ?></h1>
            <p class="text-muted mb-4">Instructed by: <strong><?php echo htmlspecialchars($course['lecturer_name']); ?></strong></p>
            
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-body">
                    <h5 class="fw-bold mb-3">Course Description</h5>
                    <p><?php echo nl2br(htmlspecialchars($course['description'])); ?></p>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card shadow-sm border-0 sticky-top" style="top: 100px;">
                <div class="card-body">
                    <?php if ($role == 'student'): ?>
                        <?php if ($is_enrolled): ?>
                            <div class="alert alert-success text-center"><i class="fas fa-check-circle me-2"></i>You are enrolled</div>
                            <a href="view_notes.php?course_id=<?php echo $course_id; ?>" class="btn btn-primary w-100 mb-3">Access Learning Materials</a>
                        <?php else: ?>
                            <a href="courses.php?enroll=<?php echo $course_id; ?>" class="btn btn-primary w-100 mb-3">Enroll in this Course</a>
                        <?php endif; ?>
                    <?php elseif ($role == 'lecturer' && $course['lecturer_id'] == $user_id): ?>
                        <a href="upload_notes.php?course_id=<?php echo $course_id; ?>" class="btn btn-primary w-100 mb-3">Upload Materials</a>
                        <a href="view_notes.php?course_id=<?php echo $course_id; ?>" class="btn btn-outline-primary w-100 mb-3">View Uploaded Notes</a>
                    <?php endif; ?>
                    
                    <a href="courses.php" class="btn btn-outline-secondary w-100">Back to All Courses</a>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>
