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

package projectsqlc

import (
	"context"

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

const closeRequirement = `-- name: CloseRequirement :exec
UPDATE requirements
SET status = 'closed', closed_at = now(), updated_at = now()
WHERE id = $1 AND status = 'open'
`

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

const createRequirement = `-- name: CreateRequirement :one
INSERT INTO requirements (id, project_id, number, title, description, owner_id, workspace_id)
VALUES (
  $1, $2,
  (SELECT COALESCE(MAX(number), 0) + 1 FROM requirements WHERE project_id = $2),
  $3, $4, $5, $6
)
RETURNING id, project_id, number, title, description, phase, status, owner_id,
          closed_at, created_at, updated_at, workspace_id
`

type CreateRequirementParams struct {
	ID          pgtype.UUID `json:"id"`
	ProjectID   pgtype.UUID `json:"project_id"`
	Title       string      `json:"title"`
	Description string      `json:"description"`
	OwnerID     pgtype.UUID `json:"owner_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) CreateRequirement(ctx context.Context, arg CreateRequirementParams) (Requirement, error) {
	row := q.db.QueryRow(ctx, createRequirement,
		arg.ID,
		arg.ProjectID,
		arg.Title,
		arg.Description,
		arg.OwnerID,
		arg.WorkspaceID,
	)
	var i Requirement
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Phase,
		&i.Status,
		&i.OwnerID,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const getRequirementByID = `-- name: GetRequirementByID :one
SELECT id, project_id, number, title, description, phase, status, owner_id,
       closed_at, created_at, updated_at, workspace_id
FROM requirements WHERE id = $1
`

func (q *Queries) GetRequirementByID(ctx context.Context, id pgtype.UUID) (Requirement, error) {
	row := q.db.QueryRow(ctx, getRequirementByID, id)
	var i Requirement
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Phase,
		&i.Status,
		&i.OwnerID,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const getRequirementByNumber = `-- name: GetRequirementByNumber :one
SELECT id, project_id, number, title, description, phase, status, owner_id,
       closed_at, created_at, updated_at, workspace_id
FROM requirements
WHERE project_id = $1 AND number = $2
`

type GetRequirementByNumberParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	Number    int32       `json:"number"`
}

func (q *Queries) GetRequirementByNumber(ctx context.Context, arg GetRequirementByNumberParams) (Requirement, error) {
	row := q.db.QueryRow(ctx, getRequirementByNumber, arg.ProjectID, arg.Number)
	var i Requirement
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Phase,
		&i.Status,
		&i.OwnerID,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const listRequirements = `-- name: ListRequirements :many
SELECT id, project_id, number, title, description, phase, status, owner_id,
       closed_at, created_at, updated_at, workspace_id
FROM requirements
WHERE project_id = $1
  AND ($2::text  IS NULL OR phase  = $2)
  AND ($3::text IS NULL OR status = $3)
ORDER BY number DESC
`

type ListRequirementsParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	Phase     *string     `json:"phase"`
	Status    *string     `json:"status"`
}

func (q *Queries) ListRequirements(ctx context.Context, arg ListRequirementsParams) ([]Requirement, error) {
	rows, err := q.db.Query(ctx, listRequirements, arg.ProjectID, arg.Phase, arg.Status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Requirement
	for rows.Next() {
		var i Requirement
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Phase,
			&i.Status,
			&i.OwnerID,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const reopenRequirement = `-- name: ReopenRequirement :exec
UPDATE requirements
SET status = 'open', closed_at = NULL, updated_at = now()
WHERE id = $1 AND status = 'closed'
`

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

const updateRequirement = `-- name: UpdateRequirement :one
UPDATE requirements
SET title        = $2,
    description  = $3,
    owner_id     = $4,
    workspace_id = $5,
    updated_at   = now()
WHERE id = $1
RETURNING id, project_id, number, title, description, phase, status, owner_id,
          closed_at, created_at, updated_at, workspace_id
`

type UpdateRequirementParams struct {
	ID          pgtype.UUID `json:"id"`
	Title       string      `json:"title"`
	Description string      `json:"description"`
	OwnerID     pgtype.UUID `json:"owner_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) UpdateRequirement(ctx context.Context, arg UpdateRequirementParams) (Requirement, error) {
	row := q.db.QueryRow(ctx, updateRequirement,
		arg.ID,
		arg.Title,
		arg.Description,
		arg.OwnerID,
		arg.WorkspaceID,
	)
	var i Requirement
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Phase,
		&i.Status,
		&i.OwnerID,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const updateRequirementPhase = `-- name: UpdateRequirementPhase :one
UPDATE requirements
SET phase = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_id, number, title, description, phase, status, owner_id,
          closed_at, created_at, updated_at, workspace_id
`

type UpdateRequirementPhaseParams struct {
	ID    pgtype.UUID `json:"id"`
	Phase string      `json:"phase"`
}

func (q *Queries) UpdateRequirementPhase(ctx context.Context, arg UpdateRequirementPhaseParams) (Requirement, error) {
	row := q.db.QueryRow(ctx, updateRequirementPhase, arg.ID, arg.Phase)
	var i Requirement
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Phase,
		&i.Status,
		&i.OwnerID,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}
