-- name: CreateTask :one
INSERT INTO tasks (title, source, payload)
VALUES ($1, $2, $3)
RETURNING *;

-- name: GetTask :one
SELECT *
FROM tasks
WHERE id::text = $1;

-- name: ListTasks :many
SELECT *
FROM tasks
ORDER BY created_at DESC
LIMIT $1;

-- name: UpdateTaskStatus :one
UPDATE tasks
SET status = $2, updated_at = now()
WHERE id::text = $1
RETURNING *;

-- name: CompleteTask :one
UPDATE tasks
SET status = 'completed', result = $2, error = NULL, updated_at = now()
WHERE id::text = $1
RETURNING *;

-- name: FailTask :one
UPDATE tasks
SET status = 'failed', error = $2, updated_at = now()
WHERE id::text = $1
RETURNING *;
