-- name: CreateLesson :one
INSERT INTO lessons (module_id, title, video_url, thumbnail, description)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;

-- name: GetLessonByID :one
SELECT *
FROM lessons
WHERE id = $1;

-- name: ListLessonsByModuleID :many
SELECT
    COUNT(*) OVER () AS total_count,
    l.id,
    l.module_id,
    l.title,
    l.video_url,
    l.thumbnail,
    l.description,
    l.created_at,
    l.updated_at
FROM lessons l
WHERE l.module_id = $1
ORDER BY l.created_at DESC
LIMIT $2 OFFSET $3;

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

-- name: DeleteLesson :exec
DELETE FROM lessons
WHERE id = $1;
