-- name: ListEtapesByTablo :many
SELECT id, tablo_id, title, description, position, created_at, updated_at
FROM etapes
WHERE tablo_id = $1
ORDER BY position, created_at;

-- name: InsertEtape :one
INSERT INTO etapes (tablo_id, title, description, position)
VALUES ($1, $2, $3, $4)
RETURNING id, tablo_id, title, description, position, created_at, updated_at;

-- name: GetEtapeByID :one
SELECT id, tablo_id, title, description, position, created_at, updated_at
FROM etapes
WHERE id = $1 AND tablo_id = $2;

-- name: UpdateEtape :one
UPDATE etapes
SET title = $3, description = $4, updated_at = now()
WHERE id = $1 AND tablo_id = $2
RETURNING id, tablo_id, title, description, position, created_at, updated_at;

-- name: DeleteEtape :exec
DELETE FROM etapes WHERE id = $1 AND tablo_id = $2;

-- name: MaxEtapePositionByTablo :one
SELECT COALESCE(MAX(position), 0)::integer AS max_position
FROM etapes
WHERE tablo_id = $1;

-- name: UpdateEtapePosition :one
UPDATE etapes
SET position = $3, updated_at = now()
WHERE id = $1 AND tablo_id = $2
RETURNING id, tablo_id, title, description, position, created_at, updated_at;
