-- name: CreateFinancialReport :one
INSERT INTO virtual_game_financial_reports (
    game_id, provider_id, report_date, report_type,
    total_bets, total_wins, created_at
) VALUES ($1, $2, $3, $4, $5, $6, NOW())
RETURNING *;

-- name: UpsertFinancialReport :one
INSERT INTO virtual_game_financial_reports (
    game_id, provider_id, report_date, report_type,
    total_bets, total_wins, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW())
ON CONFLICT (game_id, provider_id, report_date, report_type)
DO UPDATE SET 
    total_bets = EXCLUDED.total_bets,
    total_wins = EXCLUDED.total_wins,
    updated_at = NOW()
RETURNING *;

-- name: GetFinancialReportByID :one
SELECT * FROM virtual_game_financial_reports
WHERE id = $1;

-- name: GetFinancialReportsForGame :many
SELECT * FROM virtual_game_financial_reports
WHERE game_id = $1
  AND provider_id = $2
  AND report_date BETWEEN $3 AND $4
ORDER BY report_date;

-- name: GetDailyFinancialReports :many
SELECT * FROM virtual_game_financial_reports
WHERE report_date = $1
  AND report_type = 'daily';

-- name: DeleteFinancialReport :exec
DELETE FROM virtual_game_financial_reports
WHERE id = $1;

-- name: CreateCompanyReport :one
INSERT INTO virtual_game_company_reports (
    company_id, provider_id,
    report_date, report_type,
    total_bet_amount, total_win_amount, created_at
)
VALUES ($1, $2, $3, $4, $5, $6, NOW())
RETURNING *;

-- name: UpsertCompanyReport :one
INSERT INTO virtual_game_company_reports (
    company_id, provider_id,
    report_date, report_type,
    total_bet_amount, total_win_amount,
    created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW())
ON CONFLICT (company_id, provider_id, report_date, report_type)
DO UPDATE SET 
    total_bet_amount = EXCLUDED.total_bet_amount,
    total_win_amount = EXCLUDED.total_win_amount,
    updated_at = NOW()
RETURNING *;

-- name: GetCompanyReportByID :one
SELECT * FROM virtual_game_company_reports
WHERE id = $1;

-- name: GetCompanyReportsInRange :many
SELECT * FROM virtual_game_company_reports
WHERE company_id = $1
  AND provider_id = $2
  AND report_date BETWEEN $3 AND $4
ORDER BY report_date;

-- name: GetCompanyProfitTrend :many
SELECT report_date, SUM(net_profit) AS total_profit
FROM virtual_game_company_reports
WHERE company_id = $1
  AND provider_id = $2
  AND report_date BETWEEN $3 AND $4
GROUP BY report_date
ORDER BY report_date;

-- name: CreatePlayerActivityReport :one
INSERT INTO virtual_game_player_activity_reports (
    user_id, report_date, report_type,
    total_deposits, total_withdrawals,
    total_bet_amount, total_win_amount,
    rounds_played, created_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW())
RETURNING *;

-- name: UpsertPlayerActivityReport :one
INSERT INTO virtual_game_player_activity_reports (
    user_id, report_date, report_type,
    total_deposits, total_withdrawals,
    total_bet_amount, total_win_amount,
    rounds_played, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW())
ON CONFLICT (user_id, report_date, report_type)
DO UPDATE SET 
    total_deposits = EXCLUDED.total_deposits,
    total_withdrawals = EXCLUDED.total_withdrawals,
    total_bet_amount = EXCLUDED.total_bet_amount,
    total_win_amount = EXCLUDED.total_win_amount,
    rounds_played = EXCLUDED.rounds_played,
    updated_at = NOW()
RETURNING *;

-- name: GetPlayerActivityByID :one
SELECT * FROM virtual_game_player_activity_reports
WHERE id = $1;

-- name: GetPlayerActivityByDate :one
SELECT * FROM virtual_game_player_activity_reports
WHERE user_id = $1
  AND report_date = $2
  AND report_type = $3;

-- name: GetPlayerActivityRange :many
SELECT * FROM virtual_game_player_activity_reports
WHERE user_id = $1
  AND report_date BETWEEN $2 AND $3
ORDER BY report_date;

-- name: GetTopPlayersByNetResult :many
SELECT user_id, SUM(net_result) AS total_net
FROM virtual_game_player_activity_reports
WHERE report_date BETWEEN $1 AND $2
GROUP BY user_id
ORDER BY total_net DESC
LIMIT $3;

-- name: DeletePlayerActivityReport :exec
DELETE FROM virtual_game_player_activity_reports
WHERE id = $1;
