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

package sqlc

import (
	"context"

	"github.com/google/uuid"
)

const createTempEntryVersionTable = `-- name: CreateTempEntryVersionTable :exec

CREATE TEMP TABLE temp_entry_version ON COMMIT DROP AS
SELECT id, entry_id, version, title, description, created_at, updated_at, name FROM entry_version
  WITH NO DATA
`

// Temp Entry Version Table Operations
func (q *Queries) CreateTempEntryVersionTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempEntryVersionTable)
	return err
}

const createTempIconTable = `-- name: CreateTempIconTable :exec

CREATE TEMP TABLE temp_mcp_server_icon ON COMMIT DROP AS
SELECT server_id, source_uri, mime_type, theme FROM mcp_server_icon
  WITH NO DATA
`

// Temp Icon Table Operations
func (q *Queries) CreateTempIconTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempIconTable)
	return err
}

const createTempPackageTable = `-- name: CreateTempPackageTable :exec

CREATE TEMP TABLE temp_mcp_server_package ON COMMIT DROP AS
SELECT server_id, registry_type, pkg_registry_url, pkg_identifier, pkg_version, runtime_hint, runtime_arguments, package_arguments, sha256_hash, transport, transport_url, env_vars, transport_headers FROM mcp_server_package
  WITH NO DATA
`

// Temp Package Table Operations
func (q *Queries) CreateTempPackageTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempPackageTable)
	return err
}

const createTempRegistryEntryTable = `-- name: CreateTempRegistryEntryTable :exec


CREATE TEMP TABLE temp_registry_entry ON COMMIT DROP AS
SELECT id, source_id, entry_type, name, created_at, updated_at, claims FROM registry_entry
  WITH NO DATA
`

// Temporary table operations for bulk sync
// Note: These queries reference temp tables that don't exist in the schema.
// sqlc cannot validate these, but we organize them here for maintainability.
// Temp Server Table Operations
func (q *Queries) CreateTempRegistryEntryTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempRegistryEntryTable)
	return err
}

const createTempRemoteTable = `-- name: CreateTempRemoteTable :exec

CREATE TEMP TABLE temp_mcp_server_remote ON COMMIT DROP AS
SELECT server_id, transport, transport_url, transport_headers FROM mcp_server_remote
  WITH NO DATA
`

// Temp Remote Table Operations
func (q *Queries) CreateTempRemoteTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempRemoteTable)
	return err
}

const createTempServerTable = `-- name: CreateTempServerTable :exec

CREATE TEMP TABLE temp_mcp_server ON COMMIT DROP AS
SELECT website, upstream_meta, server_meta, repository_url, repository_id, repository_subfolder, repository_type, version_id FROM mcp_server
  WITH NO DATA
`

// Temp Server Table Operations
func (q *Queries) CreateTempServerTable(ctx context.Context) error {
	_, err := q.db.Exec(ctx, createTempServerTable)
	return err
}

const deleteOrphanedIcons = `-- name: DeleteOrphanedIcons :exec
DELETE FROM mcp_server_icon
WHERE server_id = ANY($1::UUID[])
  AND (server_id, source_uri, mime_type, theme) NOT IN (
    SELECT server_id, source_uri, mime_type, theme FROM temp_mcp_server_icon
  )
`

func (q *Queries) DeleteOrphanedIcons(ctx context.Context, serverIds []uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteOrphanedIcons, serverIds)
	return err
}

const deleteOrphanedPackages = `-- name: DeleteOrphanedPackages :exec
DELETE FROM mcp_server_package
WHERE server_id = ANY($1::UUID[])
  AND (server_id, pkg_identifier, transport) NOT IN (
    SELECT server_id, pkg_identifier, transport FROM temp_mcp_server_package
  )
`

func (q *Queries) DeleteOrphanedPackages(ctx context.Context, serverIds []uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteOrphanedPackages, serverIds)
	return err
}

const deleteOrphanedRemotes = `-- name: DeleteOrphanedRemotes :exec
DELETE FROM mcp_server_remote
WHERE server_id = ANY($1::UUID[])
  AND (server_id, transport, transport_url) NOT IN (
    SELECT server_id, transport, transport_url FROM temp_mcp_server_remote
  )
`

func (q *Queries) DeleteOrphanedRemotes(ctx context.Context, serverIds []uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteOrphanedRemotes, serverIds)
	return err
}

const dropTempEntryVersionTable = `-- name: DropTempEntryVersionTable :exec
DROP TABLE IF EXISTS temp_entry_version
`

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

const dropTempRegistryEntryTable = `-- name: DropTempRegistryEntryTable :exec
DROP TABLE IF EXISTS temp_registry_entry
`

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

const upsertEntryVersionsFromTemp = `-- name: UpsertEntryVersionsFromTemp :many
INSERT INTO entry_version (
    id, entry_id, name, version, title, description, created_at, updated_at
)
SELECT id,
       entry_id,
       name,
       version,
       title,
       description,
       created_at,
       updated_at
  FROM temp_entry_version
    ON CONFLICT (entry_id, version)
    DO UPDATE SET
      name = EXCLUDED.name,
      title = EXCLUDED.title,
      description = EXCLUDED.description,
      updated_at = EXCLUDED.updated_at
RETURNING id, entry_id, version
`

type UpsertEntryVersionsFromTempRow struct {
	ID      uuid.UUID `json:"id"`
	EntryID uuid.UUID `json:"entry_id"`
	Version string    `json:"version"`
}

func (q *Queries) UpsertEntryVersionsFromTemp(ctx context.Context) ([]UpsertEntryVersionsFromTempRow, error) {
	rows, err := q.db.Query(ctx, upsertEntryVersionsFromTemp)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []UpsertEntryVersionsFromTempRow{}
	for rows.Next() {
		var i UpsertEntryVersionsFromTempRow
		if err := rows.Scan(&i.ID, &i.EntryID, &i.Version); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertIconsFromTemp = `-- name: UpsertIconsFromTemp :exec
INSERT INTO mcp_server_icon (server_id, source_uri, mime_type, theme)
SELECT server_id, source_uri, mime_type, theme::icon_theme
FROM temp_mcp_server_icon
ON CONFLICT (server_id, source_uri, mime_type, theme)
DO NOTHING
`

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

const upsertPackagesFromTemp = `-- name: UpsertPackagesFromTemp :exec
INSERT INTO mcp_server_package (
    server_id, registry_type, pkg_registry_url, pkg_identifier, pkg_version,
    runtime_hint, runtime_arguments, package_arguments, env_vars, sha256_hash,
    transport, transport_url, transport_headers
)
SELECT
    server_id, registry_type, pkg_registry_url, pkg_identifier, pkg_version,
    runtime_hint, runtime_arguments, package_arguments, env_vars, sha256_hash,
    transport, transport_url, transport_headers
FROM temp_mcp_server_package
ON CONFLICT (server_id, registry_type, pkg_identifier, transport)
DO UPDATE SET
    pkg_registry_url = EXCLUDED.pkg_registry_url,
    pkg_version = EXCLUDED.pkg_version,
    runtime_hint = EXCLUDED.runtime_hint,
    runtime_arguments = EXCLUDED.runtime_arguments,
    package_arguments = EXCLUDED.package_arguments,
    env_vars = EXCLUDED.env_vars,
    sha256_hash = EXCLUDED.sha256_hash,
    transport_url = EXCLUDED.transport_url,
    transport_headers = EXCLUDED.transport_headers
`

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

const upsertRegistryEntriesFromTemp = `-- name: UpsertRegistryEntriesFromTemp :many
INSERT INTO registry_entry (
    id, source_id, entry_type, name, claims, created_at, updated_at
)
SELECT id,
       source_id,
       entry_type,
       name,
       claims,
       created_at,
       updated_at
  FROM temp_registry_entry
    ON CONFLICT (source_id, entry_type, name)
    DO UPDATE SET
      claims = EXCLUDED.claims,
      updated_at = EXCLUDED.updated_at
RETURNING id, source_id, entry_type, name
`

type UpsertRegistryEntriesFromTempRow struct {
	ID        uuid.UUID `json:"id"`
	SourceID  uuid.UUID `json:"source_id"`
	EntryType EntryType `json:"entry_type"`
	Name      string    `json:"name"`
}

func (q *Queries) UpsertRegistryEntriesFromTemp(ctx context.Context) ([]UpsertRegistryEntriesFromTempRow, error) {
	rows, err := q.db.Query(ctx, upsertRegistryEntriesFromTemp)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []UpsertRegistryEntriesFromTempRow{}
	for rows.Next() {
		var i UpsertRegistryEntriesFromTempRow
		if err := rows.Scan(
			&i.ID,
			&i.SourceID,
			&i.EntryType,
			&i.Name,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertRemotesFromTemp = `-- name: UpsertRemotesFromTemp :exec
INSERT INTO mcp_server_remote (server_id, transport, transport_url, transport_headers)
SELECT server_id, transport, transport_url, transport_headers
FROM temp_mcp_server_remote
ON CONFLICT (server_id, transport, transport_url)
DO UPDATE SET transport_headers = EXCLUDED.transport_headers
`

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

const upsertServersFromTemp = `-- name: UpsertServersFromTemp :exec
INSERT INTO mcp_server (
    version_id, website, upstream_meta, server_meta,
    repository_url, repository_id, repository_subfolder, repository_type
)
SELECT version_id,
       website,
       upstream_meta,
       server_meta,
       repository_url,
       repository_id,
       repository_subfolder,
       repository_type
FROM temp_mcp_server
  ON CONFLICT (version_id)
  DO UPDATE SET
    website = EXCLUDED.website,
    upstream_meta = EXCLUDED.upstream_meta,
    server_meta = EXCLUDED.server_meta,
    repository_url = EXCLUDED.repository_url,
    repository_id = EXCLUDED.repository_id,
    repository_subfolder = EXCLUDED.repository_subfolder,
    repository_type = EXCLUDED.repository_type
`

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