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

package orchestratorsqlc

import (
	"context"

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

const getActiveRunByRequirement = `-- name: GetActiveRunByRequirement :one
SELECT id, project_id, requirement_id, workspace_id, owner_id,
       status, current_phase, config, last_error,
       created_at, updated_at, ended_at
FROM orchestrator_runs
WHERE requirement_id = $1
  AND status IN ('pending','running','paused')
ORDER BY created_at DESC
LIMIT 1
`

func (q *Queries) GetActiveRunByRequirement(ctx context.Context, requirementID pgtype.UUID) (OrchestratorRun, error) {
	row := q.db.QueryRow(ctx, getActiveRunByRequirement, requirementID)
	var i OrchestratorRun
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.WorkspaceID,
		&i.OwnerID,
		&i.Status,
		&i.CurrentPhase,
		&i.Config,
		&i.LastError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EndedAt,
	)
	return i, err
}

const getRun = `-- name: GetRun :one
SELECT id, project_id, requirement_id, workspace_id, owner_id,
       status, current_phase, config, last_error,
       created_at, updated_at, ended_at
FROM orchestrator_runs
WHERE id = $1
`

func (q *Queries) GetRun(ctx context.Context, id pgtype.UUID) (OrchestratorRun, error) {
	row := q.db.QueryRow(ctx, getRun, id)
	var i OrchestratorRun
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.WorkspaceID,
		&i.OwnerID,
		&i.Status,
		&i.CurrentPhase,
		&i.Config,
		&i.LastError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EndedAt,
	)
	return i, err
}

const insertRun = `-- name: InsertRun :one
INSERT INTO orchestrator_runs (
  id, project_id, requirement_id, workspace_id, owner_id,
  status, current_phase, config, last_error
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, project_id, requirement_id, workspace_id, owner_id,
          status, current_phase, config, last_error,
          created_at, updated_at, ended_at
`

type InsertRunParams struct {
	ID            pgtype.UUID `json:"id"`
	ProjectID     pgtype.UUID `json:"project_id"`
	RequirementID pgtype.UUID `json:"requirement_id"`
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	OwnerID       pgtype.UUID `json:"owner_id"`
	Status        string      `json:"status"`
	CurrentPhase  string      `json:"current_phase"`
	Config        []byte      `json:"config"`
	LastError     *string     `json:"last_error"`
}

func (q *Queries) InsertRun(ctx context.Context, arg InsertRunParams) (OrchestratorRun, error) {
	row := q.db.QueryRow(ctx, insertRun,
		arg.ID,
		arg.ProjectID,
		arg.RequirementID,
		arg.WorkspaceID,
		arg.OwnerID,
		arg.Status,
		arg.CurrentPhase,
		arg.Config,
		arg.LastError,
	)
	var i OrchestratorRun
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.WorkspaceID,
		&i.OwnerID,
		&i.Status,
		&i.CurrentPhase,
		&i.Config,
		&i.LastError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EndedAt,
	)
	return i, err
}

const listRuns = `-- name: ListRuns :many
SELECT id, project_id, requirement_id, workspace_id, owner_id,
       status, current_phase, config, last_error,
       created_at, updated_at, ended_at
FROM orchestrator_runs
WHERE ($1::uuid IS NULL OR project_id = $1)
  AND ($2::uuid IS NULL OR requirement_id = $2)
  AND ($3::text = '' OR status = $3)
ORDER BY created_at DESC
LIMIT $4
`

type ListRunsParams struct {
	Column1 pgtype.UUID `json:"column_1"`
	Column2 pgtype.UUID `json:"column_2"`
	Column3 string      `json:"column_3"`
	Limit   int32       `json:"limit"`
}

func (q *Queries) ListRuns(ctx context.Context, arg ListRunsParams) ([]OrchestratorRun, error) {
	rows, err := q.db.Query(ctx, listRuns,
		arg.Column1,
		arg.Column2,
		arg.Column3,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []OrchestratorRun
	for rows.Next() {
		var i OrchestratorRun
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.WorkspaceID,
			&i.OwnerID,
			&i.Status,
			&i.CurrentPhase,
			&i.Config,
			&i.LastError,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.EndedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateRunPhase = `-- name: UpdateRunPhase :one
UPDATE orchestrator_runs
SET current_phase = $2,
    updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, workspace_id, owner_id,
          status, current_phase, config, last_error,
          created_at, updated_at, ended_at
`

type UpdateRunPhaseParams struct {
	ID           pgtype.UUID `json:"id"`
	CurrentPhase string      `json:"current_phase"`
}

func (q *Queries) UpdateRunPhase(ctx context.Context, arg UpdateRunPhaseParams) (OrchestratorRun, error) {
	row := q.db.QueryRow(ctx, updateRunPhase, arg.ID, arg.CurrentPhase)
	var i OrchestratorRun
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.WorkspaceID,
		&i.OwnerID,
		&i.Status,
		&i.CurrentPhase,
		&i.Config,
		&i.LastError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EndedAt,
	)
	return i, err
}

const updateRunStatus = `-- name: UpdateRunStatus :one
UPDATE orchestrator_runs
SET status = $2,
    last_error = $3,
    ended_at = CASE WHEN $2 IN ('succeeded','failed','canceled') THEN now() ELSE ended_at END,
    updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, workspace_id, owner_id,
          status, current_phase, config, last_error,
          created_at, updated_at, ended_at
`

type UpdateRunStatusParams struct {
	ID        pgtype.UUID `json:"id"`
	Status    string      `json:"status"`
	LastError *string     `json:"last_error"`
}

func (q *Queries) UpdateRunStatus(ctx context.Context, arg UpdateRunStatusParams) (OrchestratorRun, error) {
	row := q.db.QueryRow(ctx, updateRunStatus, arg.ID, arg.Status, arg.LastError)
	var i OrchestratorRun
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.WorkspaceID,
		&i.OwnerID,
		&i.Status,
		&i.CurrentPhase,
		&i.Config,
		&i.LastError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EndedAt,
	)
	return i, err
}
