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

package configdb

import (
	"context"

	"github.com/google/uuid"
)

const clearAdminAPIKeys = `-- name: ClearAdminAPIKeys :exec
DELETE FROM admin_api_keys
`

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

const createAdminAPIKey = `-- name: CreateAdminAPIKey :one
INSERT INTO admin_api_keys (
  key_hash, name, description
) VALUES (
  $1, $2, $3
) RETURNING id, key_hash, name, description, created_at
`

type CreateAdminAPIKeyParams struct {
	KeyHash     string  `json:"key_hash"`
	Name        string  `json:"name"`
	Description *string `json:"description"`
}

func (q *Queries) CreateAdminAPIKey(ctx context.Context, arg CreateAdminAPIKeyParams) (AdminApiKey, error) {
	row := q.db.QueryRow(ctx, createAdminAPIKey, arg.KeyHash, arg.Name, arg.Description)
	var i AdminApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const deleteAdminAPIKey = `-- name: DeleteAdminAPIKey :exec
DELETE FROM admin_api_keys WHERE id = $1
`

func (q *Queries) DeleteAdminAPIKey(ctx context.Context, apiKeyID uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteAdminAPIKey, apiKeyID)
	return err
}

const getAdminAPIKeyByHash = `-- name: GetAdminAPIKeyByHash :one
SELECT id, key_hash, name, description, created_at FROM admin_api_keys WHERE key_hash = $1
`

func (q *Queries) GetAdminAPIKeyByHash(ctx context.Context, keyHash string) (AdminApiKey, error) {
	row := q.db.QueryRow(ctx, getAdminAPIKeyByHash, keyHash)
	var i AdminApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const getAdminAPIKeyByID = `-- name: GetAdminAPIKeyByID :one
SELECT id, key_hash, name, description, created_at FROM admin_api_keys WHERE id = $1
`

func (q *Queries) GetAdminAPIKeyByID(ctx context.Context, apiKeyID uuid.UUID) (AdminApiKey, error) {
	row := q.db.QueryRow(ctx, getAdminAPIKeyByID, apiKeyID)
	var i AdminApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const getAllAdminAPIKeys = `-- name: GetAllAdminAPIKeys :many
SELECT id, key_hash, name, description, created_at FROM admin_api_keys ORDER BY created_at DESC
`

func (q *Queries) GetAllAdminAPIKeys(ctx context.Context) ([]AdminApiKey, error) {
	rows, err := q.db.Query(ctx, getAllAdminAPIKeys)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []AdminApiKey
	for rows.Next() {
		var i AdminApiKey
		if err := rows.Scan(
			&i.ID,
			&i.KeyHash,
			&i.Name,
			&i.Description,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertAdminAPIKey = `-- name: UpsertAdminAPIKey :one
INSERT INTO admin_api_keys (
  key_hash, name, description
) VALUES (
  $1, $2, $3
) ON CONFLICT (key_hash) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description
RETURNING id, key_hash, name, description, created_at
`

type UpsertAdminAPIKeyParams struct {
	KeyHash     string  `json:"key_hash"`
	Name        string  `json:"name"`
	Description *string `json:"description"`
}

func (q *Queries) UpsertAdminAPIKey(ctx context.Context, arg UpsertAdminAPIKeyParams) (AdminApiKey, error) {
	row := q.db.QueryRow(ctx, upsertAdminAPIKey, arg.KeyHash, arg.Name, arg.Description)
	var i AdminApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}
