-- name: SaveTenantEventSettings :exec
INSERT INTO company_event_settings (
        company_id,
        event_id,
        is_active,
        is_featured,
        winning_upper_limit
    )
VALUES ($1, $2, $3, $4, $5) ON CONFLICT(company_id, event_id) DO
UPDATE
SET is_active = EXCLUDED.is_active,
    is_featured = EXCLUDED.is_featured,
    winning_upper_limit = EXCLUDED.winning_upper_limit;
-- name: GetTotalCompanyEvents :one
SELECT COUNT(*)
FROM events e
    LEFT JOIN company_event_settings ces ON e.id = ces.event_id
    AND ces.company_id = $1
    JOIN leagues l ON l.id = e.league_id
WHERE (
        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(
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        e.sport_id = sqlc.narg('sport_id')
        OR sqlc.narg('sport_id') IS NULL
    )
    AND (
        match_name ILIKE '%' || sqlc.narg('query') || '%'
        OR league_name ILIKE '%' || sqlc.narg('query') || '%'
        OR sqlc.narg('query') IS NULL
    )
    AND (
        start_time < sqlc.narg('last_start_time')
        OR sqlc.narg('last_start_time') IS NULL
    )
    AND (
        start_time > sqlc.narg('first_start_time')
        OR sqlc.narg('first_start_time') IS NULL
    )
    AND (
        l.country_code = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    )
    AND (
        ces.is_featured = sqlc.narg('is_featured')
        OR e.default_is_featured = sqlc.narg('is_featured')
        OR sqlc.narg('is_featured') IS NULL
    )
    AND (
        ces.is_active = sqlc.narg('is_active')
        OR e.default_is_active = sqlc.narg('is_active')
        OR sqlc.narg('is_active') IS NULL
    )
    AND (
        source = sqlc.narg('source')
        OR sqlc.narg('source') IS NULL
    );
-- name: GetEventsWithSettings :many
SELECT e.*,
    ces.company_id,
    COALESCE(ces.is_active, e.default_is_active) AS is_active,
    COALESCE(ces.is_featured, e.default_is_featured) AS is_featured,
    COALESCE(
        ces.winning_upper_limit,
        e.default_winning_upper_limit
    ) AS winning_upper_limit,
    ces.updated_at,
    l.country_code as league_cc,
    COALESCE(om.total_outcomes, 0) AS total_outcomes,
    COALESCE(ebs.number_of_bets, 0) AS number_of_bets,
    COALESCE(ebs.total_amount, 0) AS total_amount,
    COALESCE(ebs.avg_bet_amount, 0) AS avg_bet_amount,
    COALESCE(ebs.total_potential_winnings, 0) AS total_potential_winnings
FROM events e
    LEFT JOIN company_event_settings ces ON e.id = ces.event_id
    AND ces.company_id = $1
    LEFT JOIN event_bet_stats ebs ON ebs.event_id = e.id
    JOIN leagues l ON l.id = e.league_id
    LEFT JOIN (
        SELECT event_id,
            SUM(number_of_outcomes) AS total_outcomes
        FROM odds_market
        GROUP BY event_id
    ) om ON om.event_id = e.id
WHERE (
        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(
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        e.sport_id = sqlc.narg('sport_id')
        OR sqlc.narg('sport_id') IS NULL
    )
    AND (
        match_name ILIKE '%' || sqlc.narg('query') || '%'
        OR league_name ILIKE '%' || sqlc.narg('query') || '%'
        OR sqlc.narg('query') IS NULL
    )
    AND (
        start_time < sqlc.narg('last_start_time')
        OR sqlc.narg('last_start_time') IS NULL
    )
    AND (
        start_time > sqlc.narg('first_start_time')
        OR sqlc.narg('first_start_time') IS NULL
    )
    AND (
        l.country_code = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    )
    AND (
        ces.is_featured = sqlc.narg('is_featured')
        OR e.default_is_featured = sqlc.narg('is_featured')
        OR sqlc.narg('is_featured') IS NULL
    )
    AND (
        ces.is_active = sqlc.narg('is_active')
        OR e.default_is_active = sqlc.narg('is_active')
        OR sqlc.narg('is_active') IS NULL
    )
    AND (
        source = sqlc.narg('source')
        OR sqlc.narg('source') IS NULL
    )
ORDER BY start_time ASC
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetEventWithSettingByID :one
SELECT e.*,
    ces.company_id,
    COALESCE(ces.is_active, e.default_is_active) AS is_active,
    COALESCE(ces.is_featured, e.default_is_featured) AS is_featured,
    COALESCE(
        ces.winning_upper_limit,
        e.default_winning_upper_limit
    ) AS winning_upper_limit,
    ces.updated_at,
    l.country_code as league_cc,
    COALESCE(om.total_outcomes, 0) AS total_outcomes,
    COALESCE(ebs.number_of_bets, 0) AS number_of_bets,
    COALESCE(ebs.total_amount, 0) AS total_amount,
    COALESCE(ebs.avg_bet_amount, 0) AS avg_bet_amount,
    COALESCE(ebs.total_potential_winnings, 0) AS total_potential_winnings
FROM events e
    LEFT JOIN company_event_settings ces ON e.id = ces.event_id
    AND ces.company_id = $2
    LEFT JOIN event_bet_stats ebs ON ebs.event_id = e.id
    JOIN leagues l ON l.id = e.league_id
    LEFT JOIN (
        SELECT event_id,
            SUM(number_of_outcomes) AS total_outcomes
        FROM odds_market
        GROUP BY event_id
    ) om ON om.event_id = e.id
WHERE e.id = $1
LIMIT 1;