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

package codeindexsqlc

import (
	"context"

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

const getLatestCompletedRun = `-- name: GetLatestCompletedRun :one
SELECT id, workspace_id, worktree_id, branch, commit_sha, status,
       embedding_endpoint_id, embedding_model, dims,
       files_indexed, chunks_indexed, error, started_at, finished_at, created_at
FROM code_index_runs
WHERE workspace_id = $1 AND status = 'completed'
ORDER BY created_at DESC
LIMIT 1
`

func (q *Queries) GetLatestCompletedRun(ctx context.Context, workspaceID pgtype.UUID) (CodeIndexRun, error) {
	row := q.db.QueryRow(ctx, getLatestCompletedRun, workspaceID)
	var i CodeIndexRun
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.WorktreeID,
		&i.Branch,
		&i.CommitSha,
		&i.Status,
		&i.EmbeddingEndpointID,
		&i.EmbeddingModel,
		&i.Dims,
		&i.FilesIndexed,
		&i.ChunksIndexed,
		&i.Error,
		&i.StartedAt,
		&i.FinishedAt,
		&i.CreatedAt,
	)
	return i, err
}

const getRun = `-- name: GetRun :one
SELECT id, workspace_id, worktree_id, branch, commit_sha, status,
       embedding_endpoint_id, embedding_model, dims,
       files_indexed, chunks_indexed, error, started_at, finished_at, created_at
FROM code_index_runs
WHERE id = $1
`

func (q *Queries) GetRun(ctx context.Context, id pgtype.UUID) (CodeIndexRun, error) {
	row := q.db.QueryRow(ctx, getRun, id)
	var i CodeIndexRun
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.WorktreeID,
		&i.Branch,
		&i.CommitSha,
		&i.Status,
		&i.EmbeddingEndpointID,
		&i.EmbeddingModel,
		&i.Dims,
		&i.FilesIndexed,
		&i.ChunksIndexed,
		&i.Error,
		&i.StartedAt,
		&i.FinishedAt,
		&i.CreatedAt,
	)
	return i, err
}

const getRunByCommit = `-- name: GetRunByCommit :one
SELECT id, workspace_id, worktree_id, branch, commit_sha, status,
       embedding_endpoint_id, embedding_model, dims,
       files_indexed, chunks_indexed, error, started_at, finished_at, created_at
FROM code_index_runs
WHERE workspace_id = $1 AND commit_sha = $2 AND embedding_model = $3
`

type GetRunByCommitParams struct {
	WorkspaceID    pgtype.UUID `json:"workspace_id"`
	CommitSha      string      `json:"commit_sha"`
	EmbeddingModel string      `json:"embedding_model"`
}

func (q *Queries) GetRunByCommit(ctx context.Context, arg GetRunByCommitParams) (CodeIndexRun, error) {
	row := q.db.QueryRow(ctx, getRunByCommit, arg.WorkspaceID, arg.CommitSha, arg.EmbeddingModel)
	var i CodeIndexRun
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.WorktreeID,
		&i.Branch,
		&i.CommitSha,
		&i.Status,
		&i.EmbeddingEndpointID,
		&i.EmbeddingModel,
		&i.Dims,
		&i.FilesIndexed,
		&i.ChunksIndexed,
		&i.Error,
		&i.StartedAt,
		&i.FinishedAt,
		&i.CreatedAt,
	)
	return i, err
}

const insertRun = `-- name: InsertRun :one
INSERT INTO code_index_runs (
  id, workspace_id, worktree_id, branch, commit_sha, status,
  embedding_endpoint_id, embedding_model, dims
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, workspace_id, worktree_id, branch, commit_sha, status,
          embedding_endpoint_id, embedding_model, dims,
          files_indexed, chunks_indexed, error, started_at, finished_at, created_at
`

type InsertRunParams struct {
	ID                  pgtype.UUID `json:"id"`
	WorkspaceID         pgtype.UUID `json:"workspace_id"`
	WorktreeID          pgtype.UUID `json:"worktree_id"`
	Branch              string      `json:"branch"`
	CommitSha           string      `json:"commit_sha"`
	Status              string      `json:"status"`
	EmbeddingEndpointID pgtype.UUID `json:"embedding_endpoint_id"`
	EmbeddingModel      string      `json:"embedding_model"`
	Dims                int32       `json:"dims"`
}

func (q *Queries) InsertRun(ctx context.Context, arg InsertRunParams) (CodeIndexRun, error) {
	row := q.db.QueryRow(ctx, insertRun,
		arg.ID,
		arg.WorkspaceID,
		arg.WorktreeID,
		arg.Branch,
		arg.CommitSha,
		arg.Status,
		arg.EmbeddingEndpointID,
		arg.EmbeddingModel,
		arg.Dims,
	)
	var i CodeIndexRun
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.WorktreeID,
		&i.Branch,
		&i.CommitSha,
		&i.Status,
		&i.EmbeddingEndpointID,
		&i.EmbeddingModel,
		&i.Dims,
		&i.FilesIndexed,
		&i.ChunksIndexed,
		&i.Error,
		&i.StartedAt,
		&i.FinishedAt,
		&i.CreatedAt,
	)
	return i, err
}

const markRunCompleted = `-- name: MarkRunCompleted :exec
UPDATE code_index_runs
SET status = 'completed', files_indexed = $2, chunks_indexed = $3,
    error = NULL, finished_at = now()
WHERE id = $1
`

type MarkRunCompletedParams struct {
	ID            pgtype.UUID `json:"id"`
	FilesIndexed  int32       `json:"files_indexed"`
	ChunksIndexed int32       `json:"chunks_indexed"`
}

func (q *Queries) MarkRunCompleted(ctx context.Context, arg MarkRunCompletedParams) error {
	_, err := q.db.Exec(ctx, markRunCompleted, arg.ID, arg.FilesIndexed, arg.ChunksIndexed)
	return err
}

const markRunFailed = `-- name: MarkRunFailed :exec
UPDATE code_index_runs
SET status = 'failed', error = $2, finished_at = now()
WHERE id = $1
`

type MarkRunFailedParams struct {
	ID    pgtype.UUID `json:"id"`
	Error *string     `json:"error"`
}

func (q *Queries) MarkRunFailed(ctx context.Context, arg MarkRunFailedParams) error {
	_, err := q.db.Exec(ctx, markRunFailed, arg.ID, arg.Error)
	return err
}

const markRunRunning = `-- name: MarkRunRunning :exec
UPDATE code_index_runs
SET status = 'running', started_at = now()
WHERE id = $1
`

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

const markRunsStale = `-- name: MarkRunsStale :exec
UPDATE code_index_runs
SET status = 'stale'
WHERE workspace_id = $1 AND status IN ('pending','running','completed')
`

func (q *Queries) MarkRunsStale(ctx context.Context, workspaceID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, markRunsStale, workspaceID)
	return err
}

const markStuckRunsStale = `-- name: MarkStuckRunsStale :many
UPDATE code_index_runs
SET status = 'stale'
WHERE status = 'running' AND started_at < $1
RETURNING id
`

func (q *Queries) MarkStuckRunsStale(ctx context.Context, startedAt pgtype.Timestamptz) ([]pgtype.UUID, error) {
	rows, err := q.db.Query(ctx, markStuckRunsStale, startedAt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []pgtype.UUID
	for rows.Next() {
		var id pgtype.UUID
		if err := rows.Scan(&id); err != nil {
			return nil, err
		}
		items = append(items, id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
