-- name: InsertLeague :exec
INSERT INTO leagues (
        id,
        name,
        country_code,
        bet365_id,
        sport_id,
        is_active
    )
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO
UPDATE
SET name = EXCLUDED.name,
    country_code = EXCLUDED.country_code,
    bet365_id = EXCLUDED.bet365_id,
    is_active = EXCLUDED.is_active,
    sport_id = EXCLUDED.sport_id;
-- name: GetAllLeagues :many
SELECT id,
    name,
    country_code,
    bet365_id,
    is_active,
    sport_id
FROM leagues
WHERE (
        country_code = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    )
    AND (
        sport_id = sqlc.narg('sport_id')
        OR sqlc.narg('sport_id') IS NULL
    )
    AND (
        is_active = sqlc.narg('is_active')
        OR sqlc.narg('is_active') IS NULL
    )
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: CheckLeagueSupport :one
SELECT EXISTS(
        SELECT 1
        FROM leagues
        WHERE id = $1
            AND is_active = true
    );
-- name: UpdateLeague :exec
UPDATE leagues
SET name = COALESCE(sqlc.narg('name'), name),
    country_code = COALESCE(sqlc.narg('country_code'), country_code),
    bet365_id = COALESCE(sqlc.narg('bet365_id'), bet365_id),
    is_active = COALESCE(sqlc.narg('is_active'), is_active),
    sport_id = COALESCE(sqlc.narg('sport_id'), sport_id)
WHERE id = $1;
-- name: UpdateLeagueByBet365ID :exec
UPDATE leagues
SET name = COALESCE(sqlc.narg('name'), name),
    id = COALESCE(sqlc.narg('id'), id),
    country_code = COALESCE(sqlc.narg('country_code'), country_code),
    is_active = COALESCE(sqlc.narg('is_active'), is_active),
    sport_id = COALESCE(sqlc.narg('sport_id'), sport_id)
WHERE bet365_id = $1;
-- name: SetLeagueActive :exec
UPDATE leagues
SET is_active = $2
WHERE id = $1;