-- name: InsertEvent :exec
INSERT INTO events (
        id,
        sport_id,
        match_name,
        home_team,
        away_team,
        home_team_id,
        away_team_id,
        home_kit_image,
        away_kit_image,
        league_id,
        league_name,
        start_time,
        is_live,
        status,
        source
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13,
        $14,
        $15
    ) ON CONFLICT (id) DO
UPDATE
SET sport_id = EXCLUDED.sport_id,
    match_name = EXCLUDED.match_name,
    home_team = EXCLUDED.home_team,
    away_team = EXCLUDED.away_team,
    home_team_id = EXCLUDED.home_team_id,
    away_team_id = EXCLUDED.away_team_id,
    home_kit_image = EXCLUDED.home_kit_image,
    away_kit_image = EXCLUDED.away_kit_image,
    league_id = EXCLUDED.league_id,
    league_name = EXCLUDED.league_name,
    league_cc = EXCLUDED.league_cc,
    start_time = EXCLUDED.start_time,
    score = EXCLUDED.score,
    match_minute = EXCLUDED.match_minute,
    timer_status = EXCLUDED.timer_status,
    added_time = EXCLUDED.added_time,
    match_period = EXCLUDED.match_period,
    is_live = EXCLUDED.is_live,
    source = EXCLUDED.source,
    fetched_at = now();
-- name: SaveEventSettings :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: ListLiveEvents :many
SELECT id
FROM event_with_country
WHERE is_live = true;
-- name: GetAllUpcomingEvents :many
SELECT *
FROM event_with_country
WHERE start_time > now()
    AND is_live = false
    AND status = 'upcoming'
ORDER BY start_time ASC;
-- name: GetExpiredEvents :many
SELECT *
FROM event_with_country
WHERE start_time < now()
    and (
        status = sqlc.narg('status')
        OR sqlc.narg('status') IS NULL
    )
ORDER BY start_time ASC;
-- name: GetTotalEvents :one
SELECT COUNT(*)
FROM event_with_country
WHERE is_live = false
    AND status = 'upcoming'
    AND (
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        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 (
        league_cc = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    );
-- name: GetPaginatedUpcomingEvents :many
SELECT *
FROM event_with_country
WHERE start_time > now()
    AND is_live = false
    AND status = 'upcoming'
    AND (
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        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 (
        league_cc = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    )
ORDER BY start_time ASC
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetTotalCompanyEvents :one
SELECT COUNT(*)
FROM event_with_settings
WHERE company_id = $1
    AND is_live = false
    AND status = 'upcoming'
    AND (
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        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 (
        league_cc = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    );
-- name: GetEventsWithSettings :many
SELECT *
FROM event_with_settings
WHERE company_id = $1
    AND start_time > now()
    AND is_live = false
    AND status = 'upcoming'
    AND (
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        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 (
        league_cc = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    )
ORDER BY start_time ASC
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: GetUpcomingByID :one
SELECT *
FROM event_with_country
WHERE id = $1
    AND is_live = false
    AND status = 'upcoming'
LIMIT 1;
-- name: GetEventWithSettingByID :one
SELECT *
FROM event_with_settings
WHERE id = $1
    AND company_id = $2
    AND is_live = false
    AND status = 'upcoming'
LIMIT 1;
-- name: UpdateMatchResult :exec
UPDATE events
SET score = $1,
    status = $2
WHERE id = $3;
-- name: IsEventMonitored :one
SELECT is_monitored
FROM events
WHERE id = $1;
-- name: UpdateEventMonitored :exec
UPDATE events
SET is_monitored = $1
WHERE id = $2;

-- name: DeleteEvent :exec
DELETE FROM events
WHERE id = $1;