-- name: InsertGlobalMarketSettings :exec
INSERT INTO global_odd_market_settings (market_id, market_name, is_active)
VALUES ($1, $2, $3) ON CONFLICT (market_id) DO
UPDATE
SET is_active = EXCLUDED.is_active,
    updated_at = CURRENT_TIMESTAMP;
-- name: InsertCompanyMarketSettings :exec
INSERT INTO company_odd_market_settings (company_id, market_id, market_name, is_active)
VALUES ($1, $2, $3, $4) ON CONFLICT (company_id, market_id) DO
UPDATE
SET is_active = EXCLUDED.is_active,
    updated_at = CURRENT_TIMESTAMP;
-- name: GetAllGlobalMarketSettings :many
SELECT *
FROM global_odd_market_settings
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetGlobalMarketSettingsByID :one
SELECT *
FROM global_odd_market_settings
WHERE market_id = $1;
-- name: GetAllCompanyMarketSettings :many
SELECT *
FROM company_odd_market_settings
WHERE (
        company_id = sqlc.narg('company_id')
        OR sqlc.narg('company_id') IS NULL
    )
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetCompanyMarketSettingsByID :one
SELECT *
FROM company_odd_market_settings
WHERE market_id = $1;
-- name: GetAllOverrideMarketSettings :many
SELECT gdm.market_id,
    gdm.market_name,
    COALESCE(cdm.is_active, gdm.is_active) AS is_active,
    COALESCE(cdm.updated_at, gdm.updated_at) AS updated_at
FROM global_odd_market_settings gdm
    LEFT JOIN company_odd_market_settings cdm ON cdm.market_id = gdm.market_id
    AND company_id = $1
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetOverrideMarketSettingByID :one
SELECT gdm.market_id,
    gdm.market_name,
    COALESCE(cdm.is_active, gdm.is_active) AS is_active,
    COALESCE(cdm.updated_at, gdm.updated_at) AS updated_at
FROM global_odd_market_settings gdm
    LEFT JOIN company_odd_market_settings cdm ON cdm.market_id = gdm.market_id
    AND company_id = $1
WHERE gdm.market_id = $2;
-- name: DeleteAllMarketSettingsForCompany :exec
DELETE FROM company_odd_market_settings
WHERE company_id = $1;
-- name: DeleteCompanyMarketSettings :exec
DELETE FROM company_odd_market_settings
WHERE market_id = $1
    AND company_id = $2;