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

package runsqlc

import (
	"context"

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

const createRunProfile = `-- name: CreateRunProfile :one
INSERT INTO workspace_run_profiles (
  id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, created_by
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
RETURNING id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, last_started_at, last_exit_code, last_error, created_by, created_at, updated_at, key_version
`

type CreateRunProfileParams struct {
	ID           pgtype.UUID `json:"id"`
	WorkspaceID  pgtype.UUID `json:"workspace_id"`
	Slug         string      `json:"slug"`
	Name         string      `json:"name"`
	Description  string      `json:"description"`
	Command      string      `json:"command"`
	Args         []string    `json:"args"`
	EncryptedEnv []byte      `json:"encrypted_env"`
	Enabled      bool        `json:"enabled"`
	CreatedBy    pgtype.UUID `json:"created_by"`
}

func (q *Queries) CreateRunProfile(ctx context.Context, arg CreateRunProfileParams) (WorkspaceRunProfile, error) {
	row := q.db.QueryRow(ctx, createRunProfile,
		arg.ID,
		arg.WorkspaceID,
		arg.Slug,
		arg.Name,
		arg.Description,
		arg.Command,
		arg.Args,
		arg.EncryptedEnv,
		arg.Enabled,
		arg.CreatedBy,
	)
	var i WorkspaceRunProfile
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Command,
		&i.Args,
		&i.EncryptedEnv,
		&i.Enabled,
		&i.LastStartedAt,
		&i.LastExitCode,
		&i.LastError,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.KeyVersion,
	)
	return i, err
}

const deleteRunProfile = `-- name: DeleteRunProfile :exec
DELETE FROM workspace_run_profiles WHERE id = $1
`

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

const getRunProfileByID = `-- name: GetRunProfileByID :one
SELECT id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, last_started_at, last_exit_code, last_error, created_by, created_at, updated_at, key_version FROM workspace_run_profiles WHERE id = $1
`

func (q *Queries) GetRunProfileByID(ctx context.Context, id pgtype.UUID) (WorkspaceRunProfile, error) {
	row := q.db.QueryRow(ctx, getRunProfileByID, id)
	var i WorkspaceRunProfile
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Command,
		&i.Args,
		&i.EncryptedEnv,
		&i.Enabled,
		&i.LastStartedAt,
		&i.LastExitCode,
		&i.LastError,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.KeyVersion,
	)
	return i, err
}

const listRunProfilesByWorkspace = `-- name: ListRunProfilesByWorkspace :many
SELECT id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, last_started_at, last_exit_code, last_error, created_by, created_at, updated_at, key_version FROM workspace_run_profiles
WHERE workspace_id = $1
ORDER BY created_at
`

func (q *Queries) ListRunProfilesByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]WorkspaceRunProfile, error) {
	rows, err := q.db.Query(ctx, listRunProfilesByWorkspace, workspaceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceRunProfile
	for rows.Next() {
		var i WorkspaceRunProfile
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Slug,
			&i.Name,
			&i.Description,
			&i.Command,
			&i.Args,
			&i.EncryptedEnv,
			&i.Enabled,
			&i.LastStartedAt,
			&i.LastExitCode,
			&i.LastError,
			&i.CreatedBy,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.KeyVersion,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markRunProfileStarted = `-- name: MarkRunProfileStarted :one
UPDATE workspace_run_profiles
SET last_started_at = now(),
    last_exit_code  = NULL,
    last_error      = '',
    updated_at      = now()
WHERE id = $1
RETURNING id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, last_started_at, last_exit_code, last_error, created_by, created_at, updated_at, key_version
`

func (q *Queries) MarkRunProfileStarted(ctx context.Context, id pgtype.UUID) (WorkspaceRunProfile, error) {
	row := q.db.QueryRow(ctx, markRunProfileStarted, id)
	var i WorkspaceRunProfile
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Command,
		&i.Args,
		&i.EncryptedEnv,
		&i.Enabled,
		&i.LastStartedAt,
		&i.LastExitCode,
		&i.LastError,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.KeyVersion,
	)
	return i, err
}

const updateRunProfile = `-- name: UpdateRunProfile :one
UPDATE workspace_run_profiles
SET slug          = $2,
    name          = $3,
    description   = $4,
    command       = $5,
    args          = $6,
    encrypted_env = $7,
    enabled       = $8,
    updated_at    = now()
WHERE id = $1
RETURNING id, workspace_id, slug, name, description, command, args, encrypted_env, enabled, last_started_at, last_exit_code, last_error, created_by, created_at, updated_at, key_version
`

type UpdateRunProfileParams struct {
	ID           pgtype.UUID `json:"id"`
	Slug         string      `json:"slug"`
	Name         string      `json:"name"`
	Description  string      `json:"description"`
	Command      string      `json:"command"`
	Args         []string    `json:"args"`
	EncryptedEnv []byte      `json:"encrypted_env"`
	Enabled      bool        `json:"enabled"`
}

func (q *Queries) UpdateRunProfile(ctx context.Context, arg UpdateRunProfileParams) (WorkspaceRunProfile, error) {
	row := q.db.QueryRow(ctx, updateRunProfile,
		arg.ID,
		arg.Slug,
		arg.Name,
		arg.Description,
		arg.Command,
		arg.Args,
		arg.EncryptedEnv,
		arg.Enabled,
	)
	var i WorkspaceRunProfile
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Command,
		&i.Args,
		&i.EncryptedEnv,
		&i.Enabled,
		&i.LastStartedAt,
		&i.LastExitCode,
		&i.LastError,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.KeyVersion,
	)
	return i, err
}

const updateRunProfileExit = `-- name: UpdateRunProfileExit :exec
UPDATE workspace_run_profiles
SET last_exit_code = $2,
    last_error     = $3,
    updated_at     = now()
WHERE id = $1
`

type UpdateRunProfileExitParams struct {
	ID           pgtype.UUID `json:"id"`
	LastExitCode *int32      `json:"last_exit_code"`
	LastError    string      `json:"last_error"`
}

func (q *Queries) UpdateRunProfileExit(ctx context.Context, arg UpdateRunProfileExitParams) error {
	_, err := q.db.Exec(ctx, updateRunProfileExit, arg.ID, arg.LastExitCode, arg.LastError)
	return err
}
