<?php
require_once 'config/database.php';
require_once 'includes/functions.php';

$courseId = 14;
$db = getDB();

echo "=== Course 14 Diagnostics ===\n";

// Check if course exists
$stmt = $db->prepare('SELECT id, title, status FROM courses WHERE id = ?');
$stmt->execute([$courseId]);
$course = $stmt->fetch();
echo "Course: " . ($course ? "Found '{$course['title']}' (status: {$course['status']})\n" : 'NOT FOUND') . "\n";

// Lessons
$stmt = $db->prepare('SELECT COUNT(*) FROM course_lessons WHERE course_id = ?');
$stmt->execute([$courseId]);
$lessons = $stmt->fetchColumn();
echo "Lessons count: $lessons\n";

// Modules
$stmt = $db->prepare('SELECT COUNT(*) FROM course_modules WHERE course_id = ?');
$stmt->execute([$courseId]);
$modules = $stmt->fetchColumn();
echo "Modules count: $modules\n";

// Topics
$stmt = $db->prepare('SELECT COUNT(*) FROM course_topics WHERE course_id = ?');
$stmt->execute([$courseId]);
$topics = $stmt->fetchColumn();
echo "Topics count: $topics\n";

// Sample lessons with joins
$stmt = $db->prepare('SELECT cl.id, cl.title, cm.title as module_title, ct.title as topic_title, cl.module_id, cl.topic_id 
                      FROM course_lessons cl 
                      LEFT JOIN course_modules cm ON cl.module_id = cm.id 
                      LEFT JOIN course_topics ct ON cl.topic_id = ct.id 
                      WHERE cl.course_id = ?');
$stmt->execute([$courseId]);
$samples = $stmt->fetchAll();
echo "Sample lessons (" . count($samples) . "):\n";
foreach ($samples as $s) {
    echo "- {$s['title']} (module_id: {$s['module_id']}, topic_id: {$s['topic_id']}, module: " . ($s['module_title'] ?? 'null') . ", topic: " . ($s['topic_title'] ?? 'null') . ")\n";
}

echo "\n=== END ===\n";
?>

