-- name: GetTotalBetsMadeInRange :one
SELECT COUNT(*) AS total_bets
FROM bets
WHERE created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to');
-- name: GetTotalCashMadeInRange :one
SELECT COALESCE(SUM(amount), 0) AS total_cash_made
FROM bets
WHERE created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to');
-- name: GetTotalCashOutInRange :one
SELECT COALESCE(SUM(amount), 0) AS total_cash_out
FROM bets
WHERE created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to')
  AND cashed_out = true;
-- name: GetTotalCashBacksInRange :one
SELECT COALESCE(SUM(amount), 0) AS total_cash_backs
FROM bets
WHERE created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to')
  AND status = 5;
-- name: GetCompanyWiseReport :many
SELECT b.company_id,
  c.name AS company_name,
  COUNT(*) AS total_bets,
  COALESCE(SUM(b.amount), 0) AS total_cash_made,
  COALESCE(
    SUM(
      CASE
        WHEN b.cashed_out THEN b.amount
        ELSE 0
      END
    ),
    0
  ) AS total_cash_out,
  COALESCE(
    SUM(
      CASE
        WHEN b.status = 5 THEN b.amount
        ELSE 0
      END
    ),
    0
  ) AS total_cash_backs
FROM shop_bet_detail b
  JOIN companies c ON b.company_id = c.id
WHERE b.created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to')
GROUP BY b.company_id,
  c.name;
-- name: GetBranchWiseReport :many
SELECT b.branch_id,
  br.name AS branch_name,
  br.company_id,
  COUNT(*) AS total_bets,
  COALESCE(SUM(b.amount), 0) AS total_cash_made,
  COALESCE(
    SUM(
      CASE
        WHEN b.cashed_out THEN b.amount
        ELSE 0
      END
    ),
    0
  ) AS total_cash_out,
  COALESCE(
    SUM(
      CASE
        WHEN b.status = 5 THEN b.amount
        ELSE 0
      END
    ),
    0
  ) AS total_cash_backs
FROM shop_bet_detail b
  JOIN branches br ON b.branch_id = br.id
WHERE b.created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to')
GROUP BY b.branch_id,
  br.name,
  br.company_id;