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

package sqlc

import (
	"context"

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

const bulkDeleteRolesFromUser = `-- name: BulkDeleteRolesFromUser :exec
DELETE FROM user_roles 
WHERE user_id = $1
`

func (q *Queries) BulkDeleteRolesFromUser(ctx context.Context, userID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, bulkDeleteRolesFromUser, userID)
	return err
}

const bulkDeleteUsersFromRole = `-- name: BulkDeleteUsersFromRole :exec
DELETE FROM user_roles 
WHERE role_id = $1
`

func (q *Queries) BulkDeleteUsersFromRole(ctx context.Context, roleID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, bulkDeleteUsersFromRole, roleID)
	return err
}

const createRole = `-- name: CreateRole :one
INSERT INTO roles (name)
VALUES ($1)
RETURNING id, name, is_deleted, created_at, updated_at
`

func (q *Queries) CreateRole(ctx context.Context, name string) (Role, error) {
	row := q.db.QueryRow(ctx, createRole, name)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createUserRole = `-- name: CreateUserRole :exec
INSERT INTO user_roles (user_id, role_id)
SELECT $1, unnest($2::uuid[])
ON CONFLICT DO NOTHING
`

type CreateUserRoleParams struct {
	UserID  pgtype.UUID   `json:"user_id"`
	Column2 []pgtype.UUID `json:"column_2"`
}

func (q *Queries) CreateUserRole(ctx context.Context, arg CreateUserRoleParams) error {
	_, err := q.db.Exec(ctx, createUserRole, arg.UserID, arg.Column2)
	return err
}

const deleteRole = `-- name: DeleteRole :exec
UPDATE roles
SET
    is_deleted = true,
    updated_at = now()
WHERE id = $1
`

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

const deleteUserRole = `-- name: DeleteUserRole :exec
DELETE FROM user_roles ur
USING roles r
WHERE ur.role_id = r.id
  AND ur.user_id = $1
  AND r.name = $2
`

type DeleteUserRoleParams struct {
	UserID pgtype.UUID `json:"user_id"`
	Name   string      `json:"name"`
}

func (q *Queries) DeleteUserRole(ctx context.Context, arg DeleteUserRoleParams) error {
	_, err := q.db.Exec(ctx, deleteUserRole, arg.UserID, arg.Name)
	return err
}

const getRoleByID = `-- name: GetRoleByID :one
SELECT id, name, is_deleted, created_at, updated_at  FROM roles
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetRoleByID(ctx context.Context, id pgtype.UUID) (Role, error) {
	row := q.db.QueryRow(ctx, getRoleByID, id)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getRoleByName = `-- name: GetRoleByName :one
SELECT id, name, is_deleted, created_at, updated_at FROM roles
WHERE name = $1 AND is_deleted = false
`

func (q *Queries) GetRoleByName(ctx context.Context, name string) (Role, error) {
	row := q.db.QueryRow(ctx, getRoleByName, name)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getRoles = `-- name: GetRoles :many
SELECT id, name, is_deleted, created_at, updated_at
FROM roles
WHERE is_deleted = false
`

func (q *Queries) GetRoles(ctx context.Context) ([]Role, error) {
	rows, err := q.db.Query(ctx, getRoles)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Role{}
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&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 getRolesByIDs = `-- name: GetRolesByIDs :many
SELECT id, name, is_deleted, created_at, updated_at
FROM roles
WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetRolesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Role, error) {
	rows, err := q.db.Query(ctx, getRolesByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Role{}
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&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 restoreRole = `-- name: RestoreRole :exec
UPDATE roles
SET
    is_deleted = false,
    updated_at = now()
WHERE id = $1
`

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

const updateRole = `-- name: UpdateRole :one
UPDATE roles 
SET 
  name = $1,
  updated_at = now()
WHERE id = $2 AND is_deleted = false
RETURNING id, name, is_deleted, created_at, updated_at
`

type UpdateRoleParams struct {
	Name string      `json:"name"`
	ID   pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error) {
	row := q.db.QueryRow(ctx, updateRole, arg.Name, arg.ID)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
