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

package repository

import (
	"context"

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

const createAPIKey = `-- name: CreateAPIKey :one
INSERT INTO api_keys (id, user_id, name, key_hash, scopes)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, user_id, name, key_hash, scopes, created_at, revoked_at
`

type CreateAPIKeyParams struct {
	ID      pgtype.UUID `json:"id"`
	UserID  pgtype.UUID `json:"user_id"`
	Name    string      `json:"name"`
	KeyHash string      `json:"key_hash"`
	Scopes  []byte      `json:"scopes"`
}

func (q *Queries) CreateAPIKey(ctx context.Context, arg CreateAPIKeyParams) (ApiKey, error) {
	row := q.db.QueryRow(ctx, createAPIKey,
		arg.ID,
		arg.UserID,
		arg.Name,
		arg.KeyHash,
		arg.Scopes,
	)
	var i ApiKey
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Name,
		&i.KeyHash,
		&i.Scopes,
		&i.CreatedAt,
		&i.RevokedAt,
	)
	return i, err
}

const getAPIKeyByHash = `-- name: GetAPIKeyByHash :one
SELECT id, user_id, name, key_hash, scopes, created_at, revoked_at
FROM api_keys
WHERE key_hash = $1 AND revoked_at IS NULL
`

func (q *Queries) GetAPIKeyByHash(ctx context.Context, keyHash string) (ApiKey, error) {
	row := q.db.QueryRow(ctx, getAPIKeyByHash, keyHash)
	var i ApiKey
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Name,
		&i.KeyHash,
		&i.Scopes,
		&i.CreatedAt,
		&i.RevokedAt,
	)
	return i, err
}

const listAPIKeysByUser = `-- name: ListAPIKeysByUser :many
SELECT id, name, scopes, created_at, revoked_at
FROM api_keys
WHERE user_id = $1
ORDER BY created_at DESC
`

type ListAPIKeysByUserRow struct {
	ID        pgtype.UUID        `json:"id"`
	Name      string             `json:"name"`
	Scopes    []byte             `json:"scopes"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	RevokedAt pgtype.Timestamptz `json:"revoked_at"`
}

func (q *Queries) ListAPIKeysByUser(ctx context.Context, userID pgtype.UUID) ([]ListAPIKeysByUserRow, error) {
	rows, err := q.db.Query(ctx, listAPIKeysByUser, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAPIKeysByUserRow{}
	for rows.Next() {
		var i ListAPIKeysByUserRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Scopes,
			&i.CreatedAt,
			&i.RevokedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const revokeAPIKey = `-- name: RevokeAPIKey :exec
UPDATE api_keys SET revoked_at = now()
WHERE id = $1 AND user_id = $2 AND revoked_at IS NULL
`

type RevokeAPIKeyParams struct {
	ID     pgtype.UUID `json:"id"`
	UserID pgtype.UUID `json:"user_id"`
}

func (q *Queries) RevokeAPIKey(ctx context.Context, arg RevokeAPIKeyParams) error {
	_, err := q.db.Exec(ctx, revokeAPIKey, arg.ID, arg.UserID)
	return err
}

const revokeAllUserAPIKeys = `-- name: RevokeAllUserAPIKeys :exec
UPDATE api_keys SET revoked_at = now()
WHERE user_id = $1 AND revoked_at IS NULL
`

func (q *Queries) RevokeAllUserAPIKeys(ctx context.Context, userID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, revokeAllUserAPIKeys, userID)
	return err
}
