// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: user.sql

package db

import (
	"context"
	"database/sql"
)

const countRecoveryCodes = `-- name: CountRecoveryCodes :one
SELECT COUNT(code_hash) FROM recovery_codes WHERE user_id = ?
`

func (q *Queries) CountRecoveryCodes(ctx context.Context, userID string) (int64, error) {
	row := q.db.QueryRowContext(ctx, countRecoveryCodes, userID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createChangeEmailRequest = `-- name: CreateChangeEmailRequest :execresult
UPDATE users SET new_email = ?, new_email_token = ?, new_email_expires = ? WHERE id = ?
`

type CreateChangeEmailRequestParams struct {
	NewEmail        sql.NullString
	NewEmailToken   []byte
	NewEmailExpires sql.NullInt64
	ID              string
}

func (q *Queries) CreateChangeEmailRequest(ctx context.Context, arg CreateChangeEmailRequestParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, createChangeEmailRequest,
		arg.NewEmail,
		arg.NewEmailToken,
		arg.NewEmailExpires,
		arg.ID,
	)
}

const createRecoveryCode = `-- name: CreateRecoveryCode :exec
INSERT INTO recovery_codes (created_at,user_id,code_hash) VALUES (?,?,?)
`

type CreateRecoveryCodeParams struct {
	CreatedAt int64
	UserID    string
	CodeHash  []byte
}

func (q *Queries) CreateRecoveryCode(ctx context.Context, arg CreateRecoveryCodeParams) error {
	_, err := q.db.ExecContext(ctx, createRecoveryCode, arg.CreatedAt, arg.UserID, arg.CodeHash)
	return err
}

const createUser = `-- name: CreateUser :one
INSERT INTO users (
  id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url
) VALUES (
  ?, ?, ?, ?, ?, ?, ?, ?
) RETURNING id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url, new_email, new_email_token, new_email_expires, admin
`

type CreateUserParams struct {
	ID             string
	CreatedAt      int64
	Name           string
	Email          string
	EmailConfirmed bool
	PasswordHash   []byte
	OtpActive      bool
	OtpUrl         string
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
	row := q.db.QueryRowContext(ctx, createUser,
		arg.ID,
		arg.CreatedAt,
		arg.Name,
		arg.Email,
		arg.EmailConfirmed,
		arg.PasswordHash,
		arg.OtpActive,
		arg.OtpUrl,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.CreatedAt,
		&i.Name,
		&i.Email,
		&i.EmailConfirmed,
		&i.PasswordHash,
		&i.OtpActive,
		&i.OtpUrl,
		&i.NewEmail,
		&i.NewEmailToken,
		&i.NewEmailExpires,
		&i.Admin,
	)
	return i, err
}

const deleteRecoveryCode = `-- name: DeleteRecoveryCode :execresult
DELETE FROM recovery_codes WHERE user_id = ? AND code_hash = ?
`

type DeleteRecoveryCodeParams struct {
	UserID   string
	CodeHash []byte
}

func (q *Queries) DeleteRecoveryCode(ctx context.Context, arg DeleteRecoveryCodeParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteRecoveryCode, arg.UserID, arg.CodeHash)
}

const deleteRecoveryCodes = `-- name: DeleteRecoveryCodes :execresult
DELETE FROM recovery_codes WHERE user_id = ?
`

func (q *Queries) DeleteRecoveryCodes(ctx context.Context, userID string) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteRecoveryCodes, userID)
}

const deleteUser = `-- name: DeleteUser :execresult
DELETE FROM users WHERE id = ?
`

func (q *Queries) DeleteUser(ctx context.Context, id string) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteUser, id)
}

const findUser = `-- name: FindUser :one
SELECT id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url, new_email, new_email_token, new_email_expires, admin FROM users WHERE id = ?
`

func (q *Queries) FindUser(ctx context.Context, id string) (User, error) {
	row := q.db.QueryRowContext(ctx, findUser, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.CreatedAt,
		&i.Name,
		&i.Email,
		&i.EmailConfirmed,
		&i.PasswordHash,
		&i.OtpActive,
		&i.OtpUrl,
		&i.NewEmail,
		&i.NewEmailToken,
		&i.NewEmailExpires,
		&i.Admin,
	)
	return i, err
}

const findUserByChangeEmailToken = `-- name: FindUserByChangeEmailToken :one
SELECT id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url, new_email, new_email_token, new_email_expires, admin FROM users WHERE new_email_token = ? AND new_email_expires > ?2
`

type FindUserByChangeEmailTokenParams struct {
	NewEmailToken []byte
	Now           sql.NullInt64
}

func (q *Queries) FindUserByChangeEmailToken(ctx context.Context, arg FindUserByChangeEmailTokenParams) (User, error) {
	row := q.db.QueryRowContext(ctx, findUserByChangeEmailToken, arg.NewEmailToken, arg.Now)
	var i User
	err := row.Scan(
		&i.ID,
		&i.CreatedAt,
		&i.Name,
		&i.Email,
		&i.EmailConfirmed,
		&i.PasswordHash,
		&i.OtpActive,
		&i.OtpUrl,
		&i.NewEmail,
		&i.NewEmailToken,
		&i.NewEmailExpires,
		&i.Admin,
	)
	return i, err
}

const findUserByEmail = `-- name: FindUserByEmail :one
SELECT id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url, new_email, new_email_token, new_email_expires, admin FROM users WHERE email = ?
`

func (q *Queries) FindUserByEmail(ctx context.Context, email string) (User, error) {
	row := q.db.QueryRowContext(ctx, findUserByEmail, email)
	var i User
	err := row.Scan(
		&i.ID,
		&i.CreatedAt,
		&i.Name,
		&i.Email,
		&i.EmailConfirmed,
		&i.PasswordHash,
		&i.OtpActive,
		&i.OtpUrl,
		&i.NewEmail,
		&i.NewEmailToken,
		&i.NewEmailExpires,
		&i.Admin,
	)
	return i, err
}

const findUsers = `-- name: FindUsers :many
SELECT id, created_at, name, email, email_confirmed, password_hash, otp_active, otp_url, new_email, new_email_token, new_email_expires, admin FROM users
`

func (q *Queries) FindUsers(ctx context.Context) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, findUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []User
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.CreatedAt,
			&i.Name,
			&i.Email,
			&i.EmailConfirmed,
			&i.PasswordHash,
			&i.OtpActive,
			&i.OtpUrl,
			&i.NewEmail,
			&i.NewEmailToken,
			&i.NewEmailExpires,
			&i.Admin,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getOTP = `-- name: GetOTP :one
SELECT otp_active,otp_url FROM users WHERE id = ?
`

type GetOTPRow struct {
	OtpActive bool
	OtpUrl    string
}

func (q *Queries) GetOTP(ctx context.Context, id string) (GetOTPRow, error) {
	row := q.db.QueryRowContext(ctx, getOTP, id)
	var i GetOTPRow
	err := row.Scan(&i.OtpActive, &i.OtpUrl)
	return i, err
}

const getUserPasswordHash = `-- name: GetUserPasswordHash :one
SELECT password_hash FROM users WHERE id = ?
`

func (q *Queries) GetUserPasswordHash(ctx context.Context, id string) ([]byte, error) {
	row := q.db.QueryRowContext(ctx, getUserPasswordHash, id)
	var password_hash []byte
	err := row.Scan(&password_hash)
	return password_hash, err
}

const setOTPActive = `-- name: SetOTPActive :execresult
UPDATE users SET otp_active = ? WHERE id = ?
`

type SetOTPActiveParams struct {
	OtpActive bool
	ID        string
}

func (q *Queries) SetOTPActive(ctx context.Context, arg SetOTPActiveParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, setOTPActive, arg.OtpActive, arg.ID)
}

const updateAdminStatus = `-- name: UpdateAdminStatus :execresult
UPDATE users SET admin = ? WHERE id = ?
`

type UpdateAdminStatusParams struct {
	Admin bool
	ID    string
}

func (q *Queries) UpdateAdminStatus(ctx context.Context, arg UpdateAdminStatusParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateAdminStatus, arg.Admin, arg.ID)
}

const updateEmail = `-- name: UpdateEmail :one
UPDATE users SET email = new_email, new_email = NULL, new_email_token = NULL, new_email_expires = NULL WHERE new_email_token = ? AND new_email_expires > ?2 RETURNING email
`

type UpdateEmailParams struct {
	NewEmailToken []byte
	Now           sql.NullInt64
}

func (q *Queries) UpdateEmail(ctx context.Context, arg UpdateEmailParams) (string, error) {
	row := q.db.QueryRowContext(ctx, updateEmail, arg.NewEmailToken, arg.Now)
	var email string
	err := row.Scan(&email)
	return email, err
}

const updateEmailConfirmed = `-- name: UpdateEmailConfirmed :execresult
UPDATE users SET email_confirmed = ? WHERE id = ?
`

type UpdateEmailConfirmedParams struct {
	EmailConfirmed bool
	ID             string
}

func (q *Queries) UpdateEmailConfirmed(ctx context.Context, arg UpdateEmailConfirmedParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateEmailConfirmed, arg.EmailConfirmed, arg.ID)
}

const updateOTP = `-- name: UpdateOTP :execresult
UPDATE users SET otp_active = ?, otp_url = ? WHERE id = ?
`

type UpdateOTPParams struct {
	OtpActive bool
	OtpUrl    string
	ID        string
}

func (q *Queries) UpdateOTP(ctx context.Context, arg UpdateOTPParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateOTP, arg.OtpActive, arg.OtpUrl, arg.ID)
}

const updatePassword = `-- name: UpdatePassword :execresult
UPDATE users SET password_hash = ? WHERE id = ?
`

type UpdatePasswordParams struct {
	PasswordHash []byte
	ID           string
}

func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updatePassword, arg.PasswordHash, arg.ID)
}

const updateUserName = `-- name: UpdateUserName :execresult
UPDATE users SET name = ? WHERE id = ?
`

type UpdateUserNameParams struct {
	Name string
	ID   string
}

func (q *Queries) UpdateUserName(ctx context.Context, arg UpdateUserNameParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateUserName, arg.Name, arg.ID)
}
