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

package changerequestsqlc

import (
	"context"

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

const createChangeRequest = `-- name: CreateChangeRequest :one
INSERT INTO change_requests (
  id, project_id, workspace_id, requirement_id, issue_id, number,
  title, description, source_branch, target_branch,
  state, review_verdict, ci_state, provider, external_id, external_url,
  merge_commit_sha, created_by
) VALUES (
  $1, $2, $3, $4, $5, $6,
  $7, $8, $9, $10,
  'open', 'pending', $11, $12, $13, $14,
  '', $15
)
RETURNING id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at
`

type CreateChangeRequestParams struct {
	ID            pgtype.UUID `json:"id"`
	ProjectID     pgtype.UUID `json:"project_id"`
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RequirementID pgtype.UUID `json:"requirement_id"`
	IssueID       pgtype.UUID `json:"issue_id"`
	Number        int32       `json:"number"`
	Title         string      `json:"title"`
	Description   string      `json:"description"`
	SourceBranch  string      `json:"source_branch"`
	TargetBranch  string      `json:"target_branch"`
	CiState       string      `json:"ci_state"`
	Provider      string      `json:"provider"`
	ExternalID    *int64      `json:"external_id"`
	ExternalUrl   string      `json:"external_url"`
	CreatedBy     pgtype.UUID `json:"created_by"`
}

func (q *Queries) CreateChangeRequest(ctx context.Context, arg CreateChangeRequestParams) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, createChangeRequest,
		arg.ID,
		arg.ProjectID,
		arg.WorkspaceID,
		arg.RequirementID,
		arg.IssueID,
		arg.Number,
		arg.Title,
		arg.Description,
		arg.SourceBranch,
		arg.TargetBranch,
		arg.CiState,
		arg.Provider,
		arg.ExternalID,
		arg.ExternalUrl,
		arg.CreatedBy,
	)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getChangeRequestByID = `-- name: GetChangeRequestByID :one
SELECT id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at FROM change_requests WHERE id = $1
`

func (q *Queries) GetChangeRequestByID(ctx context.Context, id pgtype.UUID) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, getChangeRequestByID, id)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getChangeRequestByNumber = `-- name: GetChangeRequestByNumber :one
SELECT cr.id, cr.project_id, cr.workspace_id, cr.requirement_id, cr.issue_id, cr.number, cr.title, cr.description, cr.source_branch, cr.target_branch, cr.state, cr.review_verdict, cr.ci_state, cr.provider, cr.external_id, cr.external_url, cr.merge_commit_sha, cr.reviewed_by, cr.reviewed_at, cr.created_by, cr.merged_at, cr.created_at, cr.updated_at FROM change_requests cr
JOIN projects p ON p.id = cr.project_id
WHERE p.slug = $1 AND cr.number = $2
`

type GetChangeRequestByNumberParams struct {
	Slug   string `json:"slug"`
	Number int32  `json:"number"`
}

func (q *Queries) GetChangeRequestByNumber(ctx context.Context, arg GetChangeRequestByNumberParams) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, getChangeRequestByNumber, arg.Slug, arg.Number)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listChangeRequestsByProject = `-- name: ListChangeRequestsByProject :many
SELECT id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at FROM change_requests
WHERE project_id = $1
ORDER BY number DESC
`

func (q *Queries) ListChangeRequestsByProject(ctx context.Context, projectID pgtype.UUID) ([]ChangeRequest, error) {
	rows, err := q.db.Query(ctx, listChangeRequestsByProject, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ChangeRequest
	for rows.Next() {
		var i ChangeRequest
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.WorkspaceID,
			&i.RequirementID,
			&i.IssueID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.SourceBranch,
			&i.TargetBranch,
			&i.State,
			&i.ReviewVerdict,
			&i.CiState,
			&i.Provider,
			&i.ExternalID,
			&i.ExternalUrl,
			&i.MergeCommitSha,
			&i.ReviewedBy,
			&i.ReviewedAt,
			&i.CreatedBy,
			&i.MergedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listChangeRequestsByWorkspace = `-- name: ListChangeRequestsByWorkspace :many
SELECT id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at FROM change_requests
WHERE workspace_id = $1
ORDER BY number DESC
`

func (q *Queries) ListChangeRequestsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]ChangeRequest, error) {
	rows, err := q.db.Query(ctx, listChangeRequestsByWorkspace, workspaceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ChangeRequest
	for rows.Next() {
		var i ChangeRequest
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.WorkspaceID,
			&i.RequirementID,
			&i.IssueID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.SourceBranch,
			&i.TargetBranch,
			&i.State,
			&i.ReviewVerdict,
			&i.CiState,
			&i.Provider,
			&i.ExternalID,
			&i.ExternalUrl,
			&i.MergeCommitSha,
			&i.ReviewedBy,
			&i.ReviewedAt,
			&i.CreatedBy,
			&i.MergedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const nextChangeRequestNumber = `-- name: NextChangeRequestNumber :one
SELECT COALESCE(MAX(number), 0) + 1 FROM change_requests WHERE project_id = $1 FOR UPDATE
`

// Per-project monotonic number; FOR UPDATE not needed because the
// max(number)+1 INSERT runs inside the same tx, but we lock existing project
// rows to serialize concurrent creates within a project.
func (q *Queries) NextChangeRequestNumber(ctx context.Context, projectID pgtype.UUID) (int32, error) {
	row := q.db.QueryRow(ctx, nextChangeRequestNumber, projectID)
	var column_1 int32
	err := row.Scan(&column_1)
	return column_1, err
}

const updateChangeRequestCI = `-- name: UpdateChangeRequestCI :one
UPDATE change_requests
SET ci_state   = $2,
    state      = $3,
    updated_at = now()
WHERE id = $1
RETURNING id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at
`

type UpdateChangeRequestCIParams struct {
	ID      pgtype.UUID `json:"id"`
	CiState string      `json:"ci_state"`
	State   string      `json:"state"`
}

func (q *Queries) UpdateChangeRequestCI(ctx context.Context, arg UpdateChangeRequestCIParams) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, updateChangeRequestCI, arg.ID, arg.CiState, arg.State)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateChangeRequestReview = `-- name: UpdateChangeRequestReview :one
UPDATE change_requests
SET review_verdict = $2,
    reviewed_by    = $3,
    reviewed_at    = now(),
    updated_at     = now()
WHERE id = $1
RETURNING id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at
`

type UpdateChangeRequestReviewParams struct {
	ID            pgtype.UUID `json:"id"`
	ReviewVerdict string      `json:"review_verdict"`
	ReviewedBy    pgtype.UUID `json:"reviewed_by"`
}

func (q *Queries) UpdateChangeRequestReview(ctx context.Context, arg UpdateChangeRequestReviewParams) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, updateChangeRequestReview, arg.ID, arg.ReviewVerdict, arg.ReviewedBy)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateChangeRequestState = `-- name: UpdateChangeRequestState :one
UPDATE change_requests
SET state            = $2,
    merge_commit_sha = $3,
    merged_at        = $4,
    updated_at       = now()
WHERE id = $1
RETURNING id, project_id, workspace_id, requirement_id, issue_id, number, title, description, source_branch, target_branch, state, review_verdict, ci_state, provider, external_id, external_url, merge_commit_sha, reviewed_by, reviewed_at, created_by, merged_at, created_at, updated_at
`

type UpdateChangeRequestStateParams struct {
	ID             pgtype.UUID        `json:"id"`
	State          string             `json:"state"`
	MergeCommitSha string             `json:"merge_commit_sha"`
	MergedAt       pgtype.Timestamptz `json:"merged_at"`
}

func (q *Queries) UpdateChangeRequestState(ctx context.Context, arg UpdateChangeRequestStateParams) (ChangeRequest, error) {
	row := q.db.QueryRow(ctx, updateChangeRequestState,
		arg.ID,
		arg.State,
		arg.MergeCommitSha,
		arg.MergedAt,
	)
	var i ChangeRequest
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.WorkspaceID,
		&i.RequirementID,
		&i.IssueID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.State,
		&i.ReviewVerdict,
		&i.CiState,
		&i.Provider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.MergeCommitSha,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedBy,
		&i.MergedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
