// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: ticket.sql

package dbgen

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const CountTicketByIP = `-- name: CountTicketByIP :one
SELECT count(id)
FROM tickets
WHERE IP = $1
`

func (q *Queries) CountTicketByIP(ctx context.Context, ip string) (int64, error) {
	row := q.db.QueryRow(ctx, CountTicketByIP, ip)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const CreateTicket = `-- name: CreateTicket :one
INSERT INTO tickets (amount, total_odds, ip)
VALUES ($1, $2, $3)
RETURNING id, amount, total_odds, ip, created_at, updated_at
`

type CreateTicketParams struct {
	Amount    int64   `json:"amount"`
	TotalOdds float32 `json:"total_odds"`
	Ip        string  `json:"ip"`
}

func (q *Queries) CreateTicket(ctx context.Context, arg CreateTicketParams) (Ticket, error) {
	row := q.db.QueryRow(ctx, CreateTicket, arg.Amount, arg.TotalOdds, arg.Ip)
	var i Ticket
	err := row.Scan(
		&i.ID,
		&i.Amount,
		&i.TotalOdds,
		&i.Ip,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

type CreateTicketOutcomeParams struct {
	TicketID     int64            `json:"ticket_id"`
	EventID      int64            `json:"event_id"`
	OddID        int64            `json:"odd_id"`
	HomeTeamName string           `json:"home_team_name"`
	AwayTeamName string           `json:"away_team_name"`
	MarketID     int64            `json:"market_id"`
	MarketName   string           `json:"market_name"`
	Odd          float32          `json:"odd"`
	OddName      string           `json:"odd_name"`
	OddHeader    string           `json:"odd_header"`
	OddHandicap  string           `json:"odd_handicap"`
	Expires      pgtype.Timestamp `json:"expires"`
}

const DeleteOldTickets = `-- name: DeleteOldTickets :exec
Delete from tickets
where created_at < now() - interval '1 day'
`

func (q *Queries) DeleteOldTickets(ctx context.Context) error {
	_, err := q.db.Exec(ctx, DeleteOldTickets)
	return err
}

const DeleteTicket = `-- name: DeleteTicket :exec
DELETE FROM tickets
WHERE id = $1
`

func (q *Queries) DeleteTicket(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, DeleteTicket, id)
	return err
}

const DeleteTicketOutcome = `-- name: DeleteTicketOutcome :exec
Delete from ticket_outcomes
where ticket_id = $1
`

func (q *Queries) DeleteTicketOutcome(ctx context.Context, ticketID int64) error {
	_, err := q.db.Exec(ctx, DeleteTicketOutcome, ticketID)
	return err
}

const GetAllTickets = `-- name: GetAllTickets :many
SELECT id, amount, total_odds, ip, created_at, updated_at, outcomes
FROM ticket_with_outcomes
`

func (q *Queries) GetAllTickets(ctx context.Context) ([]TicketWithOutcome, error) {
	rows, err := q.db.Query(ctx, GetAllTickets)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []TicketWithOutcome
	for rows.Next() {
		var i TicketWithOutcome
		if err := rows.Scan(
			&i.ID,
			&i.Amount,
			&i.TotalOdds,
			&i.Ip,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Outcomes,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetTicketByID = `-- name: GetTicketByID :one
SELECT id, amount, total_odds, ip, created_at, updated_at, outcomes
FROM ticket_with_outcomes
WHERE id = $1
`

func (q *Queries) GetTicketByID(ctx context.Context, id int64) (TicketWithOutcome, error) {
	row := q.db.QueryRow(ctx, GetTicketByID, id)
	var i TicketWithOutcome
	err := row.Scan(
		&i.ID,
		&i.Amount,
		&i.TotalOdds,
		&i.Ip,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Outcomes,
	)
	return i, err
}

const GetTicketOutcome = `-- name: GetTicketOutcome :many
SELECT id, ticket_id, event_id, odd_id, home_team_name, away_team_name, market_id, market_name, odd, odd_name, odd_header, odd_handicap, status, expires
FROM ticket_outcomes
WHERE ticket_id = $1
`

func (q *Queries) GetTicketOutcome(ctx context.Context, ticketID int64) ([]TicketOutcome, error) {
	rows, err := q.db.Query(ctx, GetTicketOutcome, ticketID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []TicketOutcome
	for rows.Next() {
		var i TicketOutcome
		if err := rows.Scan(
			&i.ID,
			&i.TicketID,
			&i.EventID,
			&i.OddID,
			&i.HomeTeamName,
			&i.AwayTeamName,
			&i.MarketID,
			&i.MarketName,
			&i.Odd,
			&i.OddName,
			&i.OddHeader,
			&i.OddHandicap,
			&i.Status,
			&i.Expires,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateTicketOutcomeStatus = `-- name: UpdateTicketOutcomeStatus :exec
UPDATE ticket_outcomes
SET status = $1
WHERE id = $2
`

type UpdateTicketOutcomeStatusParams struct {
	Status int32 `json:"status"`
	ID     int64 `json:"id"`
}

func (q *Queries) UpdateTicketOutcomeStatus(ctx context.Context, arg UpdateTicketOutcomeStatusParams) error {
	_, err := q.db.Exec(ctx, UpdateTicketOutcomeStatus, arg.Status, arg.ID)
	return err
}
