-- name: CreateTicket :one
INSERT INTO tickets (amount, total_odds, ip, company_id)
VALUES ($1, $2, $3, $4)
RETURNING *;
-- name: CreateTicketOutcome :copyfrom
INSERT INTO ticket_outcomes (
        ticket_id,
        event_id,
        odd_id,
        home_team_name,
        away_team_name,
        market_id,
        market_name,
        odd,
        odd_name,
        odd_header,
        odd_handicap,
        expires
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12
    );
-- name: GetAllTickets :many
SELECT *
FROM ticket_with_outcomes
WHERE (
        company_id = sqlc.narg('company_id')
        OR sqlc.narg('company_id') IS NULL
    );
-- name: GetTicketByID :one
SELECT *
FROM ticket_with_outcomes
WHERE id = $1;
-- name: GetTicketOutcome :many
SELECT *
FROM ticket_outcomes
WHERE ticket_id = $1;
-- name: CountTicketByIP :one
SELECT count(id)
FROM tickets
WHERE IP = $1;
-- name: UpdateTicketOutcomeStatus :exec
UPDATE ticket_outcomes
SET status = $1
WHERE id = $2;
-- name: DeleteTicket :exec
DELETE FROM tickets
WHERE id = $1;
-- name: DeleteOldTickets :exec
Delete from tickets
where created_at < now() - interval '1 day';
-- name: DeleteTicketOutcome :exec
Delete from ticket_outcomes
where ticket_id = $1;
-- name: GetAllTicketsInRange :one
SELECT COUNT(*) as total_tickets,
    COALESCE(SUM(amount), 0) as total_amount
FROM tickets
WHERE created_at BETWEEN $1 AND $2;