-- name: InsertOddHistory :one
INSERT INTO odd_history (
        odd_id,
        market_id,
        raw_odd_id,
        event_id,
        odd_value
    )
VALUES ($1, $2, $3, $4, $5)
RETURNING *;
-- name: GetAllOddHistory :many
SELECT *
FROM odd_history
WHERE (
        odd_id = sqlc.narg('odd_id')
        OR sqlc.narg('odd_id') IS NULL
    )
    AND (
        market_id = sqlc.narg('market_id')
        OR sqlc.narg('market_id') IS NULL
    )
    AND (
        raw_odd_id = sqlc.narg('raw_odd_id')
        OR sqlc.narg('raw_odd_id') IS NULL
    )
    AND (
        event_id = sqlc.narg('event_id')
        OR sqlc.narg('event_id') 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: GetInitialOddPerDay :many
SELECT DISTINCT ON (DATE_TRUNC('day', created_at)) *
FROM odd_history
WHERE (
        odd_id = sqlc.narg('odd_id')
        OR sqlc.narg('odd_id') IS NULL
    )
    AND (
        market_id = sqlc.narg('market_id')
        OR sqlc.narg('market_id') IS NULL
    )
    AND (
        raw_odd_id = sqlc.narg('raw_odd_id')
        OR sqlc.narg('raw_odd_id') IS NULL
    )
    AND (
        event_id = sqlc.narg('event_id')
        OR sqlc.narg('event_id') 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
    )
ORDER BY DATE_TRUNC('day', created_at),
    created_at ASC;