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

package dbq

import (
	"context"
)

const bumpSourcesVersion = `-- name: BumpSourcesVersion :one
UPDATE cover_art_sources_meta
   SET current_version = current_version + 1
 WHERE id = true
RETURNING current_version
`

// Increments and returns the new version. Called by SettingsService
// only when the *enabled set* changes (not on key/display_order
// changes). RETURNING gives the new value back so the handler can
// include it in the response without a follow-up SELECT.
func (q *Queries) BumpSourcesVersion(ctx context.Context) (int32, error) {
	row := q.db.QueryRow(ctx, bumpSourcesVersion)
	var current_version int32
	err := row.Scan(&current_version)
	return current_version, err
}

const bumpVersionAndSetProvidersHash = `-- name: BumpVersionAndSetProvidersHash :one
UPDATE cover_art_sources_meta
   SET current_version = current_version + 1,
       last_registered_providers_hash = $1
 WHERE id = true
RETURNING current_version
`

// Atomically increments the version and stores the new provider-set
// hash. Called at boot when the registered-provider set has changed
// since the last start, so 'none' rows become eligible for retry.
// RETURNING gives the new version without a follow-up SELECT.
func (q *Queries) BumpVersionAndSetProvidersHash(ctx context.Context, lastRegisteredProvidersHash string) (int32, error) {
	row := q.db.QueryRow(ctx, bumpVersionAndSetProvidersHash, lastRegisteredProvidersHash)
	var current_version int32
	err := row.Scan(&current_version)
	return current_version, err
}

const getCoverArtProvidersHash = `-- name: GetCoverArtProvidersHash :one
SELECT last_registered_providers_hash
  FROM cover_art_sources_meta
 WHERE id = true
`

func (q *Queries) GetCoverArtProvidersHash(ctx context.Context) (string, error) {
	row := q.db.QueryRow(ctx, getCoverArtProvidersHash)
	var last_registered_providers_hash string
	err := row.Scan(&last_registered_providers_hash)
	return last_registered_providers_hash, err
}

const getCurrentSourcesVersion = `-- name: GetCurrentSourcesVersion :one
SELECT current_version FROM cover_art_sources_meta WHERE id = true
`

func (q *Queries) GetCurrentSourcesVersion(ctx context.Context) (int32, error) {
	row := q.db.QueryRow(ctx, getCurrentSourcesVersion)
	var current_version int32
	err := row.Scan(&current_version)
	return current_version, err
}

const listProviderSettings = `-- name: ListProviderSettings :many

SELECT provider_id, enabled, api_key, display_order, created_at, updated_at
  FROM cover_art_provider_settings
 ORDER BY display_order, provider_id
`

// M7 cover-sources: queries for the cover_art_provider_settings table
// and the cover_art_sources_meta singleton. The SettingsService
// consumes these.
// Returns all rows from cover_art_provider_settings. Used by the
// SettingsService at boot for reconciliation and by the admin GET
// handler.
func (q *Queries) ListProviderSettings(ctx context.Context) ([]CoverArtProviderSetting, error) {
	rows, err := q.db.Query(ctx, listProviderSettings)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []CoverArtProviderSetting
	for rows.Next() {
		var i CoverArtProviderSetting
		if err := rows.Scan(
			&i.ProviderID,
			&i.Enabled,
			&i.ApiKey,
			&i.DisplayOrder,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateProviderSettings = `-- name: UpdateProviderSettings :exec
UPDATE cover_art_provider_settings
   SET enabled    = COALESCE($2, enabled),
       api_key    = CASE WHEN $3::boolean THEN $4 ELSE api_key END,
       updated_at = now()
 WHERE provider_id = $1
`

type UpdateProviderSettingsParams struct {
	ProviderID string
	Enabled    bool
	Column3    bool
	ApiKey     *string
}

// Admin PATCH applies one or both fields. Pass NULL for enabled to
// leave it unchanged. Pass FALSE for the apiKeyChanged flag to leave
// the api_key unchanged regardless of apiKey value; pass TRUE with
// empty string to clear, or TRUE with non-empty to set.
func (q *Queries) UpdateProviderSettings(ctx context.Context, arg UpdateProviderSettingsParams) error {
	_, err := q.db.Exec(ctx, updateProviderSettings,
		arg.ProviderID,
		arg.Enabled,
		arg.Column3,
		arg.ApiKey,
	)
	return err
}

const upsertProviderSettings = `-- name: UpsertProviderSettings :exec
INSERT INTO cover_art_provider_settings (provider_id, enabled, display_order)
VALUES ($1, $2, $3)
ON CONFLICT (provider_id) DO UPDATE
   SET display_order = EXCLUDED.display_order,
       updated_at    = now()
`

type UpsertProviderSettingsParams struct {
	ProviderID   string
	Enabled      bool
	DisplayOrder int32
}

// INSERT a default row for a newly-registered provider, or update an
// existing row's display_order on boot reconciliation. Does NOT
// overwrite enabled / api_key on conflict — the operator's settings
// persist across restarts.
func (q *Queries) UpsertProviderSettings(ctx context.Context, arg UpsertProviderSettingsParams) error {
	_, err := q.db.Exec(ctx, upsertProviderSettings, arg.ProviderID, arg.Enabled, arg.DisplayOrder)
	return err
}
