-- name: CreateBet :one
INSERT INTO bets (
        amount,
        total_odds,
        status,
        user_id,
        is_shop_bet,
        outcomes_hash,
        fast_code
    )
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: CreateBetOutcome :copyfrom
INSERT INTO bet_outcomes (
        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,
        expires
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13
    );
-- name: GetAllBets :many
SELECT *
FROM bet_with_outcomes
wHERE (
        user_id = sqlc.narg('user_id')
        OR sqlc.narg('user_id') IS NULL
    )
    AND (
        is_shop_bet = sqlc.narg('is_shop_bet')
        OR sqlc.narg('is_shop_bet') IS NULL
    )
    AND (
        cashed_out = sqlc.narg('cashed_out')
        OR sqlc.narg('cashed_out') IS NULL
    )
    AND (
        full_name ILIKE '%' || sqlc.narg('query') || '%'
        OR phone_number ILIKE '%' || sqlc.narg('query') || '%'
        OR sqlc.narg('query') IS NULL
    )
    AND (
        created_at > sqlc.narg('created_before')
        OR sqlc.narg('created_before') IS NULL
    )
    AND (
        created_at < sqlc.narg('created_after')
        OR sqlc.narg('created_after') IS NULL
    );
-- name: GetBetByID :one
SELECT *
FROM bet_with_outcomes
WHERE id = $1;
-- name: GetBetByUserID :many
SELECT *
FROM bet_with_outcomes
WHERE user_id = $1;
-- name: GetBetByFastCode :one
SELECT *
FROM bet_with_outcomes
WHERE fast_code = $1
LIMIT 1;
-- name: GetBetsForCashback :many
SELECT *
FROM bet_with_outcomes
WHERE status = 2
    AND processed = false;
-- name: GetBetOutcomeByEventID :many
SELECT *
FROM bet_outcomes
WHERE (event_id = $1)
    AND (
        status = sqlc.narg('filter_status')
        OR sqlc.narg('filter_status') IS NULL
        OR status = sqlc.narg('filter_status_2')
        OR sqlc.narg('filter_status_2') IS NULL
    );
-- name: GetBetOutcomeByBetID :many
SELECT *
FROM bet_outcomes
WHERE bet_id = $1;
-- name: GetBetCountByUserID :one
SELECT COUNT(*)
FROM bets
WHERE user_id = $1
    AND outcomes_hash = $2;
-- name: GetBetCountByOutcomesHash :one
SELECT COUNT(*)
FROM bets
WHERE outcomes_hash = $1;
-- name: UpdateCashOut :exec
UPDATE bets
SET cashed_out = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1;
-- name: UpdateBetOutcomeStatus :one
UPDATE bet_outcomes
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateBetOutcomeStatusByBetID :one
UPDATE bet_outcomes
SET status = $1
WHERE bet_id = $2
RETURNING *;
-- name: UpdateBetOutcomeStatusForEvent :many
UPDATE bet_outcomes
SEt status = $1
WHERE event_id = $2
RETURNING *;
-- name: UpdateStatus :exec
UPDATE bets
SET status = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2;
-- name: UpdateBetWithCashback :exec
UPDATE bets
SET processed = $1
WHERE id = $2;
-- name: DeleteBet :exec
DELETE FROM bets
WHERE id = $1;
-- name: DeleteBetOutcome :exec
DELETE FROM bet_outcomes
WHERE bet_id = $1;
