-- name: InsertOddsMarket :exec
INSERT INTO odds_market (
        event_id,
        market_type,
        market_name,
        market_category,
        market_id,
        raw_odds,
        fetched_at,
        expires_at
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8
    ) ON CONFLICT (event_id, market_id) DO
UPDATE
SET market_type = EXCLUDED.market_type,
    market_name = EXCLUDED.market_name,
    market_category = EXCLUDED.market_category,
    raw_odds = EXCLUDED.raw_odds,
    fetched_at = EXCLUDED.fetched_at,
    expires_at = EXCLUDED.expires_at;
-- name: SaveOddSettings :exec
INSERT INTO company_odd_settings (
        company_id,
        odds_market_id,
        is_active,
        custom_raw_odds
    )
VALUES ($1, $2, $3, $4) ON CONFLICT (company_id, odds_market_id) DO
UPDATE
SET is_active = EXCLUDED.is_active,
    custom_raw_odds = EXCLUDED.custom_raw_odds;
-- name: GetAllOdds :many
SELECT *
FROM odds_market_with_event
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetAllOddsWithSettings :many
SELECT o.id,
    o.event_id,
    o.market_type,
    o.market_name,
    o.market_category,
    o.market_id,
    o.default_is_active,
    o.fetched_at,
    o.expires_at,
    cos.company_id,
    COALESCE(cos.is_active, o.default_is_active) AS is_active,
    COALESCE(cos.custom_raw_odds, o.raw_odds) AS raw_odds,
    cos.updated_at
FROM odds_market o
    LEFT JOIN company_odd_settings cos ON o.id = cos.odds_market_id
    AND company_id = $1
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetOddByID :one
SELECT *
FROM odds_market_with_event
WHERE id = $1;
-- name: GetOddsByMarketID :one
SELECT *
FROM odds_market_with_event
WHERE market_id = $1
    AND event_id = $2;
-- name: GetOddsWithSettingsByMarketID :one
SELECT o.id,
    o.event_id,
    o.market_type,
    o.market_name,
    o.market_category,
    o.market_id,
    o.default_is_active,
    o.fetched_at,
    o.expires_at,
    cos.company_id,
    COALESCE(cos.is_active, o.default_is_active) AS is_active,
    COALESCE(cos.custom_raw_odds, o.raw_odds) AS raw_odds,
    cos.updated_at
FROM odds_market o
    LEFT JOIN company_odd_settings cos ON o.id = cos.odds_market_id
    AND company_id = $3
WHERE market_id = $1
    AND event_id = $2;
-- name: GetOddsWithSettingsByID :one
SELECT o.id,
    o.event_id,
    o.market_type,
    o.market_name,
    o.market_category,
    o.market_id,
    o.default_is_active,
    o.fetched_at,
    o.expires_at,
    cos.company_id,
    COALESCE(cos.is_active, o.default_is_active) AS is_active,
    COALESCE(cos.custom_raw_odds, o.raw_odds) AS raw_odds,
    cos.updated_at
FROM odds_market o
    LEFT JOIN company_odd_settings cos ON o.id = cos.odds_market_id
    AND company_id = $2
WHERE o.id = $1;
-- name: GetOddsByEventID :many
SELECT *
FROM odds_market_with_event
WHERE event_id = $1
    AND (
        is_live = sqlc.narg('is_live')
        OR sqlc.narg('is_live') IS NULL
    )
    AND (
        status = sqlc.narg('status')
        OR sqlc.narg('status') IS NULL
    )
    AND (
        source = sqlc.narg('source')
        OR sqlc.narg('source') IS NULL
    )
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetOddsWithSettingsByEventID :many
SELECT o.id,
    o.event_id,
    o.market_type,
    o.market_name,
    o.market_category,
    o.market_id,
    o.default_is_active,
    o.fetched_at,
    o.expires_at,
    cos.company_id,
    COALESCE(cos.is_active, o.default_is_active) AS is_active,
    COALESCE(cos.custom_raw_odds, o.raw_odds) AS raw_odds,
    cos.updated_at
FROM odds_market o
    LEFT JOIN company_odd_settings cos ON o.id = cos.odds_market_id
    AND company_id = $2
WHERE event_id = $1
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: DeleteOddsForEvent :exec
DELETE FROM odds_market
Where event_id = $1;