<?php
include '../config.php';

if (!isGuru()) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$guru_id = $_SESSION['user_id'];
$course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;

if ($course_id <= 0) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Invalid course']);
    exit();
}

// Verify course ownership
$verify_sql = "SELECT id FROM lms_courses WHERE id = ? AND id_guru = ?";
$verify_stmt = $conn->prepare($verify_sql);
$verify_stmt->bind_param('ii', $course_id, $guru_id);
$verify_stmt->execute();
$verify_result = $verify_stmt->get_result();

if ($verify_result->num_rows === 0) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Course not found']);
    exit();
}

// Get lms_lessons for the course
$sql_lms_lessons = "SELECT id, judul_lesson FROM lms_lessons WHERE id_course = ? ORDER BY urutan";
$stmt_lms_lessons = $conn->prepare($sql_lms_lessons);
$stmt_lms_lessons->bind_param('i', $course_id);
$stmt_lms_lessons->execute();
$lms_lessons_result = $stmt_lms_lessons->get_result();

$lms_lessons = [];
while($lesson = $lms_lessons_result->fetch_assoc()) {
    $lms_lessons[] = $lesson;
}

header('Content-Type: application/json');
echo json_encode(['success' => true, 'lms_lessons' => $lms_lessons]);

$verify_stmt->close();
$stmt_lms_lessons->close();
$conn->close();
?>