// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: worktrees.sql

package workspacesqlc

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const createWorktree = `-- 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 id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

type CreateWorktreeParams struct {
	ID           pgtype.UUID        `json:"id"`
	WorkspaceID  pgtype.UUID        `json:"workspace_id"`
	Branch       string             `json:"branch"`
	Path         string             `json:"path"`
	Status       string             `json:"status"`
	ActiveHolder *string            `json:"active_holder"`
	AcquiredAt   pgtype.Timestamptz `json:"acquired_at"`
}

func (q *Queries) CreateWorktree(ctx context.Context, arg CreateWorktreeParams) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, createWorktree,
		arg.ID,
		arg.WorkspaceID,
		arg.Branch,
		arg.Path,
		arg.Status,
		arg.ActiveHolder,
		arg.AcquiredAt,
	)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const deleteWorktree = `-- name: DeleteWorktree :exec
DELETE FROM workspace_worktrees WHERE id = $1
`

func (q *Queries) DeleteWorktree(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteWorktree, id)
	return err
}

const finishPruneRollback = `-- name: FinishPruneRollback :exec
UPDATE workspace_worktrees
   SET status = 'idle'
 WHERE id = $1 AND status = 'pruning'
`

func (q *Queries) FinishPruneRollback(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, finishPruneRollback, id)
	return err
}

const finishPruneSuccess = `-- name: FinishPruneSuccess :exec
DELETE FROM workspace_worktrees WHERE id = $1
`

func (q *Queries) FinishPruneSuccess(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, finishPruneSuccess, id)
	return err
}

const getWorktreeByBranchForUpdate = `-- name: GetWorktreeByBranchForUpdate :one
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees
WHERE workspace_id = $1 AND branch = $2
FOR UPDATE
`

type GetWorktreeByBranchForUpdateParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	Branch      string      `json:"branch"`
}

func (q *Queries) GetWorktreeByBranchForUpdate(ctx context.Context, arg GetWorktreeByBranchForUpdateParams) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, getWorktreeByBranchForUpdate, arg.WorkspaceID, arg.Branch)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const getWorktreeByID = `-- name: GetWorktreeByID :one
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees WHERE id = $1
`

func (q *Queries) GetWorktreeByID(ctx context.Context, id pgtype.UUID) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, getWorktreeByID, id)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const getWorktreeByIDForUpdate = `-- name: GetWorktreeByIDForUpdate :one
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees WHERE id = $1
FOR UPDATE
`

func (q *Queries) GetWorktreeByIDForUpdate(ctx context.Context, id pgtype.UUID) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, getWorktreeByIDForUpdate, id)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const listWorktreesByWorkspace = `-- name: ListWorktreesByWorkspace :many
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees
WHERE workspace_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListWorktreesByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]WorkspaceWorktree, error) {
	rows, err := q.db.Query(ctx, listWorktreesByWorkspace, workspaceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceWorktree
	for rows.Next() {
		var i WorkspaceWorktree
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Branch,
			&i.Path,
			&i.Status,
			&i.ActiveHolder,
			&i.AcquiredAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.PruneWarningAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorktreesNeedingPrune = `-- name: ListWorktreesNeedingPrune :many
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees
 WHERE status = 'idle'
   AND last_used_at < $1
 ORDER BY last_used_at ASC
`

func (q *Queries) ListWorktreesNeedingPrune(ctx context.Context, lastUsedAt pgtype.Timestamptz) ([]WorkspaceWorktree, error) {
	rows, err := q.db.Query(ctx, listWorktreesNeedingPrune, lastUsedAt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceWorktree
	for rows.Next() {
		var i WorkspaceWorktree
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Branch,
			&i.Path,
			&i.Status,
			&i.ActiveHolder,
			&i.AcquiredAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.PruneWarningAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorktreesNeedingPruneWarning = `-- name: ListWorktreesNeedingPruneWarning :many
SELECT id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at FROM workspace_worktrees
 WHERE status = 'idle'
   AND prune_warning_at IS NULL
   AND last_used_at < $1
 ORDER BY last_used_at ASC
`

func (q *Queries) ListWorktreesNeedingPruneWarning(ctx context.Context, lastUsedAt pgtype.Timestamptz) ([]WorkspaceWorktree, error) {
	rows, err := q.db.Query(ctx, listWorktreesNeedingPruneWarning, lastUsedAt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceWorktree
	for rows.Next() {
		var i WorkspaceWorktree
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Branch,
			&i.Path,
			&i.Status,
			&i.ActiveHolder,
			&i.AcquiredAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.PruneWarningAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markPruneWarning = `-- name: MarkPruneWarning :exec
UPDATE workspace_worktrees
   SET prune_warning_at = now()
 WHERE id = $1 AND prune_warning_at IS NULL
`

func (q *Queries) MarkPruneWarning(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, markPruneWarning, id)
	return err
}

const releaseWorktreesByHolder = `-- name: ReleaseWorktreesByHolder :many
UPDATE workspace_worktrees
SET status = 'idle',
    active_holder = NULL,
    acquired_at = NULL,
    last_used_at = now(),
    prune_warning_at = NULL
WHERE active_holder = $1 AND status = 'active'
RETURNING id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

func (q *Queries) ReleaseWorktreesByHolder(ctx context.Context, activeHolder *string) ([]WorkspaceWorktree, error) {
	rows, err := q.db.Query(ctx, releaseWorktreesByHolder, activeHolder)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceWorktree
	for rows.Next() {
		var i WorkspaceWorktree
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Branch,
			&i.Path,
			&i.Status,
			&i.ActiveHolder,
			&i.AcquiredAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.PruneWarningAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const setWorktreeActive = `-- name: SetWorktreeActive :one
UPDATE workspace_worktrees
SET status = 'active',
    active_holder = $2,
    acquired_at = now(),
    last_used_at = now(),
    prune_warning_at = NULL
WHERE id = $1
RETURNING id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

type SetWorktreeActiveParams struct {
	ID           pgtype.UUID `json:"id"`
	ActiveHolder *string     `json:"active_holder"`
}

func (q *Queries) SetWorktreeActive(ctx context.Context, arg SetWorktreeActiveParams) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, setWorktreeActive, arg.ID, arg.ActiveHolder)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const setWorktreeIdle = `-- name: SetWorktreeIdle :one
UPDATE workspace_worktrees
SET status = 'idle',
    active_holder = NULL,
    acquired_at = NULL,
    last_used_at = now(),
    prune_warning_at = NULL
WHERE id = $1
RETURNING id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

func (q *Queries) SetWorktreeIdle(ctx context.Context, id pgtype.UUID) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, setWorktreeIdle, id)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const setWorktreePruning = `-- name: SetWorktreePruning :one
UPDATE workspace_worktrees
SET status = 'pruning', last_used_at = now()
WHERE id = $1
RETURNING id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

func (q *Queries) SetWorktreePruning(ctx context.Context, id pgtype.UUID) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, setWorktreePruning, id)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}

const tryStartPrune = `-- name: TryStartPrune :one
UPDATE workspace_worktrees
   SET status = 'pruning'
 WHERE id = $1 AND status = 'idle'
RETURNING id, workspace_id, branch, path, status, active_holder, acquired_at, last_used_at, created_at, prune_warning_at
`

func (q *Queries) TryStartPrune(ctx context.Context, id pgtype.UUID) (WorkspaceWorktree, error) {
	row := q.db.QueryRow(ctx, tryStartPrune, id)
	var i WorkspaceWorktree
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Branch,
		&i.Path,
		&i.Status,
		&i.ActiveHolder,
		&i.AcquiredAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.PruneWarningAt,
	)
	return i, err
}
