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

package projectsqlc

import (
	"context"

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

const archiveProject = `-- name: ArchiveProject :exec
UPDATE projects SET archived_at = now(), updated_at = now()
WHERE id = $1 AND archived_at IS NULL
`

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

const createProject = `-- name: CreateProject :one
INSERT INTO projects (id, slug, name, description, visibility, owner_id)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, slug, name, description, visibility, owner_id, archived_at, created_at, updated_at
`

type CreateProjectParams struct {
	ID          pgtype.UUID `json:"id"`
	Slug        string      `json:"slug"`
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Visibility  string      `json:"visibility"`
	OwnerID     pgtype.UUID `json:"owner_id"`
}

func (q *Queries) CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, createProject,
		arg.ID,
		arg.Slug,
		arg.Name,
		arg.Description,
		arg.Visibility,
		arg.OwnerID,
	)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Visibility,
		&i.OwnerID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getProjectByID = `-- name: GetProjectByID :one
SELECT id, slug, name, description, visibility, owner_id, archived_at, created_at, updated_at
FROM projects WHERE id = $1
`

func (q *Queries) GetProjectByID(ctx context.Context, id pgtype.UUID) (Project, error) {
	row := q.db.QueryRow(ctx, getProjectByID, id)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Visibility,
		&i.OwnerID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getProjectBySlug = `-- name: GetProjectBySlug :one
SELECT id, slug, name, description, visibility, owner_id, archived_at, created_at, updated_at
FROM projects WHERE slug = $1
`

func (q *Queries) GetProjectBySlug(ctx context.Context, slug string) (Project, error) {
	row := q.db.QueryRow(ctx, getProjectBySlug, slug)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Visibility,
		&i.OwnerID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listProjects = `-- name: ListProjects :many
SELECT id, slug, name, description, visibility, owner_id, archived_at, created_at, updated_at
FROM projects
WHERE ($1::bool OR archived_at IS NULL)
ORDER BY created_at DESC
`

func (q *Queries) ListProjects(ctx context.Context, includeArchived bool) ([]Project, error) {
	rows, err := q.db.Query(ctx, listProjects, includeArchived)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Project
	for rows.Next() {
		var i Project
		if err := rows.Scan(
			&i.ID,
			&i.Slug,
			&i.Name,
			&i.Description,
			&i.Visibility,
			&i.OwnerID,
			&i.ArchivedAt,
			&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 unarchiveProject = `-- name: UnarchiveProject :exec
UPDATE projects SET archived_at = NULL, updated_at = now()
WHERE id = $1 AND archived_at IS NOT NULL
`

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

const updateProject = `-- name: UpdateProject :one
UPDATE projects
SET name        = $2,
    description = $3,
    visibility  = $4,
    updated_at  = now()
WHERE id = $1
RETURNING id, slug, name, description, visibility, owner_id, archived_at, created_at, updated_at
`

type UpdateProjectParams struct {
	ID          pgtype.UUID `json:"id"`
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Visibility  string      `json:"visibility"`
}

func (q *Queries) UpdateProject(ctx context.Context, arg UpdateProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, updateProject,
		arg.ID,
		arg.Name,
		arg.Description,
		arg.Visibility,
	)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.Visibility,
		&i.OwnerID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
