// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: refresh_tokens.sql

package database

import (
	"context"
	"database/sql"
	"time"

	"github.com/google/uuid"
)

const createRefreshToken = `-- name: CreateRefreshToken :one
INSERT INTO refresh_tokens (created_at, updated_at, token, expires_at, revoked_at, user_id)
VALUES (
  NOW(),
  NOW(),
  $1,
  $2,
  $3,
  $4
)
RETURNING token, created_at, updated_at, expires_at, revoked_at, user_id
`

type CreateRefreshTokenParams struct {
	Token     string
	ExpiresAt time.Time
	RevokedAt sql.NullTime
	UserID    uuid.UUID
}

func (q *Queries) CreateRefreshToken(ctx context.Context, arg CreateRefreshTokenParams) (RefreshToken, error) {
	row := q.db.QueryRowContext(ctx, createRefreshToken,
		arg.Token,
		arg.ExpiresAt,
		arg.RevokedAt,
		arg.UserID,
	)
	var i RefreshToken
	err := row.Scan(
		&i.Token,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExpiresAt,
		&i.RevokedAt,
		&i.UserID,
	)
	return i, err
}

const getRefreshTokenByToken = `-- name: GetRefreshTokenByToken :one
SELECT 
  created_at, 
  updated_at, 
  token, 
  expires_at, 
  revoked_at, 
  user_id 
FROM refresh_tokens
WHERE token = $1
`

type GetRefreshTokenByTokenRow struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	Token     string
	ExpiresAt time.Time
	RevokedAt sql.NullTime
	UserID    uuid.UUID
}

func (q *Queries) GetRefreshTokenByToken(ctx context.Context, token string) (GetRefreshTokenByTokenRow, error) {
	row := q.db.QueryRowContext(ctx, getRefreshTokenByToken, token)
	var i GetRefreshTokenByTokenRow
	err := row.Scan(
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Token,
		&i.ExpiresAt,
		&i.RevokedAt,
		&i.UserID,
	)
	return i, err
}

const revokeRefreshToken = `-- name: RevokeRefreshToken :exec
UPDATE refresh_tokens
SET revoked_at = NOW(), updated_at = NOW()
WHERE token = $1
`

func (q *Queries) RevokeRefreshToken(ctx context.Context, token string) error {
	_, err := q.db.ExecContext(ctx, revokeRefreshToken, token)
	return err
}
