-- name: CreateWorktree :one
INSERT INTO workspace_worktrees (
  id, workspace_id, branch, path, status, active_holder, acquired_at
) VALUES ($1,$2,$3,$4,$5,$6,$7)
RETURNING *;

-- name: GetWorktreeByID :one
SELECT * FROM workspace_worktrees WHERE id = $1;

-- name: GetWorktreeByBranchForUpdate :one
SELECT * FROM workspace_worktrees
WHERE workspace_id = $1 AND branch = $2
FOR UPDATE;

-- name: ListWorktreesByWorkspace :many
SELECT * FROM workspace_worktrees
WHERE workspace_id = $1
ORDER BY created_at DESC;

-- name: SetWorktreeActive :one
UPDATE workspace_worktrees
SET status = 'active', active_holder = $2, acquired_at = now(), last_used_at = now()
WHERE id = $1
RETURNING *;

-- name: SetWorktreeIdle :one
UPDATE workspace_worktrees
SET status = 'idle', active_holder = NULL, acquired_at = NULL, last_used_at = now()
WHERE id = $1
RETURNING *;

-- name: SetWorktreePruning :one
UPDATE workspace_worktrees
SET status = 'pruning', last_used_at = now()
WHERE id = $1
RETURNING *;

-- name: DeleteWorktree :exec
DELETE FROM workspace_worktrees WHERE id = $1;
