-- name: GetTotalEventStats :one
SELECT COUNT(*) AS event_count,
    COUNT(*) FILTER (
        WHERE events.default_is_active = TRUE
    ) AS total_active_events,
    COUNT(*) FILTER (
        WHERE events.default_is_active = FALSE
    ) AS total_inactive_events,
    COUNT(*) FILTER (
        WHERE events.default_is_featured = TRUE
    ) AS total_featured_events,
    COUNT(DISTINCT league_id) as total_leagues,
    COUNT(*) FILTER (
        WHERE events.status = 'upcoming'
    ) AS pending,
    COUNT(*) FILTER (
        WHERE events.status = 'in_play'
    ) AS in_play,
    COUNT(*) FILTER (
        WHERE events.status = 'to_be_fixed'
    ) AS to_be_fixed,
    COUNT(*) FILTER (
        WHERE events.status = 'ended'
    ) AS ended,
    COUNT(*) FILTER (
        WHERE events.status = 'postponed'
    ) AS postponed,
    COUNT(*) FILTER (
        WHERE events.status = 'cancelled'
    ) AS cancelled,
    COUNT(*) FILTER (
        WHERE events.status = 'walkover'
    ) AS walkover,
    COUNT(*) FILTER (
        WHERE events.status = 'interrupted'
    ) AS interrupted,
    COUNT(*) FILTER (
        WHERE events.status = 'abandoned'
    ) AS abandoned,
    COUNT(*) FILTER (
        WHERE events.status = 'retired'
    ) AS retired,
    COUNT(*) FILTER (
        WHERE events.status = 'suspended'
    ) AS suspended,
    COUNT(*) FILTER (
        WHERE events.status = 'decided_by_fa'
    ) AS decided_by_fa,
    COUNT(*) FILTER (
        WHERE events.status = 'removed'
    ) AS removed
FROM events
WHERE (
        events.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
    );
-- name: GetTotalEventStatsByInterval :many
SELECT DATE_TRUNC(sqlc.narg('interval'), start_time)::timestamp AS date,
    COUNT(*) AS event_count,
    COUNT(*) FILTER (
        WHERE events.default_is_active = TRUE
    ) AS total_active_events,
    COUNT(*) FILTER (
        WHERE events.default_is_active = FALSE
    ) AS total_inactive_events,
    COUNT(*) FILTER (
        WHERE events.default_is_featured = TRUE
    ) AS total_featured_events,
    COUNT(DISTINCT league_id) as total_leagues,
    COUNT(*) FILTER (
        WHERE events.status = 'upcoming'
    ) AS pending,
    COUNT(*) FILTER (
        WHERE events.status = 'in_play'
    ) AS in_play,
    COUNT(*) FILTER (
        WHERE events.status = 'to_be_fixed'
    ) AS to_be_fixed,
    COUNT(*) FILTER (
        WHERE events.status = 'ended'
    ) AS ended,
    COUNT(*) FILTER (
        WHERE events.status = 'postponed'
    ) AS postponed,
    COUNT(*) FILTER (
        WHERE events.status = 'cancelled'
    ) AS cancelled,
    COUNT(*) FILTER (
        WHERE events.status = 'walkover'
    ) AS walkover,
    COUNT(*) FILTER (
        WHERE events.status = 'interrupted'
    ) AS interrupted,
    COUNT(*) FILTER (
        WHERE events.status = 'abandoned'
    ) AS abandoned,
    COUNT(*) FILTER (
        WHERE events.status = 'retired'
    ) AS retired,
    COUNT(*) FILTER (
        WHERE events.status = 'suspended'
    ) AS suspended,
    COUNT(*) FILTER (
        WHERE events.status = 'decided_by_fa'
    ) AS decided_by_fa,
    COUNT(*) FILTER (
        WHERE events.status = 'removed'
    ) AS removed
FROM events
WHERE (
        events.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
    )
GROUP BY date
ORDER BY date;