-- name: CreateLmsPersona :one
INSERT INTO lms_personas (name, description, avatar_url, is_active)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: GetLmsPersonaByID :one
SELECT *
FROM lms_personas
WHERE id = $1;

-- name: UpdateLmsPersona :one
UPDATE lms_personas
SET
    name = COALESCE(sqlc.narg('name')::varchar, name),
    description = COALESCE(sqlc.narg('description')::text, description),
    avatar_url = COALESCE(sqlc.narg('avatar_url')::text, avatar_url),
    is_active = COALESCE(sqlc.narg('is_active')::boolean, is_active),
    updated_at = CURRENT_TIMESTAMP
WHERE id = sqlc.arg('id')
RETURNING *;

-- name: DeleteLmsPersona :exec
DELETE FROM lms_personas
WHERE id = $1;

-- name: ListLmsPersonas :many
SELECT
    COUNT(*) OVER () AS total_count,
    p.id,
    p.name,
    p.description,
    p.avatar_url,
    p.is_active,
    p.created_at,
    p.updated_at
FROM lms_personas p
WHERE (
        sqlc.arg('filter_active')::boolean = FALSE
        OR p.is_active = TRUE
    )
ORDER BY p.name ASC, p.created_at DESC
LIMIT $1 OFFSET $2;
