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

package dbgen

import (
	"context"

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

const CreateBet = `-- name: CreateBet :one
INSERT INTO bets (
        amount,
        total_odds,
        status,
        full_name,
        phone_number,
        branch_id,
        user_id,
        is_shop_bet,
        cashout_id,
        company_id,
        outcomes_hash
    )
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
RETURNING id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash
`

type CreateBetParams struct {
	Amount       int64       `json:"amount"`
	TotalOdds    float32     `json:"total_odds"`
	Status       int32       `json:"status"`
	FullName     string      `json:"full_name"`
	PhoneNumber  string      `json:"phone_number"`
	BranchID     pgtype.Int8 `json:"branch_id"`
	UserID       pgtype.Int8 `json:"user_id"`
	IsShopBet    bool        `json:"is_shop_bet"`
	CashoutID    string      `json:"cashout_id"`
	CompanyID    pgtype.Int8 `json:"company_id"`
	OutcomesHash string      `json:"outcomes_hash"`
}

func (q *Queries) CreateBet(ctx context.Context, arg CreateBetParams) (Bet, error) {
	row := q.db.QueryRow(ctx, CreateBet,
		arg.Amount,
		arg.TotalOdds,
		arg.Status,
		arg.FullName,
		arg.PhoneNumber,
		arg.BranchID,
		arg.UserID,
		arg.IsShopBet,
		arg.CashoutID,
		arg.CompanyID,
		arg.OutcomesHash,
	)
	var i Bet
	err := row.Scan(
		&i.ID,
		&i.Amount,
		&i.TotalOdds,
		&i.Status,
		&i.FullName,
		&i.PhoneNumber,
		&i.CompanyID,
		&i.BranchID,
		&i.UserID,
		&i.CashedOut,
		&i.CashoutID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsShopBet,
		&i.OutcomesHash,
	)
	return i, err
}

type CreateBetOutcomeParams struct {
	BetID        int64            `json:"bet_id"`
	SportID      int64            `json:"sport_id"`
	EventID      int64            `json:"event_id"`
	OddID        int64            `json:"odd_id"`
	HomeTeamName string           `json:"home_team_name"`
	AwayTeamName string           `json:"away_team_name"`
	MarketID     int64            `json:"market_id"`
	MarketName   string           `json:"market_name"`
	Odd          float32          `json:"odd"`
	OddName      string           `json:"odd_name"`
	OddHeader    string           `json:"odd_header"`
	OddHandicap  string           `json:"odd_handicap"`
	Expires      pgtype.Timestamp `json:"expires"`
}

const DeleteBet = `-- name: DeleteBet :exec
DELETE FROM bets
WHERE id = $1
`

func (q *Queries) DeleteBet(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, DeleteBet, id)
	return err
}

const DeleteBetOutcome = `-- name: DeleteBetOutcome :exec
DELETE FROM bet_outcomes
WHERE bet_id = $1
`

func (q *Queries) DeleteBetOutcome(ctx context.Context, betID int64) error {
	_, err := q.db.Exec(ctx, DeleteBetOutcome, betID)
	return err
}

const GetAllBets = `-- name: GetAllBets :many
SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, outcomes
FROM bet_with_outcomes
wHERE (
        branch_id = $1
        OR $1 IS NULL
    )
    AND (
        company_id = $2
        OR $2 IS NULL
    )
    AND (
        user_id = $3
        OR $3 IS NULL
    )
`

type GetAllBetsParams struct {
	BranchID  pgtype.Int8 `json:"branch_id"`
	CompanyID pgtype.Int8 `json:"company_id"`
	UserID    pgtype.Int8 `json:"user_id"`
}

func (q *Queries) GetAllBets(ctx context.Context, arg GetAllBetsParams) ([]BetWithOutcome, error) {
	rows, err := q.db.Query(ctx, GetAllBets, arg.BranchID, arg.CompanyID, arg.UserID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BetWithOutcome
	for rows.Next() {
		var i BetWithOutcome
		if err := rows.Scan(
			&i.ID,
			&i.Amount,
			&i.TotalOdds,
			&i.Status,
			&i.FullName,
			&i.PhoneNumber,
			&i.CompanyID,
			&i.BranchID,
			&i.UserID,
			&i.CashedOut,
			&i.CashoutID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.IsShopBet,
			&i.OutcomesHash,
			&i.Outcomes,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBetByBranchID = `-- name: GetBetByBranchID :many
SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, outcomes
FROM bet_with_outcomes
WHERE branch_id = $1
`

func (q *Queries) GetBetByBranchID(ctx context.Context, branchID pgtype.Int8) ([]BetWithOutcome, error) {
	rows, err := q.db.Query(ctx, GetBetByBranchID, branchID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BetWithOutcome
	for rows.Next() {
		var i BetWithOutcome
		if err := rows.Scan(
			&i.ID,
			&i.Amount,
			&i.TotalOdds,
			&i.Status,
			&i.FullName,
			&i.PhoneNumber,
			&i.CompanyID,
			&i.BranchID,
			&i.UserID,
			&i.CashedOut,
			&i.CashoutID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.IsShopBet,
			&i.OutcomesHash,
			&i.Outcomes,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBetByCashoutID = `-- name: GetBetByCashoutID :one
SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, outcomes
FROM bet_with_outcomes
WHERE cashout_id = $1
`

func (q *Queries) GetBetByCashoutID(ctx context.Context, cashoutID string) (BetWithOutcome, error) {
	row := q.db.QueryRow(ctx, GetBetByCashoutID, cashoutID)
	var i BetWithOutcome
	err := row.Scan(
		&i.ID,
		&i.Amount,
		&i.TotalOdds,
		&i.Status,
		&i.FullName,
		&i.PhoneNumber,
		&i.CompanyID,
		&i.BranchID,
		&i.UserID,
		&i.CashedOut,
		&i.CashoutID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsShopBet,
		&i.OutcomesHash,
		&i.Outcomes,
	)
	return i, err
}

const GetBetByID = `-- name: GetBetByID :one
SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, outcomes
FROM bet_with_outcomes
WHERE id = $1
`

func (q *Queries) GetBetByID(ctx context.Context, id int64) (BetWithOutcome, error) {
	row := q.db.QueryRow(ctx, GetBetByID, id)
	var i BetWithOutcome
	err := row.Scan(
		&i.ID,
		&i.Amount,
		&i.TotalOdds,
		&i.Status,
		&i.FullName,
		&i.PhoneNumber,
		&i.CompanyID,
		&i.BranchID,
		&i.UserID,
		&i.CashedOut,
		&i.CashoutID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsShopBet,
		&i.OutcomesHash,
		&i.Outcomes,
	)
	return i, err
}

const GetBetByUserID = `-- name: GetBetByUserID :many
SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, outcomes
FROM bet_with_outcomes
WHERE user_id = $1
`

func (q *Queries) GetBetByUserID(ctx context.Context, userID pgtype.Int8) ([]BetWithOutcome, error) {
	rows, err := q.db.Query(ctx, GetBetByUserID, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BetWithOutcome
	for rows.Next() {
		var i BetWithOutcome
		if err := rows.Scan(
			&i.ID,
			&i.Amount,
			&i.TotalOdds,
			&i.Status,
			&i.FullName,
			&i.PhoneNumber,
			&i.CompanyID,
			&i.BranchID,
			&i.UserID,
			&i.CashedOut,
			&i.CashoutID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.IsShopBet,
			&i.OutcomesHash,
			&i.Outcomes,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBetCount = `-- name: GetBetCount :one
SELECT COUNT(*)
FROM bets
where user_id = $1
    AND outcomes_hash = $2
`

type GetBetCountParams struct {
	UserID       pgtype.Int8 `json:"user_id"`
	OutcomesHash string      `json:"outcomes_hash"`
}

func (q *Queries) GetBetCount(ctx context.Context, arg GetBetCountParams) (int64, error) {
	row := q.db.QueryRow(ctx, GetBetCount, arg.UserID, arg.OutcomesHash)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const GetBetOutcomeByBetID = `-- name: GetBetOutcomeByBetID :many
SELECT id, bet_id, sport_id, event_id, odd_id, home_team_name, away_team_name, market_id, market_name, odd, odd_name, odd_header, odd_handicap, status, expires
FROM bet_outcomes
WHERE bet_id = $1
`

func (q *Queries) GetBetOutcomeByBetID(ctx context.Context, betID int64) ([]BetOutcome, error) {
	rows, err := q.db.Query(ctx, GetBetOutcomeByBetID, betID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BetOutcome
	for rows.Next() {
		var i BetOutcome
		if err := rows.Scan(
			&i.ID,
			&i.BetID,
			&i.SportID,
			&i.EventID,
			&i.OddID,
			&i.HomeTeamName,
			&i.AwayTeamName,
			&i.MarketID,
			&i.MarketName,
			&i.Odd,
			&i.OddName,
			&i.OddHeader,
			&i.OddHandicap,
			&i.Status,
			&i.Expires,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBetOutcomeByEventID = `-- name: GetBetOutcomeByEventID :many
SELECT id, bet_id, sport_id, event_id, odd_id, home_team_name, away_team_name, market_id, market_name, odd, odd_name, odd_header, odd_handicap, status, expires
FROM bet_outcomes
WHERE event_id = $1
`

func (q *Queries) GetBetOutcomeByEventID(ctx context.Context, eventID int64) ([]BetOutcome, error) {
	rows, err := q.db.Query(ctx, GetBetOutcomeByEventID, eventID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BetOutcome
	for rows.Next() {
		var i BetOutcome
		if err := rows.Scan(
			&i.ID,
			&i.BetID,
			&i.SportID,
			&i.EventID,
			&i.OddID,
			&i.HomeTeamName,
			&i.AwayTeamName,
			&i.MarketID,
			&i.MarketName,
			&i.Odd,
			&i.OddName,
			&i.OddHeader,
			&i.OddHandicap,
			&i.Status,
			&i.Expires,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateBetOutcomeStatus = `-- name: UpdateBetOutcomeStatus :one
UPDATE bet_outcomes
SET status = $1
WHERE id = $2
RETURNING id, bet_id, sport_id, event_id, odd_id, home_team_name, away_team_name, market_id, market_name, odd, odd_name, odd_header, odd_handicap, status, expires
`

type UpdateBetOutcomeStatusParams struct {
	Status int32 `json:"status"`
	ID     int64 `json:"id"`
}

func (q *Queries) UpdateBetOutcomeStatus(ctx context.Context, arg UpdateBetOutcomeStatusParams) (BetOutcome, error) {
	row := q.db.QueryRow(ctx, UpdateBetOutcomeStatus, arg.Status, arg.ID)
	var i BetOutcome
	err := row.Scan(
		&i.ID,
		&i.BetID,
		&i.SportID,
		&i.EventID,
		&i.OddID,
		&i.HomeTeamName,
		&i.AwayTeamName,
		&i.MarketID,
		&i.MarketName,
		&i.Odd,
		&i.OddName,
		&i.OddHeader,
		&i.OddHandicap,
		&i.Status,
		&i.Expires,
	)
	return i, err
}

const UpdateCashOut = `-- name: UpdateCashOut :exec
UPDATE bets
SET cashed_out = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1
`

type UpdateCashOutParams struct {
	ID        int64 `json:"id"`
	CashedOut bool  `json:"cashed_out"`
}

func (q *Queries) UpdateCashOut(ctx context.Context, arg UpdateCashOutParams) error {
	_, err := q.db.Exec(ctx, UpdateCashOut, arg.ID, arg.CashedOut)
	return err
}

const UpdateStatus = `-- name: UpdateStatus :exec
UPDATE bets
SET status = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2
`

type UpdateStatusParams struct {
	Status int32 `json:"status"`
	ID     int64 `json:"id"`
}

func (q *Queries) UpdateStatus(ctx context.Context, arg UpdateStatusParams) error {
	_, err := q.db.Exec(ctx, UpdateStatus, arg.Status, arg.ID)
	return err
}
