-- name: InsertNonLiveOdd :exec
INSERT INTO odds (
        event_id,
        fi,
        market_type,
        market_name,
        market_category,
        market_id,
        name,
        handicap,
        odds_value,
        section,
        category,
        raw_odds,
        is_active,
        source,
        fetched_at
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13,
        $14,
        $15
    ) ON CONFLICT (event_id, market_id) DO
UPDATE
SET odds_value = EXCLUDED.odds_value,
    raw_odds = EXCLUDED.raw_odds,
    market_type = EXCLUDED.market_type,
    market_name = EXCLUDED.market_name,
    market_category = EXCLUDED.market_category,
    name = EXCLUDED.name,
    handicap = EXCLUDED.handicap,
    fetched_at = EXCLUDED.fetched_at,
    is_active = EXCLUDED.is_active,
    source = EXCLUDED.source,
    fi = EXCLUDED.fi;
-- name: GetPrematchOdds :many
SELECT *
FROM odds
WHERE is_active = true
    AND source = 'bet365';
-- name: GetALLPrematchOdds :many
SELECT *
FROM odds
WHERE is_active = true
    AND source = 'bet365';
-- name: GetOddsByMarketID :one
SELECT *
FROM odds
WHERE market_id = $1
    AND fi = $2
    AND is_active = true
    AND source = 'bet365';
-- name: GetPrematchOddsByUpcomingID :many
SELECT o.*
FROM odds o
    JOIN events e ON o.fi = e.id
WHERE e.id = $1
    AND e.is_live = false
    AND e.status = 'upcoming'
    AND o.is_active = true
    AND o.source = 'bet365';
-- name: GetPaginatedPrematchOddsByUpcomingID :many
SELECT o.*
FROM odds o
    JOIN events e ON o.fi = e.id
WHERE e.id = $1
    AND e.is_live = false
    AND e.status = 'upcoming'
    AND o.is_active = true
    AND o.source = 'bet365'
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: DeleteOddsForEvent :exec
DELETE FROM odds
Where fi = $1;