// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: entities.sql

package sqlc

import (
	"context"

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

const createEntity = `-- name: CreateEntity :one
INSERT INTO entities (
    id, name, slug, description, project_id, status, time_start, time_end
) VALUES (
    COALESCE($8::uuid, uuidv7()), $1, $2, $3, $4, $5, $6, $7
)
RETURNING id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
`

type CreateEntityParams struct {
	Name        string      `json:"name"`
	Slug        pgtype.Text `json:"slug"`
	Description pgtype.Text `json:"description"`
	ProjectID   pgtype.UUID `json:"project_id"`
	Status      pgtype.Int2 `json:"status"`
	TimeStart   pgtype.Int4 `json:"time_start"`
	TimeEnd     pgtype.Int4 `json:"time_end"`
	ID          pgtype.UUID `json:"id"`
}

func (q *Queries) CreateEntity(ctx context.Context, arg CreateEntityParams) (Entity, error) {
	row := q.db.QueryRow(ctx, createEntity,
		arg.Name,
		arg.Slug,
		arg.Description,
		arg.ProjectID,
		arg.Status,
		arg.TimeStart,
		arg.TimeEnd,
		arg.ID,
	)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Slug,
		&i.Description,
		&i.Status,
		&i.TimeStart,
		&i.TimeEnd,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteEntitiesByIDs = `-- name: DeleteEntitiesByIDs :exec
UPDATE entities
SET is_deleted = true
WHERE id = ANY($1::uuid[])
`

func (q *Queries) DeleteEntitiesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteEntitiesByIDs, dollar_1)
	return err
}

const deleteEntity = `-- name: DeleteEntity :exec
UPDATE entities
SET 
    is_deleted = true
WHERE id = $1
`

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

const getEntitiesByIDs = `-- name: GetEntitiesByIDs :many
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at FROM entities WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetEntitiesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Entity, error) {
	rows, err := q.db.Query(ctx, getEntitiesByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Entity{}
	for rows.Next() {
		var i Entity
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Name,
			&i.Slug,
			&i.Description,
			&i.Status,
			&i.TimeStart,
			&i.TimeEnd,
			&i.IsDeleted,
			&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 getEntitiesByProjectId = `-- name: GetEntitiesByProjectId :many
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
FROM entities
WHERE project_id = $1 AND is_deleted = false
`

func (q *Queries) GetEntitiesByProjectId(ctx context.Context, projectID pgtype.UUID) ([]Entity, error) {
	rows, err := q.db.Query(ctx, getEntitiesByProjectId, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Entity{}
	for rows.Next() {
		var i Entity
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Name,
			&i.Slug,
			&i.Description,
			&i.Status,
			&i.TimeStart,
			&i.TimeEnd,
			&i.IsDeleted,
			&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 getEntitiesBySlugs = `-- name: GetEntitiesBySlugs :many
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at FROM entities WHERE slug = ANY($1::text[]) AND is_deleted = false
`

func (q *Queries) GetEntitiesBySlugs(ctx context.Context, dollar_1 []string) ([]Entity, error) {
	rows, err := q.db.Query(ctx, getEntitiesBySlugs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Entity{}
	for rows.Next() {
		var i Entity
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Name,
			&i.Slug,
			&i.Description,
			&i.Status,
			&i.TimeStart,
			&i.TimeEnd,
			&i.IsDeleted,
			&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 getEntityById = `-- name: GetEntityById :one
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
FROM entities
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetEntityById(ctx context.Context, id pgtype.UUID) (Entity, error) {
	row := q.db.QueryRow(ctx, getEntityById, id)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Slug,
		&i.Description,
		&i.Status,
		&i.TimeStart,
		&i.TimeEnd,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getEntityBySlug = `-- name: GetEntityBySlug :one
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
FROM entities
WHERE slug = $1 AND is_deleted = false
`

func (q *Queries) GetEntityBySlug(ctx context.Context, slug pgtype.Text) (Entity, error) {
	row := q.db.QueryRow(ctx, getEntityBySlug, slug)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Slug,
		&i.Description,
		&i.Status,
		&i.TimeStart,
		&i.TimeEnd,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const searchEntities = `-- name: SearchEntities :many
SELECT id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
FROM entities
WHERE is_deleted = false
  AND ($1::uuid IS NULL OR project_id = $1::uuid)
  AND ($2::text IS NULL OR name ILIKE '%' || $2::text || '%')
  AND (
    $3::int IS NULL OR
    int4range(time_start, time_end, '[]') @> $3::int
  )
  AND ($4::uuid IS NULL OR id < $4::uuid)
ORDER BY id DESC
LIMIT $5
`

type SearchEntitiesParams struct {
	ProjectID  pgtype.UUID `json:"project_id"`
	Name       pgtype.Text `json:"name"`
	TimePoint  pgtype.Int4 `json:"time_point"`
	CursorID   pgtype.UUID `json:"cursor_id"`
	LimitCount int32       `json:"limit_count"`
}

func (q *Queries) SearchEntities(ctx context.Context, arg SearchEntitiesParams) ([]Entity, error) {
	rows, err := q.db.Query(ctx, searchEntities,
		arg.ProjectID,
		arg.Name,
		arg.TimePoint,
		arg.CursorID,
		arg.LimitCount,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Entity{}
	for rows.Next() {
		var i Entity
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Name,
			&i.Slug,
			&i.Description,
			&i.Status,
			&i.TimeStart,
			&i.TimeEnd,
			&i.IsDeleted,
			&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 updateEntity = `-- name: UpdateEntity :one
UPDATE entities
SET 
    name = COALESCE($1, name),
    slug = COALESCE($2, slug),
    description = COALESCE($3, description),
    project_id = COALESCE($4, project_id),
    status = COALESCE($5, status),
    time_start = COALESCE($6, time_start),
    time_end = COALESCE($7, time_end)
WHERE id = $8 AND is_deleted = false
RETURNING id, project_id, name, slug, description, status, time_start, time_end, is_deleted, created_at, updated_at
`

type UpdateEntityParams struct {
	Name        pgtype.Text `json:"name"`
	Slug        pgtype.Text `json:"slug"`
	Description pgtype.Text `json:"description"`
	ProjectID   pgtype.UUID `json:"project_id"`
	Status      pgtype.Int2 `json:"status"`
	TimeStart   pgtype.Int4 `json:"time_start"`
	TimeEnd     pgtype.Int4 `json:"time_end"`
	ID          pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateEntity(ctx context.Context, arg UpdateEntityParams) (Entity, error) {
	row := q.db.QueryRow(ctx, updateEntity,
		arg.Name,
		arg.Slug,
		arg.Description,
		arg.ProjectID,
		arg.Status,
		arg.TimeStart,
		arg.TimeEnd,
		arg.ID,
	)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Slug,
		&i.Description,
		&i.Status,
		&i.TimeStart,
		&i.TimeEnd,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
