<?php
require_once __DIR__ . '/../config/database.php';
requireLogin();
$db = Database::getInstance();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$lesson = $db->get("SELECT cl.*, c.title as course_title, c.id as course_id, c.instructor_id FROM course_lessons cl JOIN courses c ON cl.course_id = c.id WHERE cl.id = ?", [$id]);
if (!$lesson) redirect('courses.php');
$user = getCurrentUser();

// Check access: students must be enrolled or lesson is free, or instructor
if (!hasRole('instructor') || (int)$lesson['instructor_id'] !== (int)$user['id']) {
    $enrolled = $db->get("SELECT id FROM course_enrollments WHERE course_id = ? AND student_id = ? AND status = 'active'", [$lesson['course_id'], $user['id']]);
    if (!$enrolled && !$lesson['is_free'] && !hasRole('instructor')) {
        // not enrolled
        header('Location: ' . APP_URL . '/lms/courses.php?course_id=' . (int)$lesson['course_id']);
        exit;
    }
}

$pageTitle = 'Lesson - ' . htmlspecialchars($lesson['title']);

// Get media metadata if any
$media = $db->get("SELECT * FROM course_media WHERE lesson_id = ? ORDER BY id DESC LIMIT 1", [$id]);
$thumbnail = $media['thumbnail_path'] ?? null;

include __DIR__ . '/includes/header.php';
?>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header"><h5 class="m-0"><?php echo htmlspecialchars($lesson['title']); ?></h5></div>
            <div class="card-body">
                <p class="text-muted">Course: <a href="<?php echo APP_URL; ?>/lms/courses.php?course_id=<?php echo (int)$lesson['course_id']; ?>"><?php echo htmlspecialchars($lesson['course_title']); ?></a></p>

                <?php if ($lesson['content_type'] === 'text'): ?>
                    <div><?php echo nl2br(htmlspecialchars($lesson['content_text'])); ?></div>
                <?php elseif ($lesson['content_type'] === 'video'): ?>
                    <?php if ($lesson['content_url']): ?>
                        <video controls width="100%" src="/<?php echo htmlspecialchars($lesson['content_url']); ?>" <?php echo $thumbnail ? 'poster="/' . htmlspecialchars($thumbnail) . '"' : ''; ?>></video>
                    <?php else: ?>
                        <p class="text-muted">No video uploaded yet.</p>
                    <?php endif; ?>
                <?php elseif ($lesson['content_type'] === 'slides'): ?>
                    <?php if ($lesson['content_url']): ?>
                        <a class="btn btn-primary" href="/<?php echo htmlspecialchars($lesson['content_url']); ?>" target="_blank">Open Slides</a>
                    <?php else: ?>
                        <p class="text-muted">No slides uploaded yet.</p>
                    <?php endif; ?>
                <?php elseif ($lesson['content_type'] === 'quiz'): ?>
                    <p class="text-muted">This lesson is a quiz. <a href="<?php echo APP_URL; ?>/lms/assessments.php">Open Assessments</a></p>
                <?php else: ?>
                    <p class="text-muted">Content type: <?php echo htmlspecialchars($lesson['content_type']); ?></p>
                <?php endif; ?>

                <?php if (!empty($lesson['downloadable_resources'])): $resources = json_decode($lesson['downloadable_resources'], true); ?>
                    <hr />
                    <h6>Resources</h6>
                    <ul>
                        <?php foreach ($resources as $r): ?>
                            <li><a href="/<?php echo htmlspecialchars($r); ?>" target="_blank"><?php echo basename($r); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php';
