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

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

include('../header/header.php');

$student_id = $_SESSION['user_id'];
$enrollment_id = $_GET['enrollment_id'] ?? 0;

// 🔒 Verify student owns this enrollment
$stmt = $pdo->prepare("
    SELECT e.course_id, c.course_name 
    FROM enrollments e
    JOIN courses c ON e.course_id = c.id
    WHERE e.id = ? AND e.student_id = ? AND e.status = 'enrolled'
");
$stmt->execute([$enrollment_id, $student_id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$data) {
    die("Access denied.");
}

$course_id = $data['course_id'];
$course_name = $data['course_name'];

// 📚 Fetch notes for this course
$stmt = $pdo->prepare("
    SELECT * FROM notes 
    WHERE course_id = ? 
    ORDER BY uploaded_at DESC
");
$stmt->execute([$course_id]);
$notes = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!-- 🎨 STYLES -->
<style>
body {
    background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.6)),
                url('https://images.unsplash.com/photo-1522202176988-66273c2fd55f');
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
}

.notes-wrapper {
    padding: 40px 15px;
}

.notes-card {
    max-width: 900px;
    margin: auto;
    background: rgba(255,255,255,0.92);
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    backdrop-filter: blur(10px);
}

.page-title {
    text-align: center;
    color: white;
    font-weight: bold;
    margin-bottom: 25px;
}

.course-title {
    text-align: center;
    font-weight: bold;
    margin-bottom: 20px;
    color: #333;
}

.note-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid #ddd;
}

.note-item:hover {
    background: #f8f9fa;
}
</style>

<!-- 📚 CONTENT -->
<div class="notes-wrapper">

    <h2 class="page-title">📚 Course Notes</h2>

    <div class="notes-card">

        <h4 class="course-title">
            <?= htmlspecialchars($course_name); ?>
        </h4>

        <?php if (empty($notes)): ?>
            <p class="text-center text-muted">No notes available for this course.</p>
        <?php else: ?>

            <?php foreach ($notes as $n): ?>

                <div class="note-item">

                    <!-- NOTE NAME -->
                    <div>
                        📄 <?= htmlspecialchars($n['file_name']); ?>
                    </div>

                    <!-- DOWNLOAD -->
                    <a href="download.php?file=<?= urlencode($n['file_path']); ?>" 
                       class="btn btn-sm btn-primary">
                       Download
                    </a>

                </div>

            <?php endforeach; ?>

        <?php endif; ?>

    </div>
</div>

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