-- 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,
        league_cc,
        start_time,
        score,
        match_minute,
        timer_status,
        added_time,
        match_period,
        is_live,
        status,
        source
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13,
        $14,
        $15,
        $16,
        $17,
        $18,
        $19,
        $20,
        $21
    ) 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,
    status = EXCLUDED.status,
    source = EXCLUDED.source,
    fetched_at = now();
-- name: InsertUpcomingEvent :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,
        league_cc,
        start_time,
        is_live,
        status,
        source
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13,
        false,
        'upcoming',
        $14
    ) 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,
    is_live = false,
    status = 'upcoming',
    source = EXCLUDED.source,
    fetched_at = now();
-- name: ListLiveEvents :many
SELECT id
FROM events
WHERE is_live = true;
-- name: GetAllUpcomingEvents :many
SELECT 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,
    league_cc,
    start_time,
    is_live,
    status,
    source,
    fetched_at
FROM events
WHERE start_time > now()
    AND is_live = false
    AND status = 'upcoming'
ORDER BY start_time ASC;
-- name: GetExpiredUpcomingEvents :many
SELECT events.id,
    events.sport_id,
    events.match_name,
    events.home_team,
    events.away_team,
    events.home_team_id,
    events.away_team_id,
    events.home_kit_image,
    events.away_kit_image,
    events.league_id,
    events.league_name,
    events.start_time,
    events.is_live,
    events.status,
    events.source,
    events.fetched_at,
    leagues.country_code as league_cc
FROM events
    LEFT JOIN leagues ON leagues.id = league_id
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 events
    LEFT JOIN leagues ON leagues.id = league_id
WHERE is_live = false
    AND status = 'upcoming'
    AND (
        league_id = sqlc.narg('league_id')
        OR sqlc.narg('league_id') IS NULL
    )
    AND (
        events.sport_id = sqlc.narg('sport_id')
        OR sqlc.narg('sport_id') 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 (
        leagues.country_code = sqlc.narg('country_code')
        OR sqlc.narg('country_code') IS NULL
    );
-- name: GetPaginatedUpcomingEvents :many
SELECT events.id,
    events.sport_id,
    events.match_name,
    events.home_team,
    events.away_team,
    events.home_team_id,
    events.away_team_id,
    events.home_kit_image,
    events.away_kit_image,
    events.league_id,
    events.league_name,
    events.start_time,
    events.is_live,
    events.status,
    events.source,
    events.fetched_at,
    leagues.country_code as league_cc
FROM events
    LEFT JOIN leagues ON leagues.id = league_id
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 (
        events.sport_id = sqlc.narg('sport_id')
        OR sqlc.narg('sport_id') 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 (
        leagues.country_code = 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 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,
    league_cc,
    start_time,
    is_live,
    status,
    source,
    fetched_at
FROM events
WHERE id = $1
    AND is_live = false
    AND status = 'upcoming'
LIMIT 1;
-- name: UpdateMatchResult :exec
UPDATE events
SET score = $1,
    status = $2,
    fetched_at = NOW()
WHERE id = $3;
-- name: DeleteEvent :exec
DELETE FROM events
WHERE id = $1;