-- name: GetFullLearningTree :many
SELECT
    c.id AS course_id,
    c.title AS course_title,
    sc.id AS sub_course_id,
    sc.title AS sub_course_title,
    sc.level AS sub_course_level
FROM courses c
LEFT JOIN sub_courses sc ON sc.course_id = c.id AND sc.is_active = true
WHERE c.is_active = true
ORDER BY c.id, sc.display_order, sc.id;

-- name: GetCourseLearningPath :many
SELECT
    c.id AS course_id,
    c.title AS course_title,
    c.description AS course_description,
    c.thumbnail AS course_thumbnail,
    c.intro_video_url AS course_intro_video_url,
    cc.id AS category_id,
    cc.name AS category_name,
    sc.id AS sub_course_id,
    sc.title AS sub_course_title,
    sc.description AS sub_course_description,
    sc.thumbnail AS sub_course_thumbnail,
    sc.display_order AS sub_course_display_order,
    sc.level AS sub_course_level,
    (SELECT COUNT(*) FROM sub_course_prerequisites WHERE sub_course_id = sc.id) AS prerequisite_count,
    (SELECT COUNT(*) FROM sub_course_videos WHERE sub_course_id = sc.id AND status = 'PUBLISHED') AS video_count,
    (SELECT COUNT(*) FROM question_sets WHERE set_type = 'PRACTICE' AND owner_type = 'SUB_COURSE' AND owner_id = sc.id AND status = 'PUBLISHED') AS practice_count
FROM courses c
JOIN course_categories cc ON cc.id = c.category_id
LEFT JOIN sub_courses sc ON sc.course_id = c.id AND sc.is_active = true
WHERE c.id = $1
ORDER BY sc.display_order, sc.id;

-- name: GetSubCourseVideosForLearningPath :many
SELECT id, title, description, video_url, duration, resolution, display_order, 
       vimeo_id, vimeo_embed_url, video_host_provider
FROM sub_course_videos
WHERE sub_course_id = $1 AND status = 'PUBLISHED'
ORDER BY display_order, id;

-- name: GetSubCoursePracticesForLearningPath :many
SELECT id, title, description, persona, status,
       (SELECT COUNT(*) FROM question_set_items WHERE set_id = qs.id) AS question_count
FROM question_sets qs
WHERE qs.owner_type = 'SUB_COURSE' AND qs.owner_id = $1 
  AND qs.set_type = 'PRACTICE' AND qs.status = 'PUBLISHED'
ORDER BY qs.display_order ASC, qs.created_at;

-- name: GetSubCoursePrerequisitesForLearningPath :many
SELECT p.prerequisite_sub_course_id, sc.title, sc.level
FROM sub_course_prerequisites p
JOIN sub_courses sc ON sc.id = p.prerequisite_sub_course_id
WHERE p.sub_course_id = $1
ORDER BY sc.display_order;
