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

package projectsqlc

import (
	"context"

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

const approveRequirementArtifact = `-- name: ApproveRequirementArtifact :one
UPDATE requirement_artifacts SET verdict = $2 WHERE id = $1
RETURNING id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
`

type ApproveRequirementArtifactParams struct {
	ID      pgtype.UUID `json:"id"`
	Verdict string      `json:"verdict"`
}

func (q *Queries) ApproveRequirementArtifact(ctx context.Context, arg ApproveRequirementArtifactParams) (RequirementArtifact, error) {
	row := q.db.QueryRow(ctx, approveRequirementArtifact, arg.ID, arg.Verdict)
	var i RequirementArtifact
	err := row.Scan(
		&i.ID,
		&i.RequirementID,
		&i.Phase,
		&i.Version,
		&i.Content,
		&i.Note,
		&i.SourceMessageID,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.Verdict,
	)
	return i, err
}

const createRequirementArtifact = `-- name: CreateRequirementArtifact :one
INSERT INTO requirement_artifacts (id, requirement_id, phase, version, content, note, source_message_id, created_by)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
`

type CreateRequirementArtifactParams struct {
	ID              pgtype.UUID `json:"id"`
	RequirementID   pgtype.UUID `json:"requirement_id"`
	Phase           string      `json:"phase"`
	Version         int32       `json:"version"`
	Content         string      `json:"content"`
	Note            string      `json:"note"`
	SourceMessageID *int64      `json:"source_message_id"`
	CreatedBy       pgtype.UUID `json:"created_by"`
}

func (q *Queries) CreateRequirementArtifact(ctx context.Context, arg CreateRequirementArtifactParams) (RequirementArtifact, error) {
	row := q.db.QueryRow(ctx, createRequirementArtifact,
		arg.ID,
		arg.RequirementID,
		arg.Phase,
		arg.Version,
		arg.Content,
		arg.Note,
		arg.SourceMessageID,
		arg.CreatedBy,
	)
	var i RequirementArtifact
	err := row.Scan(
		&i.ID,
		&i.RequirementID,
		&i.Phase,
		&i.Version,
		&i.Content,
		&i.Note,
		&i.SourceMessageID,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.Verdict,
	)
	return i, err
}

const getMaxRequirementArtifactVersion = `-- name: GetMaxRequirementArtifactVersion :one
SELECT COALESCE(MAX(version), 0)::int FROM requirement_artifacts
WHERE requirement_id = $1 AND phase = $2
`

type GetMaxRequirementArtifactVersionParams struct {
	RequirementID pgtype.UUID `json:"requirement_id"`
	Phase         string      `json:"phase"`
}

func (q *Queries) GetMaxRequirementArtifactVersion(ctx context.Context, arg GetMaxRequirementArtifactVersionParams) (int32, error) {
	row := q.db.QueryRow(ctx, getMaxRequirementArtifactVersion, arg.RequirementID, arg.Phase)
	var column_1 int32
	err := row.Scan(&column_1)
	return column_1, err
}

const getRequirementArtifactByID = `-- name: GetRequirementArtifactByID :one
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE id = $1
`

func (q *Queries) GetRequirementArtifactByID(ctx context.Context, id pgtype.UUID) (RequirementArtifact, error) {
	row := q.db.QueryRow(ctx, getRequirementArtifactByID, id)
	var i RequirementArtifact
	err := row.Scan(
		&i.ID,
		&i.RequirementID,
		&i.Phase,
		&i.Version,
		&i.Content,
		&i.Note,
		&i.SourceMessageID,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.Verdict,
	)
	return i, err
}

const getRequirementArtifactByVersion = `-- name: GetRequirementArtifactByVersion :one
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE requirement_id = $1 AND phase = $2 AND version = $3
`

type GetRequirementArtifactByVersionParams struct {
	RequirementID pgtype.UUID `json:"requirement_id"`
	Phase         string      `json:"phase"`
	Version       int32       `json:"version"`
}

func (q *Queries) GetRequirementArtifactByVersion(ctx context.Context, arg GetRequirementArtifactByVersionParams) (RequirementArtifact, error) {
	row := q.db.QueryRow(ctx, getRequirementArtifactByVersion, arg.RequirementID, arg.Phase, arg.Version)
	var i RequirementArtifact
	err := row.Scan(
		&i.ID,
		&i.RequirementID,
		&i.Phase,
		&i.Version,
		&i.Content,
		&i.Note,
		&i.SourceMessageID,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.Verdict,
	)
	return i, err
}

const listRequirementArtifactsByRequirement = `-- name: ListRequirementArtifactsByRequirement :many
SELECT id, requirement_id, phase, version, content, note, source_message_id, created_by, created_at, verdict
FROM requirement_artifacts
WHERE requirement_id = $1 AND ($2::text = '' OR phase = $2)
ORDER BY phase ASC, version ASC
`

type ListRequirementArtifactsByRequirementParams struct {
	RequirementID pgtype.UUID `json:"requirement_id"`
	Column2       string      `json:"column_2"`
}

func (q *Queries) ListRequirementArtifactsByRequirement(ctx context.Context, arg ListRequirementArtifactsByRequirementParams) ([]RequirementArtifact, error) {
	rows, err := q.db.Query(ctx, listRequirementArtifactsByRequirement, arg.RequirementID, arg.Column2)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []RequirementArtifact
	for rows.Next() {
		var i RequirementArtifact
		if err := rows.Scan(
			&i.ID,
			&i.RequirementID,
			&i.Phase,
			&i.Version,
			&i.Content,
			&i.Note,
			&i.SourceMessageID,
			&i.CreatedBy,
			&i.CreatedAt,
			&i.Verdict,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
