// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: invites.sql

package dbgen

import (
	"context"
	"database/sql"
)

const createInvite = `-- name: CreateInvite :exec
INSERT INTO invites (code, created_by, max_uses, expires_at) VALUES (?, ?, ?, ?)
`

type CreateInviteParams struct {
	Code      string  `json:"code"`
	CreatedBy int64   `json:"createdBy"`
	MaxUses   *int64  `json:"maxUses"`
	ExpiresAt *string `json:"expiresAt"`
}

func (q *Queries) CreateInvite(ctx context.Context, arg CreateInviteParams) error {
	_, err := q.db.ExecContext(ctx, createInvite,
		arg.Code,
		arg.CreatedBy,
		arg.MaxUses,
		arg.ExpiresAt,
	)
	return err
}

const getInvite = `-- name: GetInvite :one
SELECT id, code, created_by, max_uses, use_count, expires_at, revoked, created_at
FROM invites WHERE code = ?
`

type GetInviteRow struct {
	ID        int64   `json:"id"`
	Code      string  `json:"code"`
	CreatedBy int64   `json:"createdBy"`
	MaxUses   *int64  `json:"maxUses"`
	UseCount  int64   `json:"useCount"`
	ExpiresAt *string `json:"expiresAt"`
	Revoked   int64   `json:"revoked"`
	CreatedAt string  `json:"createdAt"`
}

func (q *Queries) GetInvite(ctx context.Context, code string) (GetInviteRow, error) {
	row := q.db.QueryRowContext(ctx, getInvite, code)
	var i GetInviteRow
	err := row.Scan(
		&i.ID,
		&i.Code,
		&i.CreatedBy,
		&i.MaxUses,
		&i.UseCount,
		&i.ExpiresAt,
		&i.Revoked,
		&i.CreatedAt,
	)
	return i, err
}

const listInvites = `-- name: ListInvites :many
SELECT id, code, created_by, max_uses, use_count, expires_at, revoked, created_at
FROM invites ORDER BY created_at DESC LIMIT 200
`

type ListInvitesRow struct {
	ID        int64   `json:"id"`
	Code      string  `json:"code"`
	CreatedBy int64   `json:"createdBy"`
	MaxUses   *int64  `json:"maxUses"`
	UseCount  int64   `json:"useCount"`
	ExpiresAt *string `json:"expiresAt"`
	Revoked   int64   `json:"revoked"`
	CreatedAt string  `json:"createdAt"`
}

func (q *Queries) ListInvites(ctx context.Context) ([]ListInvitesRow, error) {
	rows, err := q.db.QueryContext(ctx, listInvites)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListInvitesRow{}
	for rows.Next() {
		var i ListInvitesRow
		if err := rows.Scan(
			&i.ID,
			&i.Code,
			&i.CreatedBy,
			&i.MaxUses,
			&i.UseCount,
			&i.ExpiresAt,
			&i.Revoked,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const revokeInvite = `-- name: RevokeInvite :exec
UPDATE invites SET revoked = 1 WHERE code = ?
`

func (q *Queries) RevokeInvite(ctx context.Context, code string) error {
	_, err := q.db.ExecContext(ctx, revokeInvite, code)
	return err
}

const useInviteAtomic = `-- name: UseInviteAtomic :execresult
UPDATE invites SET use_count = use_count + 1
WHERE code = ? AND revoked = 0
  AND (max_uses IS NULL OR use_count < max_uses)
  AND (expires_at IS NULL OR strftime('%s', expires_at) > strftime('%s', 'now'))
`

func (q *Queries) UseInviteAtomic(ctx context.Context, code string) (sql.Result, error) {
	return q.db.ExecContext(ctx, useInviteAtomic, code)
}
