// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: wallet.sql

package dbgen

import (
	"context"

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

const CreateCustomerWallet = `-- name: CreateCustomerWallet :one
INSERT INTO customer_wallets (
        customer_id,
        regular_wallet_id,
        static_wallet_id
    )
VALUES ($1, $2, $3)
RETURNING id, customer_id, regular_wallet_id, static_wallet_id, created_at, updated_at
`

type CreateCustomerWalletParams struct {
	CustomerID      int64 `json:"customer_id"`
	RegularWalletID int64 `json:"regular_wallet_id"`
	StaticWalletID  int64 `json:"static_wallet_id"`
}

func (q *Queries) CreateCustomerWallet(ctx context.Context, arg CreateCustomerWalletParams) (CustomerWallet, error) {
	row := q.db.QueryRow(ctx, CreateCustomerWallet, arg.CustomerID, arg.RegularWalletID, arg.StaticWalletID)
	var i CustomerWallet
	err := row.Scan(
		&i.ID,
		&i.CustomerID,
		&i.RegularWalletID,
		&i.StaticWalletID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const CreateWallet = `-- name: CreateWallet :one
INSERT INTO wallets (
        is_withdraw,
        is_bettable,
        is_transferable,
        user_id
    )
VALUES ($1, $2, $3, $4)
RETURNING id, balance, is_withdraw, is_bettable, is_transferable, user_id, is_active, created_at, updated_at, currency, bonus_balance, cash_balance
`

type CreateWalletParams struct {
	IsWithdraw     bool  `json:"is_withdraw"`
	IsBettable     bool  `json:"is_bettable"`
	IsTransferable bool  `json:"is_transferable"`
	UserID         int64 `json:"user_id"`
}

func (q *Queries) CreateWallet(ctx context.Context, arg CreateWalletParams) (Wallet, error) {
	row := q.db.QueryRow(ctx, CreateWallet,
		arg.IsWithdraw,
		arg.IsBettable,
		arg.IsTransferable,
		arg.UserID,
	)
	var i Wallet
	err := row.Scan(
		&i.ID,
		&i.Balance,
		&i.IsWithdraw,
		&i.IsBettable,
		&i.IsTransferable,
		&i.UserID,
		&i.IsActive,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Currency,
		&i.BonusBalance,
		&i.CashBalance,
	)
	return i, err
}

const GetAllBranchWallets = `-- name: GetAllBranchWallets :many
SELECT wallets.id,
    wallets.balance,
    wallets.is_active,
    wallets.updated_at,
    wallets.created_at,
    branches.name,
    branches.location,
    branches.branch_manager_id,
    branches.company_id,
    branches.is_self_owned
FROM branches
    JOIN wallets ON branches.wallet_id = wallets.id
`

type GetAllBranchWalletsRow struct {
	ID              int64            `json:"id"`
	Balance         int64            `json:"balance"`
	IsActive        bool             `json:"is_active"`
	UpdatedAt       pgtype.Timestamp `json:"updated_at"`
	CreatedAt       pgtype.Timestamp `json:"created_at"`
	Name            string           `json:"name"`
	Location        string           `json:"location"`
	BranchManagerID int64            `json:"branch_manager_id"`
	CompanyID       int64            `json:"company_id"`
	IsSelfOwned     bool             `json:"is_self_owned"`
}

func (q *Queries) GetAllBranchWallets(ctx context.Context) ([]GetAllBranchWalletsRow, error) {
	rows, err := q.db.Query(ctx, GetAllBranchWallets)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetAllBranchWalletsRow
	for rows.Next() {
		var i GetAllBranchWalletsRow
		if err := rows.Scan(
			&i.ID,
			&i.Balance,
			&i.IsActive,
			&i.UpdatedAt,
			&i.CreatedAt,
			&i.Name,
			&i.Location,
			&i.BranchManagerID,
			&i.CompanyID,
			&i.IsSelfOwned,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetAllWallets = `-- name: GetAllWallets :many
SELECT id, balance, is_withdraw, is_bettable, is_transferable, user_id, is_active, created_at, updated_at, currency, bonus_balance, cash_balance
FROM wallets
`

func (q *Queries) GetAllWallets(ctx context.Context) ([]Wallet, error) {
	rows, err := q.db.Query(ctx, GetAllWallets)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Wallet
	for rows.Next() {
		var i Wallet
		if err := rows.Scan(
			&i.ID,
			&i.Balance,
			&i.IsWithdraw,
			&i.IsBettable,
			&i.IsTransferable,
			&i.UserID,
			&i.IsActive,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Currency,
			&i.BonusBalance,
			&i.CashBalance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetCustomerWallet = `-- name: GetCustomerWallet :one
SELECT cw.id,
    cw.customer_id,
    rw.id AS regular_id,
    rw.balance AS regular_balance,
    sw.id AS static_id,
    sw.balance AS static_balance,
    rw.updated_at as regular_updated_at,
    sw.updated_at as static_updated_at,
    cw.created_at
FROM customer_wallets cw
    JOIN wallets rw ON cw.regular_wallet_id = rw.id
    JOIN wallets sw ON cw.static_wallet_id = sw.id
WHERE cw.customer_id = $1
`

type GetCustomerWalletRow struct {
	ID               int64            `json:"id"`
	CustomerID       int64            `json:"customer_id"`
	RegularID        int64            `json:"regular_id"`
	RegularBalance   int64            `json:"regular_balance"`
	StaticID         int64            `json:"static_id"`
	StaticBalance    int64            `json:"static_balance"`
	RegularUpdatedAt pgtype.Timestamp `json:"regular_updated_at"`
	StaticUpdatedAt  pgtype.Timestamp `json:"static_updated_at"`
	CreatedAt        pgtype.Timestamp `json:"created_at"`
}

func (q *Queries) GetCustomerWallet(ctx context.Context, customerID int64) (GetCustomerWalletRow, error) {
	row := q.db.QueryRow(ctx, GetCustomerWallet, customerID)
	var i GetCustomerWalletRow
	err := row.Scan(
		&i.ID,
		&i.CustomerID,
		&i.RegularID,
		&i.RegularBalance,
		&i.StaticID,
		&i.StaticBalance,
		&i.RegularUpdatedAt,
		&i.StaticUpdatedAt,
		&i.CreatedAt,
	)
	return i, err
}

const GetWalletByID = `-- name: GetWalletByID :one
SELECT id, balance, is_withdraw, is_bettable, is_transferable, user_id, is_active, created_at, updated_at, currency, bonus_balance, cash_balance
FROM wallets
WHERE id = $1
`

func (q *Queries) GetWalletByID(ctx context.Context, id int64) (Wallet, error) {
	row := q.db.QueryRow(ctx, GetWalletByID, id)
	var i Wallet
	err := row.Scan(
		&i.ID,
		&i.Balance,
		&i.IsWithdraw,
		&i.IsBettable,
		&i.IsTransferable,
		&i.UserID,
		&i.IsActive,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Currency,
		&i.BonusBalance,
		&i.CashBalance,
	)
	return i, err
}

const GetWalletByUserID = `-- name: GetWalletByUserID :many
SELECT id, balance, is_withdraw, is_bettable, is_transferable, user_id, is_active, created_at, updated_at, currency, bonus_balance, cash_balance
FROM wallets
WHERE user_id = $1
`

func (q *Queries) GetWalletByUserID(ctx context.Context, userID int64) ([]Wallet, error) {
	rows, err := q.db.Query(ctx, GetWalletByUserID, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Wallet
	for rows.Next() {
		var i Wallet
		if err := rows.Scan(
			&i.ID,
			&i.Balance,
			&i.IsWithdraw,
			&i.IsBettable,
			&i.IsTransferable,
			&i.UserID,
			&i.IsActive,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Currency,
			&i.BonusBalance,
			&i.CashBalance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateBalance = `-- name: UpdateBalance :exec
UPDATE wallets
SET balance = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2
`

type UpdateBalanceParams struct {
	Balance int64 `json:"balance"`
	ID      int64 `json:"id"`
}

func (q *Queries) UpdateBalance(ctx context.Context, arg UpdateBalanceParams) error {
	_, err := q.db.Exec(ctx, UpdateBalance, arg.Balance, arg.ID)
	return err
}

const UpdateWalletActive = `-- name: UpdateWalletActive :exec
UPDATE wallets
SET is_active = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2
`

type UpdateWalletActiveParams struct {
	IsActive bool  `json:"is_active"`
	ID       int64 `json:"id"`
}

func (q *Queries) UpdateWalletActive(ctx context.Context, arg UpdateWalletActiveParams) error {
	_, err := q.db.Exec(ctx, UpdateWalletActive, arg.IsActive, arg.ID)
	return err
}
