<?php
require_once 'config/database.php';
$db = getDB();

// What causes empty curriculum?
// Let's check if course exists and is published
echo "=== Checking course exists and status ===\n";
$courses = $db->query('SELECT id, title, status FROM courses ORDER BY id DESC LIMIT 5')->fetchAll(PDO::FETCH_ASSOC);
foreach ($courses as $c) {
    echo "Course {$c['id']}: {$c['title']} - {$c['status']}\n";
}

// Check what query returns for different courses
echo "\n=== Query results for different courses ===\n";
foreach ([14, 11, 1] as $courseId) {
    echo "Course $courseId:\n";
    $stmt = $db->prepare("
        SELECT cm.id as module_id, cm.title as module_name, 
               ct.id as topic_id, ct.title as topic_name,
               cl.id as lesson_id, cl.title as lesson_name
        FROM course_modules cm
        LEFT JOIN course_topics ct ON cm.id = ct.module_id
        LEFT JOIN course_lessons cl ON (ct.id = cl.topic_id OR (cl.topic_id IS NULL AND cl.module_id = cm.id))
        WHERE cm.course_id = ?
        ORDER BY cm.sort_order, ct.sort_order, cl.sort_order
    ");
    $stmt->execute([$courseId]);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $lessonIds = array_filter(array_column($rows, 'lesson_id'));
    echo "  Rows: " . count($rows) . ", Lessons: " . count($lessonIds) . "\n";
}