-- name: CreateSubCourse :one
INSERT INTO sub_courses (
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
)
VALUES ($1, $2, $3, $4, COALESCE($5, 0), $6, COALESCE($7, true))
RETURNING *;

-- name: GetSubCourseByID :one
SELECT *
FROM sub_courses
WHERE id = $1;

-- name: GetSubCoursesByCourse :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE course_id = $1
ORDER BY display_order ASC, id ASC;

-- name: ListSubCoursesByCourse :many
SELECT
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE course_id = $1
  AND is_active = TRUE
ORDER BY display_order ASC, id ASC;

-- name: ListActiveSubCourses :many
SELECT
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE is_active = TRUE
ORDER BY display_order ASC;

-- name: UpdateSubCourse :exec
UPDATE sub_courses
SET
    title = COALESCE($1, title),
    description = COALESCE($2, description),
    thumbnail = COALESCE($3, thumbnail),
    display_order = COALESCE($4, display_order),
    level = COALESCE($5, level),
    is_active = COALESCE($6, is_active)
WHERE id = $7;

-- name: DeleteSubCourse :one
DELETE FROM sub_courses
WHERE id = $1
RETURNING *;

-- name: DeactivateSubCourse :exec
UPDATE sub_courses
SET is_active = FALSE
WHERE id = $1;
