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

package projectsqlc

import (
	"context"

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

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

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

const createIssue = `-- name: CreateIssue :one
INSERT INTO issues (id, project_id, requirement_id, number, title, description, assignee_id, created_by, workspace_id, parent_id, priority)
VALUES (
  $1, $2, $3,
  (SELECT COALESCE(MAX(number), 0) + 1 FROM issues WHERE project_id = $2),
  $4, $5, $6, $7, $8, $9, $10
)
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
`

type CreateIssueParams struct {
	ID            pgtype.UUID `json:"id"`
	ProjectID     pgtype.UUID `json:"project_id"`
	RequirementID pgtype.UUID `json:"requirement_id"`
	Title         string      `json:"title"`
	Description   string      `json:"description"`
	AssigneeID    pgtype.UUID `json:"assignee_id"`
	CreatedBy     pgtype.UUID `json:"created_by"`
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	ParentID      pgtype.UUID `json:"parent_id"`
	Priority      int32       `json:"priority"`
}

func (q *Queries) CreateIssue(ctx context.Context, arg CreateIssueParams) (Issue, error) {
	row := q.db.QueryRow(ctx, createIssue,
		arg.ID,
		arg.ProjectID,
		arg.RequirementID,
		arg.Title,
		arg.Description,
		arg.AssigneeID,
		arg.CreatedBy,
		arg.WorkspaceID,
		arg.ParentID,
		arg.Priority,
	)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}

const getIssueByID = `-- name: GetIssueByID :one
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE id = $1
`

func (q *Queries) GetIssueByID(ctx context.Context, id pgtype.UUID) (Issue, error) {
	row := q.db.QueryRow(ctx, getIssueByID, id)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}

const getIssueByNumber = `-- name: GetIssueByNumber :one
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE project_id = $1 AND number = $2
`

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

func (q *Queries) GetIssueByNumber(ctx context.Context, arg GetIssueByNumberParams) (Issue, error) {
	row := q.db.QueryRow(ctx, getIssueByNumber, arg.ProjectID, arg.Number)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}

const listIssues = `-- name: ListIssues :many
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE project_id = $1
  AND ($2::text  IS NULL OR status = $2)
  AND (
    $3::text IS NULL
    OR ($3 = 'none' AND requirement_id IS NULL)
    OR ($3 = 'id'   AND requirement_id = $4)
  )
  AND ($5::uuid IS NULL OR assignee_id = $5)
ORDER BY number DESC
`

type ListIssuesParams struct {
	ProjectID         pgtype.UUID `json:"project_id"`
	Status            *string     `json:"status"`
	RequirementFilter *string     `json:"requirement_filter"`
	RequirementID     pgtype.UUID `json:"requirement_id"`
	AssigneeID        pgtype.UUID `json:"assignee_id"`
}

func (q *Queries) ListIssues(ctx context.Context, arg ListIssuesParams) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listIssues,
		arg.ProjectID,
		arg.Status,
		arg.RequirementFilter,
		arg.RequirementID,
		arg.AssigneeID,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listIssuesByRequirement = `-- name: ListIssuesByRequirement :many
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE requirement_id = $1
ORDER BY number DESC
`

func (q *Queries) ListIssuesByRequirement(ctx context.Context, requirementID pgtype.UUID) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listIssuesByRequirement, requirementID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

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

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

const updateIssueAssignee = `-- name: UpdateIssueAssignee :one
UPDATE issues
SET assignee_id = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
`

type UpdateIssueAssigneeParams struct {
	ID         pgtype.UUID `json:"id"`
	AssigneeID pgtype.UUID `json:"assignee_id"`
}

func (q *Queries) UpdateIssueAssignee(ctx context.Context, arg UpdateIssueAssigneeParams) (Issue, error) {
	row := q.db.QueryRow(ctx, updateIssueAssignee, arg.ID, arg.AssigneeID)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}

const updateIssueCore = `-- name: UpdateIssueCore :one
UPDATE issues
SET title          = $2,
    description    = $3,
    requirement_id = $4,
    workspace_id   = $5,
    updated_at     = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
`

type UpdateIssueCoreParams struct {
	ID            pgtype.UUID `json:"id"`
	Title         string      `json:"title"`
	Description   string      `json:"description"`
	RequirementID pgtype.UUID `json:"requirement_id"`
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) UpdateIssueCore(ctx context.Context, arg UpdateIssueCoreParams) (Issue, error) {
	row := q.db.QueryRow(ctx, updateIssueCore,
		arg.ID,
		arg.Title,
		arg.Description,
		arg.RequirementID,
		arg.WorkspaceID,
	)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}

const updateIssuePriority = `-- name: UpdateIssuePriority :one
UPDATE issues
SET priority = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_id, requirement_id, number, title, description, status,
          assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
`

type UpdateIssuePriorityParams struct {
	ID       pgtype.UUID `json:"id"`
	Priority int32       `json:"priority"`
}

func (q *Queries) UpdateIssuePriority(ctx context.Context, arg UpdateIssuePriorityParams) (Issue, error) {
	row := q.db.QueryRow(ctx, updateIssuePriority, arg.ID, arg.Priority)
	var i Issue
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.RequirementID,
		&i.Number,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.AssigneeID,
		&i.CreatedBy,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
		&i.ParentID,
		&i.Priority,
	)
	return i, err
}
