-- name: CreateModule :one
INSERT INTO modules (program_id, course_id, name, description, icon)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;

-- name: GetModuleByID :one
SELECT *
FROM modules
WHERE id = $1;

-- name: ListModulesByProgramAndCourse :many
SELECT
    COUNT(*) OVER () AS total_count,
    m.id,
    m.program_id,
    m.course_id,
    m.name,
    m.description,
    m.icon,
    m.created_at,
    m.updated_at
FROM modules m
WHERE m.program_id = $1
  AND m.course_id = $2
ORDER BY m.created_at DESC
LIMIT $3 OFFSET $4;

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

-- name: DeleteModule :exec
DELETE FROM modules
WHERE id = $1;
