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

package sqlc

import (
	"context"
	"time"

	"github.com/google/uuid"
)

const countRegistriesBySourceID = `-- name: CountRegistriesBySourceID :one
SELECT COUNT(*) FROM registry_source WHERE source_id = $1
`

// Count how many registries reference a given source (via registry_source junction).
func (q *Queries) CountRegistriesBySourceID(ctx context.Context, sourceID uuid.UUID) (int64, error) {
	row := q.db.QueryRow(ctx, countRegistriesBySourceID, sourceID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const deleteConfigRegistriesNotInList = `-- name: DeleteConfigRegistriesNotInList :exec
DELETE FROM registry
WHERE creation_type = 'CONFIG'
  AND name NOT IN (SELECT unnest($1::text[]))
`

// Delete CONFIG registry rows whose names are not in the provided list.
// Used during config sync to clean up registry/junction rows before deleting orphaned sources.
func (q *Queries) DeleteConfigRegistriesNotInList(ctx context.Context, keepNames []string) error {
	_, err := q.db.Exec(ctx, deleteConfigRegistriesNotInList, keepNames)
	return err
}

const deleteRegistry = `-- name: DeleteRegistry :execrows
DELETE FROM registry WHERE name = $1
`

// Delete a registry by name. Go callers guard against deleting wrong creation_type.
func (q *Queries) DeleteRegistry(ctx context.Context, name string) (int64, error) {
	result, err := q.db.Exec(ctx, deleteRegistry, name)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const getRegistryByName = `-- name: GetRegistryByName :one
SELECT id, name, claims, creation_type, created_at, updated_at
FROM registry WHERE name = $1
`

func (q *Queries) GetRegistryByName(ctx context.Context, name string) (Registry, error) {
	row := q.db.QueryRow(ctx, getRegistryByName, name)
	var i Registry
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Claims,
		&i.CreationType,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const linkRegistrySource = `-- name: LinkRegistrySource :exec
INSERT INTO registry_source (registry_id, source_id, position)
VALUES ($1, $2, $3)
ON CONFLICT (registry_id, source_id) DO UPDATE SET position = EXCLUDED.position
`

type LinkRegistrySourceParams struct {
	RegistryID uuid.UUID `json:"registry_id"`
	SourceID   uuid.UUID `json:"source_id"`
	Position   int32     `json:"position"`
}

func (q *Queries) LinkRegistrySource(ctx context.Context, arg LinkRegistrySourceParams) error {
	_, err := q.db.Exec(ctx, linkRegistrySource, arg.RegistryID, arg.SourceID, arg.Position)
	return err
}

const listRegistries = `-- name: ListRegistries :many

SELECT id, name, claims, creation_type, created_at, updated_at
FROM registry
WHERE ($1::text IS NULL OR name > $1)
ORDER BY name
LIMIT $2::bigint
`

type ListRegistriesParams struct {
	Cursor *string `json:"cursor"`
	Size   int64   `json:"size"`
}

// Queries for the new lightweight registry table and registry_source junction.
func (q *Queries) ListRegistries(ctx context.Context, arg ListRegistriesParams) ([]Registry, error) {
	rows, err := q.db.Query(ctx, listRegistries, arg.Cursor, arg.Size)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Registry{}
	for rows.Next() {
		var i Registry
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Claims,
			&i.CreationType,
			&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 listRegistrySources = `-- name: ListRegistrySources :many
SELECT s.id, s.name
FROM registry_source rs
JOIN source s ON rs.source_id = s.id
WHERE rs.registry_id = $1
ORDER BY rs.position
`

type ListRegistrySourcesRow struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
}

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

const unlinkAllRegistrySources = `-- name: UnlinkAllRegistrySources :exec
DELETE FROM registry_source WHERE registry_id = $1
`

func (q *Queries) UnlinkAllRegistrySources(ctx context.Context, registryID uuid.UUID) error {
	_, err := q.db.Exec(ctx, unlinkAllRegistrySources, registryID)
	return err
}

const unlinkRegistrySource = `-- name: UnlinkRegistrySource :exec
DELETE FROM registry_source
WHERE registry_id = $1 AND source_id = $2
`

type UnlinkRegistrySourceParams struct {
	RegistryID uuid.UUID `json:"registry_id"`
	SourceID   uuid.UUID `json:"source_id"`
}

func (q *Queries) UnlinkRegistrySource(ctx context.Context, arg UnlinkRegistrySourceParams) error {
	_, err := q.db.Exec(ctx, unlinkRegistrySource, arg.RegistryID, arg.SourceID)
	return err
}

const upsertRegistry = `-- name: UpsertRegistry :one
INSERT INTO registry (name, claims, creation_type, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (name) DO UPDATE SET claims = EXCLUDED.claims, updated_at = EXCLUDED.updated_at
RETURNING id, name, claims, creation_type, created_at, updated_at
`

type UpsertRegistryParams struct {
	Name         string       `json:"name"`
	Claims       []byte       `json:"claims"`
	CreationType CreationType `json:"creation_type"`
	CreatedAt    *time.Time   `json:"created_at"`
	UpdatedAt    *time.Time   `json:"updated_at"`
}

// Insert or update a registry. The creation_type is passed as a parameter.
// Business logic in Go guards against cross-type overwrites.
func (q *Queries) UpsertRegistry(ctx context.Context, arg UpsertRegistryParams) (Registry, error) {
	row := q.db.QueryRow(ctx, upsertRegistry,
		arg.Name,
		arg.Claims,
		arg.CreationType,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	var i Registry
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Claims,
		&i.CreationType,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
