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

package configdb

import (
	"context"
	"time"

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

const clearOrganizationAPIKeyMappings = `-- name: ClearOrganizationAPIKeyMappings :exec
DELETE FROM organization_api_key_mappings
`

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

const clearOrganizationAPIKeys = `-- name: ClearOrganizationAPIKeys :exec
DELETE FROM organization_api_keys
`

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

const createOrganizationAPIKey = `-- name: CreateOrganizationAPIKey :one
INSERT INTO organization_api_keys (
  key_hash, name, description
) VALUES (
  $1, $2, $3
) RETURNING id, key_hash, name, description, created_at
`

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

func (q *Queries) CreateOrganizationAPIKey(ctx context.Context, arg CreateOrganizationAPIKeyParams) (OrganizationApiKey, error) {
	row := q.db.QueryRow(ctx, createOrganizationAPIKey, arg.KeyHash, arg.Name, arg.Description)
	var i OrganizationApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const createOrganizationAPIKeyMapping = `-- name: CreateOrganizationAPIKeyMapping :one
INSERT INTO organization_api_key_mappings (
  api_key_id, organization_id
) VALUES (
  $1, $2
) RETURNING id, api_key_id, organization_id
`

type CreateOrganizationAPIKeyMappingParams struct {
	ApiKeyID       uuid.UUID `json:"api_key_id"`
	OrganizationID uuid.UUID `json:"organization_id"`
}

func (q *Queries) CreateOrganizationAPIKeyMapping(ctx context.Context, arg CreateOrganizationAPIKeyMappingParams) (OrganizationApiKeyMapping, error) {
	row := q.db.QueryRow(ctx, createOrganizationAPIKeyMapping, arg.ApiKeyID, arg.OrganizationID)
	var i OrganizationApiKeyMapping
	err := row.Scan(&i.ID, &i.ApiKeyID, &i.OrganizationID)
	return i, err
}

const deleteOrganizationAPIKey = `-- name: DeleteOrganizationAPIKey :exec
DELETE FROM organization_api_keys
WHERE id = $1
`

func (q *Queries) DeleteOrganizationAPIKey(ctx context.Context, id uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteOrganizationAPIKey, id)
	return err
}

const deleteOrganizationAPIKeyMapping = `-- name: DeleteOrganizationAPIKeyMapping :exec
DELETE FROM organization_api_key_mappings
WHERE api_key_id = $1
`

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

const getAllCOrganizationAPIKeysForSync = `-- name: GetAllCOrganizationAPIKeysForSync :many
SELECT organization_id, api_key, name, enabled
FROM c_organization_api_keys
WHERE enabled = true OR enabled IS NULL
`

type GetAllCOrganizationAPIKeysForSyncRow struct {
	OrganizationID pgtype.UUID `json:"organization_id"`
	ApiKey         *string     `json:"api_key"`
	Name           *string     `json:"name"`
	Enabled        pgtype.Bool `json:"enabled"`
}

func (q *Queries) GetAllCOrganizationAPIKeysForSync(ctx context.Context) ([]GetAllCOrganizationAPIKeysForSyncRow, error) {
	rows, err := q.db.Query(ctx, getAllCOrganizationAPIKeysForSync)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetAllCOrganizationAPIKeysForSyncRow
	for rows.Next() {
		var i GetAllCOrganizationAPIKeysForSyncRow
		if err := rows.Scan(
			&i.OrganizationID,
			&i.ApiKey,
			&i.Name,
			&i.Enabled,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getAllOrganizationAPIKeys = `-- name: GetAllOrganizationAPIKeys :many
SELECT ak.id, ak.key_hash, ak.name, ak.description, ak.created_at, ako.organization_id
FROM organization_api_keys ak
JOIN organization_api_key_mappings ako ON ak.id = ako.api_key_id
`

type GetAllOrganizationAPIKeysRow struct {
	ID             uuid.UUID  `json:"id"`
	KeyHash        string     `json:"key_hash"`
	Name           string     `json:"name"`
	Description    *string    `json:"description"`
	CreatedAt      *time.Time `json:"created_at"`
	OrganizationID uuid.UUID  `json:"organization_id"`
}

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

const getOrganizationAPIKeyByHash = `-- name: GetOrganizationAPIKeyByHash :one
SELECT ak.id, ak.key_hash, ak.name, ak.description, ak.created_at, ako.organization_id
FROM organization_api_keys ak
JOIN organization_api_key_mappings ako ON ak.id = ako.api_key_id
WHERE ak.key_hash = $1
`

type GetOrganizationAPIKeyByHashRow struct {
	ID             uuid.UUID  `json:"id"`
	KeyHash        string     `json:"key_hash"`
	Name           string     `json:"name"`
	Description    *string    `json:"description"`
	CreatedAt      *time.Time `json:"created_at"`
	OrganizationID uuid.UUID  `json:"organization_id"`
}

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

const getOrganizationAPIKeyByID = `-- name: GetOrganizationAPIKeyByID :one
SELECT ak.id, ak.key_hash, ak.name, ak.description, ak.created_at, ako.organization_id
FROM organization_api_keys ak
JOIN organization_api_key_mappings ako ON ak.id = ako.api_key_id
WHERE ak.id = $1
`

type GetOrganizationAPIKeyByIDRow struct {
	ID             uuid.UUID  `json:"id"`
	KeyHash        string     `json:"key_hash"`
	Name           string     `json:"name"`
	Description    *string    `json:"description"`
	CreatedAt      *time.Time `json:"created_at"`
	OrganizationID uuid.UUID  `json:"organization_id"`
}

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

const listOrganizationAPIKeysByOrg = `-- name: ListOrganizationAPIKeysByOrg :many
SELECT ak.id, ak.name, ak.key_hash, ak.description
FROM organization_api_keys ak
JOIN organization_api_key_mappings ako ON ak.id = ako.api_key_id
WHERE ako.organization_id = $1
`

type ListOrganizationAPIKeysByOrgRow struct {
	ID          uuid.UUID `json:"id"`
	Name        string    `json:"name"`
	KeyHash     string    `json:"key_hash"`
	Description *string   `json:"description"`
}

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

const upsertOrganizationAPIKey = `-- name: UpsertOrganizationAPIKey :one
INSERT INTO organization_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 UpsertOrganizationAPIKeyParams struct {
	KeyHash     string  `json:"key_hash"`
	Name        string  `json:"name"`
	Description *string `json:"description"`
}

func (q *Queries) UpsertOrganizationAPIKey(ctx context.Context, arg UpsertOrganizationAPIKeyParams) (OrganizationApiKey, error) {
	row := q.db.QueryRow(ctx, upsertOrganizationAPIKey, arg.KeyHash, arg.Name, arg.Description)
	var i OrganizationApiKey
	err := row.Scan(
		&i.ID,
		&i.KeyHash,
		&i.Name,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const upsertOrganizationAPIKeyMapping = `-- name: UpsertOrganizationAPIKeyMapping :exec
INSERT INTO organization_api_key_mappings (
  api_key_id, organization_id
) VALUES (
  $1, $2
) ON CONFLICT (api_key_id) DO UPDATE SET
  organization_id = EXCLUDED.organization_id
`

type UpsertOrganizationAPIKeyMappingParams struct {
	ApiKeyID       uuid.UUID `json:"api_key_id"`
	OrganizationID uuid.UUID `json:"organization_id"`
}

func (q *Queries) UpsertOrganizationAPIKeyMapping(ctx context.Context, arg UpsertOrganizationAPIKeyMappingParams) error {
	_, err := q.db.Exec(ctx, upsertOrganizationAPIKeyMapping, arg.ApiKeyID, arg.OrganizationID)
	return err
}
