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

package db

import (
	"context"
	"time"

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

const getAssignedUsersByWalletId = `-- name: GetAssignedUsersByWalletId :many
SELECT "user".id, "user".username 
FROM "user"
INNER JOIN wallet_user 
    ON "user".id = wallet_user.user_id 
        AND wallet_user.wallet_id = $1
`

type GetAssignedUsersByWalletIdRow struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

func (q *Queries) GetAssignedUsersByWalletId(ctx context.Context, walletID int64) ([]GetAssignedUsersByWalletIdRow, error) {
	rows, err := q.db.Query(ctx, getAssignedUsersByWalletId, walletID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetAssignedUsersByWalletIdRow
	for rows.Next() {
		var i GetAssignedUsersByWalletIdRow
		if err := rows.Scan(&i.ID, &i.Username); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUser = `-- name: GetUser :one
SELECT id, username, password, created_at, wallet_id, can_create_warehouses FROM "user" WHERE username = $1
`

func (q *Queries) GetUser(ctx context.Context, username string) (User, error) {
	row := q.db.QueryRow(ctx, getUser, username)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Password,
		&i.CreatedAt,
		&i.WalletID,
		&i.CanCreateWarehouses,
	)
	return i, err
}

const getUserById = `-- name: GetUserById :one
SELECT id, username, password, created_at, wallet_id, can_create_warehouses FROM "user" WHERE id = $1
`

func (q *Queries) GetUserById(ctx context.Context, id int64) (User, error) {
	row := q.db.QueryRow(ctx, getUserById, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Password,
		&i.CreatedAt,
		&i.WalletID,
		&i.CanCreateWarehouses,
	)
	return i, err
}

const getUserCanCreateWarehouses = `-- name: GetUserCanCreateWarehouses :one
SELECT can_create_warehouses FROM "user" WHERE id = $1
`

func (q *Queries) GetUserCanCreateWarehouses(ctx context.Context, id int64) (bool, error) {
	row := q.db.QueryRow(ctx, getUserCanCreateWarehouses, id)
	var can_create_warehouses bool
	err := row.Scan(&can_create_warehouses)
	return can_create_warehouses, err
}

const getUserIdByUsername = `-- name: GetUserIdByUsername :one
SELECT id FROM "user" WHERE username = $1
`

func (q *Queries) GetUserIdByUsername(ctx context.Context, username string) (int64, error) {
	row := q.db.QueryRow(ctx, getUserIdByUsername, username)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const getUserSessionInfo = `-- name: GetUserSessionInfo :one
SELECT
    "user".username,
    "user".wallet_id AS primary_wallet_id,
    wallet.address AS wallet_address,
    wallet.user_id as wallet_user_id
FROM
    "user",
    wallet
WHERE
    "user".id = $1::bigint
    AND wallet.id = $2::bigint
`

type GetUserSessionInfoParams struct {
	UserID   int64 `json:"user_id"`
	WalletID int64 `json:"wallet_id"`
}

type GetUserSessionInfoRow struct {
	Username        string      `json:"username"`
	PrimaryWalletID pgtype.Int8 `json:"primary_wallet_id"`
	WalletAddress   string      `json:"wallet_address"`
	WalletUserID    int64       `json:"wallet_user_id"`
}

func (q *Queries) GetUserSessionInfo(ctx context.Context, arg GetUserSessionInfoParams) (GetUserSessionInfoRow, error) {
	row := q.db.QueryRow(ctx, getUserSessionInfo, arg.UserID, arg.WalletID)
	var i GetUserSessionInfoRow
	err := row.Scan(
		&i.Username,
		&i.PrimaryWalletID,
		&i.WalletAddress,
		&i.WalletUserID,
	)
	return i, err
}

const getUserWalletId = `-- name: GetUserWalletId :one
SELECT
    wallet_id
FROM
    "user"
WHERE
    id = $1::bigint
`

func (q *Queries) GetUserWalletId(ctx context.Context, userID int64) (pgtype.Int8, error) {
	row := q.db.QueryRow(ctx, getUserWalletId, userID)
	var wallet_id pgtype.Int8
	err := row.Scan(&wallet_id)
	return wallet_id, err
}

const getWalletByUsername = `-- name: GetWalletByUsername :one
SELECT wallet_id FROM "user" WHERE username = $1
`

func (q *Queries) GetWalletByUsername(ctx context.Context, username string) (pgtype.Int8, error) {
	row := q.db.QueryRow(ctx, getWalletByUsername, username)
	var wallet_id pgtype.Int8
	err := row.Scan(&wallet_id)
	return wallet_id, err
}

const insertUser = `-- name: InsertUser :one
INSERT INTO "user" (username, password, created_at) VALUES ($1, $2, $3) RETURNING id
`

type InsertUserParams struct {
	Username  string    `json:"username"`
	Password  string    `json:"password"`
	CreatedAt time.Time `json:"created_at"`
}

func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (int64, error) {
	row := q.db.QueryRow(ctx, insertUser, arg.Username, arg.Password, arg.CreatedAt)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const updateCanCreateWarehouses = `-- name: UpdateCanCreateWarehouses :exec
UPDATE "user" SET can_create_warehouses = $1 WHERE username = $2
`

type UpdateCanCreateWarehousesParams struct {
	CanCreateWarehouses bool   `json:"can_create_warehouses"`
	Username            string `json:"username"`
}

func (q *Queries) UpdateCanCreateWarehouses(ctx context.Context, arg UpdateCanCreateWarehousesParams) error {
	_, err := q.db.Exec(ctx, updateCanCreateWarehouses, arg.CanCreateWarehouses, arg.Username)
	return err
}

const updateUserPassword = `-- name: UpdateUserPassword :exec
UPDATE "user" SET password = $1 WHERE id = $2
`

type UpdateUserPasswordParams struct {
	Password string `json:"password"`
	ID       int64  `json:"id"`
}

func (q *Queries) UpdateUserPassword(ctx context.Context, arg UpdateUserPasswordParams) error {
	_, err := q.db.Exec(ctx, updateUserPassword, arg.Password, arg.ID)
	return err
}

const updateUserUsername = `-- name: UpdateUserUsername :exec
UPDATE "user" SET username = $1 WHERE id = $2
`

type UpdateUserUsernameParams struct {
	Username string `json:"username"`
	ID       int64  `json:"id"`
}

func (q *Queries) UpdateUserUsername(ctx context.Context, arg UpdateUserUsernameParams) error {
	_, err := q.db.Exec(ctx, updateUserUsername, arg.Username, arg.ID)
	return err
}

const updateUserWallet = `-- name: UpdateUserWallet :execrows
UPDATE "user" SET wallet_id = $1 WHERE id = $2
`

type UpdateUserWalletParams struct {
	WalletID pgtype.Int8 `json:"wallet_id"`
	ID       int64       `json:"id"`
}

func (q *Queries) UpdateUserWallet(ctx context.Context, arg UpdateUserWalletParams) (int64, error) {
	result, err := q.db.Exec(ctx, updateUserWallet, arg.WalletID, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
