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

package dbgen

import (
	"context"

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

const CreateOtp = `-- name: CreateOtp :exec
INSERT INTO otps (
  user_id,
  sent_to,
  medium,
  otp_for,
  otp,
  expires_at
)
VALUES ($1, $2, $3, $4, $5, $6)
`

type CreateOtpParams struct {
	UserID    int64              `json:"user_id"`
	SentTo    string             `json:"sent_to"`
	Medium    string             `json:"medium"`
	OtpFor    string             `json:"otp_for"`
	Otp       string             `json:"otp"`
	ExpiresAt pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) CreateOtp(ctx context.Context, arg CreateOtpParams) error {
	_, err := q.db.Exec(ctx, CreateOtp,
		arg.UserID,
		arg.SentTo,
		arg.Medium,
		arg.OtpFor,
		arg.Otp,
		arg.ExpiresAt,
	)
	return err
}

const GetOtp = `-- name: GetOtp :one
SELECT id, user_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
FROM otps
WHERE user_id = $1
ORDER BY created_at DESC LIMIT 1
`

type GetOtpRow struct {
	ID        int64              `json:"id"`
	UserID    int64              `json:"user_id"`
	SentTo    string             `json:"sent_to"`
	Medium    string             `json:"medium"`
	OtpFor    string             `json:"otp_for"`
	Otp       string             `json:"otp"`
	Used      bool               `json:"used"`
	UsedAt    pgtype.Timestamptz `json:"used_at"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	ExpiresAt pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) GetOtp(ctx context.Context, userID int64) (GetOtpRow, error) {
	row := q.db.QueryRow(ctx, GetOtp, userID)
	var i GetOtpRow
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.SentTo,
		&i.Medium,
		&i.OtpFor,
		&i.Otp,
		&i.Used,
		&i.UsedAt,
		&i.CreatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const MarkOtpAsUsed = `-- name: MarkOtpAsUsed :exec
UPDATE otps
SET used = TRUE, used_at = $2
WHERE id = $1
`

type MarkOtpAsUsedParams struct {
	ID     int64              `json:"id"`
	UsedAt pgtype.Timestamptz `json:"used_at"`
}

func (q *Queries) MarkOtpAsUsed(ctx context.Context, arg MarkOtpAsUsedParams) error {
	_, err := q.db.Exec(ctx, MarkOtpAsUsed, arg.ID, arg.UsedAt)
	return err
}

const UpdateExpiredOtp = `-- name: UpdateExpiredOtp :exec
UPDATE otps
SET
    otp = $2,
    used = FALSE,
    used_at = NULL,
    expires_at = $3
WHERE user_id = $1
`

type UpdateExpiredOtpParams struct {
	UserID    int64              `json:"user_id"`
	Otp       string             `json:"otp"`
	ExpiresAt pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) UpdateExpiredOtp(ctx context.Context, arg UpdateExpiredOtpParams) error {
	_, err := q.db.Exec(ctx, UpdateExpiredOtp, arg.UserID, arg.Otp, arg.ExpiresAt)
	return err
}
