-- name: CreateVirtualGameProvider :one
INSERT INTO virtual_game_providers (
    provider_id, provider_name, logo_dark, logo_light, enabled
) VALUES (
    $1, $2, $3, $4, $5
) RETURNING id, provider_id, provider_name, logo_dark, logo_light, enabled, created_at, updated_at;

-- name: DeleteVirtualGameProvider :exec
DELETE FROM virtual_game_providers
WHERE provider_id = $1;

-- name: DeleteAllVirtualGameProviders :exec
DELETE FROM virtual_game_providers;

-- name: GetVirtualGameProviderByID :one
SELECT id, provider_id, provider_name, logo_dark, logo_light, enabled, created_at, updated_at
FROM virtual_game_providers
WHERE provider_id = $1;

-- name: ListVirtualGameProviders :many
SELECT id, provider_id, provider_name, logo_dark, logo_light, enabled, created_at, updated_at
FROM virtual_game_providers
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;

-- name: CountVirtualGameProviders :one
SELECT COUNT(*) AS total
FROM virtual_game_providers;

-- name: UpdateVirtualGameProviderEnabled :one
UPDATE virtual_game_providers
SET enabled = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE provider_id = $1
RETURNING id, provider_id, provider_name, logo_dark, logo_light, enabled, created_at, updated_at;

-- name: CreateVirtualGameSession :one
INSERT INTO virtual_game_sessions (
    user_id, game_id, session_token, currency, status, expires_at
) VALUES (
    $1, $2, $3, $4, $5, $6
) RETURNING id, user_id, game_id, session_token, currency, status, created_at, updated_at, expires_at;
-- name: GetVirtualGameSessionByToken :one
SELECT id, user_id, game_id, session_token, currency, status, created_at, updated_at, expires_at
FROM virtual_game_sessions
WHERE session_token = $1;
-- name: UpdateVirtualGameSessionStatus :exec
UPDATE virtual_game_sessions
SET status = $2, updated_at = CURRENT_TIMESTAMP
WHERE id = $1;
-- name: CreateVirtualGameTransaction :one
INSERT INTO virtual_game_transactions (
    session_id, user_id, company_id, provider, wallet_id, transaction_type, amount, currency, external_transaction_id, status
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) RETURNING id, session_id, user_id, company_id, provider, wallet_id, transaction_type, amount, currency, external_transaction_id, status, created_at, updated_at;
-- name: CreateVirtualGameHistory :one
INSERT INTO virtual_game_histories (
    session_id,
    user_id,
    company_id,
    provider,
    wallet_id,
    game_id,
    transaction_type,
    amount,
    currency,
    external_transaction_id,
    reference_transaction_id,
    status
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) RETURNING
    id,
    session_id,
    user_id,
    company_id,
    provider,
    wallet_id,
    game_id,
    transaction_type,
    amount,
    currency,
    external_transaction_id,
    reference_transaction_id,
    status,
    created_at,
    updated_at;
-- name: GetVirtualGameTransactionByExternalID :one
SELECT id, session_id, user_id, wallet_id, transaction_type, amount, currency, external_transaction_id, status, created_at, updated_at
FROM virtual_game_transactions
WHERE external_transaction_id = $1;
-- name: UpdateVirtualGameTransactionStatus :exec
UPDATE virtual_game_transactions
SET status = $2, updated_at = CURRENT_TIMESTAMP
WHERE id = $1;
-- name: GetVirtualGameSummaryInRange :many
SELECT
    c.name AS company_name,
    vg.name AS game_name,
    COUNT(vgt.id) AS number_of_bets,
    COALESCE(SUM(vgt.amount), 0) AS total_transaction_sum
FROM virtual_game_transactions vgt
JOIN virtual_game_sessions vgs ON vgt.session_id = vgs.id
JOIN virtual_games vg ON vgs.game_id = vg.id
JOIN companies c ON vgt.company_id = c.id
WHERE vgt.transaction_type = 'BET'
  AND vgt.created_at BETWEEN $1 AND $2
GROUP BY c.name, vg.name;
-- name: AddFavoriteGame :exec
INSERT INTO favorite_games (
    user_id,
    game_id,
    created_at
) VALUES ($1, $2, NOW())
ON CONFLICT (user_id, game_id) DO NOTHING;
-- name: RemoveFavoriteGame :exec
DELETE FROM favorite_games
WHERE user_id = $1 AND game_id = $2;
-- name: ListFavoriteGames :many
SELECT game_id
FROM favorite_games
WHERE user_id = $1;

