-- name: CreateLevel :one
INSERT INTO levels (
    program_id,
    title,
    description,
    level_index,
    is_active
)
VALUES ($1, $2, $3, $4, COALESCE($5, true))
RETURNING *;


-- name: GetLevelsByProgram :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    program_id,
    title,
    description,
    level_index,
    number_of_modules,
    number_of_practices,
    number_of_videos,
    is_active
FROM levels
WHERE program_id = $1
ORDER BY level_index ASC;


-- name: UpdateLevel :exec
UPDATE levels
SET
    title = COALESCE($1, title),
    description = COALESCE($2, description),
    level_index = COALESCE($3, level_index),
    is_active = COALESCE($4, is_active)
WHERE id = $5;


-- name: IncrementLevelModuleCount :exec
UPDATE levels
SET number_of_modules = number_of_modules + 1
WHERE id = $1;


-- name: IncrementLevelPracticeCount :exec
UPDATE levels
SET number_of_practices = number_of_practices + 1
WHERE id = $1;


-- name: IncrementLevelVideoCount :exec
UPDATE levels
SET number_of_videos = number_of_videos + 1
WHERE id = $1;


-- name: DeleteLevel :exec
DELETE FROM levels
WHERE id = $1;
