-- name: CreateCourse :one
INSERT INTO courses (program_id, name, description, thumbnail)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: GetCourseByID :one
SELECT *
FROM courses
WHERE id = $1;

-- name: ListCoursesByProgramID :many
SELECT
    COUNT(*) OVER () AS total_count,
    c.id,
    c.program_id,
    c.name,
    c.description,
    c.thumbnail,
    c.created_at,
    c.updated_at
FROM courses c
WHERE c.program_id = $1
ORDER BY c.created_at DESC
LIMIT $2 OFFSET $3;

-- name: UpdateCourse :one
UPDATE courses
SET
    name = COALESCE(sqlc.narg('name')::varchar, name),
    description = COALESCE(sqlc.narg('description')::text, description),
    thumbnail = COALESCE(sqlc.narg('thumbnail')::text, thumbnail),
    updated_at = CURRENT_TIMESTAMP
WHERE id = sqlc.arg('id')
RETURNING *;

-- name: DeleteCourse :exec
DELETE FROM courses
WHERE id = $1;
