// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.22.0
// source: account.sql

package db

import (
	"context"
	"database/sql"
)

const createAccount = `-- name: CreateAccount :one
INSERT INTO accounts (
    "email",
    "passwordhash",
    "secret_key"
)
VALUES (    
    $1,
    $2,
    $3
)
RETURNING id, permission_level, passwordhash, email, secret_key, email_verified, email_verified_time
`

type CreateAccountParams struct {
	Email        string         `json:"email"`
	Passwordhash string         `json:"passwordhash"`
	SecretKey    sql.NullString `json:"secret_key"`
}

func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) {
	row := q.db.QueryRowContext(ctx, createAccount, arg.Email, arg.Passwordhash, arg.SecretKey)
	var i Account
	err := row.Scan(
		&i.ID,
		&i.PermissionLevel,
		&i.Passwordhash,
		&i.Email,
		&i.SecretKey,
		&i.EmailVerified,
		&i.EmailVerifiedTime,
	)
	return i, err
}

const deleteAccount = `-- name: DeleteAccount :exec
DELETE FROM accounts
WHERE "id" = $1
`

func (q *Queries) DeleteAccount(ctx context.Context, id uint64) error {
	_, err := q.db.ExecContext(ctx, deleteAccount, id)
	return err
}

const getAccount = `-- name: GetAccount :one
SELECT id, permission_level, passwordhash, email, secret_key, email_verified, email_verified_time FROM accounts
WHERE "id" = $1
`

func (q *Queries) GetAccount(ctx context.Context, id uint64) (Account, error) {
	row := q.db.QueryRowContext(ctx, getAccount, id)
	var i Account
	err := row.Scan(
		&i.ID,
		&i.PermissionLevel,
		&i.Passwordhash,
		&i.Email,
		&i.SecretKey,
		&i.EmailVerified,
		&i.EmailVerifiedTime,
	)
	return i, err
}

const getAccountByEmail = `-- name: GetAccountByEmail :one
SELECT id, permission_level, passwordhash, email, secret_key, email_verified, email_verified_time FROM accounts
WHERE "email" = $1
`

func (q *Queries) GetAccountByEmail(ctx context.Context, email string) (Account, error) {
	row := q.db.QueryRowContext(ctx, getAccountByEmail, email)
	var i Account
	err := row.Scan(
		&i.ID,
		&i.PermissionLevel,
		&i.Passwordhash,
		&i.Email,
		&i.SecretKey,
		&i.EmailVerified,
		&i.EmailVerifiedTime,
	)
	return i, err
}

const listAccounts = `-- name: ListAccounts :many
SELECT id, permission_level, passwordhash, email, secret_key, email_verified, email_verified_time FROM accounts
ORDER BY "email"
LIMIT $1
OFFSET $2
`

type ListAccountsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListAccounts(ctx context.Context, arg ListAccountsParams) ([]Account, error) {
	rows, err := q.db.QueryContext(ctx, listAccounts, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Account{}
	for rows.Next() {
		var i Account
		if err := rows.Scan(
			&i.ID,
			&i.PermissionLevel,
			&i.Passwordhash,
			&i.Email,
			&i.SecretKey,
			&i.EmailVerified,
			&i.EmailVerifiedTime,
		); 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 updateAccount = `-- name: UpdateAccount :one
UPDATE accounts 
SET
    "email" = COALESCE($1, "email"),
    "passwordhash" = COALESCE($2, "passwordhash"),
    "secret_key" = COALESCE($3, "secret_key")
WHERE "id" = $4
RETURNING id, permission_level, passwordhash, email, secret_key, email_verified, email_verified_time
`

type UpdateAccountParams struct {
	Email        sql.NullString `json:"email"`
	Passwordhash sql.NullString `json:"passwordhash"`
	SecretKey    sql.NullString `json:"secret_key"`
	ID           uint64         `json:"id"`
}

func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error) {
	row := q.db.QueryRowContext(ctx, updateAccount,
		arg.Email,
		arg.Passwordhash,
		arg.SecretKey,
		arg.ID,
	)
	var i Account
	err := row.Scan(
		&i.ID,
		&i.PermissionLevel,
		&i.Passwordhash,
		&i.Email,
		&i.SecretKey,
		&i.EmailVerified,
		&i.EmailVerifiedTime,
	)
	return i, err
}

const verifyAccountEmail = `-- name: VerifyAccountEmail :exec
UPDATE accounts
SET
    "email_verified" = $1,
    "email_verified_time" = $2,
    "secret_key" = ''
WHERE "id" = $3
`

type VerifyAccountEmailParams struct {
	EmailVerified     sql.NullBool `json:"email_verified"`
	EmailVerifiedTime sql.NullTime `json:"email_verified_time"`
	ID                uint64       `json:"id"`
}

func (q *Queries) VerifyAccountEmail(ctx context.Context, arg VerifyAccountEmailParams) error {
	_, err := q.db.ExecContext(ctx, verifyAccountEmail, arg.EmailVerified, arg.EmailVerifiedTime, arg.ID)
	return err
}
