// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: providers.sql

package database

import (
	"context"
	"encoding/json"
)

const createProvider = `-- name: CreateProvider :one
INSERT INTO providers (
  user_id,
  type,
  name,
  config
) VALUES (
  $1, $2, $3, $4
)
RETURNING id, user_id, type, name, config, created_at, updated_at, deleted_at
`

type CreateProviderParams struct {
	UserID int64           `json:"user_id"`
	Type   ProviderType    `json:"type"`
	Name   string          `json:"name"`
	Config json.RawMessage `json:"config"`
}

func (q *Queries) CreateProvider(ctx context.Context, arg CreateProviderParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, createProvider,
		arg.UserID,
		arg.Type,
		arg.Name,
		arg.Config,
	)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const deleteProvider = `-- name: DeleteProvider :one
UPDATE providers
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING id, user_id, type, name, config, created_at, updated_at, deleted_at
`

func (q *Queries) DeleteProvider(ctx context.Context, id int64) (Provider, error) {
	row := q.db.QueryRowContext(ctx, deleteProvider, id)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const deleteUserProvider = `-- name: DeleteUserProvider :one
UPDATE providers
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $2
RETURNING id, user_id, type, name, config, created_at, updated_at, deleted_at
`

type DeleteUserProviderParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) DeleteUserProvider(ctx context.Context, arg DeleteUserProviderParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, deleteUserProvider, arg.ID, arg.UserID)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getProvider = `-- name: GetProvider :one
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
WHERE p.id = $1 AND p.deleted_at IS NULL
`

func (q *Queries) GetProvider(ctx context.Context, id int64) (Provider, error) {
	row := q.db.QueryRowContext(ctx, getProvider, id)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getProviders = `-- name: GetProviders :many
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
WHERE p.deleted_at IS NULL
ORDER BY p.created_at ASC
`

func (q *Queries) GetProviders(ctx context.Context) ([]Provider, error) {
	rows, err := q.db.QueryContext(ctx, getProviders)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Provider
	for rows.Next() {
		var i Provider
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Type,
			&i.Name,
			&i.Config,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getProvidersByType = `-- name: GetProvidersByType :many
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
WHERE p.type = $1 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC
`

func (q *Queries) GetProvidersByType(ctx context.Context, type_ ProviderType) ([]Provider, error) {
	rows, err := q.db.QueryContext(ctx, getProvidersByType, type_)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Provider
	for rows.Next() {
		var i Provider
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Type,
			&i.Name,
			&i.Config,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserProvider = `-- name: GetUserProvider :one
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.id = $1 AND p.user_id = $2 AND p.deleted_at IS NULL
`

type GetUserProviderParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) GetUserProvider(ctx context.Context, arg GetUserProviderParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, getUserProvider, arg.ID, arg.UserID)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getUserProviderByName = `-- name: GetUserProviderByName :one
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.name = $1 AND p.user_id = $2 AND p.deleted_at IS NULL
`

type GetUserProviderByNameParams struct {
	Name   string `json:"name"`
	UserID int64  `json:"user_id"`
}

func (q *Queries) GetUserProviderByName(ctx context.Context, arg GetUserProviderByNameParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, getUserProviderByName, arg.Name, arg.UserID)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getUserProviders = `-- name: GetUserProviders :many
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.user_id = $1 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC
`

func (q *Queries) GetUserProviders(ctx context.Context, userID int64) ([]Provider, error) {
	rows, err := q.db.QueryContext(ctx, getUserProviders, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Provider
	for rows.Next() {
		var i Provider
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Type,
			&i.Name,
			&i.Config,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserProvidersByType = `-- name: GetUserProvidersByType :many
SELECT
  p.id, p.user_id, p.type, p.name, p.config, p.created_at, p.updated_at, p.deleted_at
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.user_id = $1 AND p.type = $2 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC
`

type GetUserProvidersByTypeParams struct {
	UserID int64        `json:"user_id"`
	Type   ProviderType `json:"type"`
}

func (q *Queries) GetUserProvidersByType(ctx context.Context, arg GetUserProvidersByTypeParams) ([]Provider, error) {
	rows, err := q.db.QueryContext(ctx, getUserProvidersByType, arg.UserID, arg.Type)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Provider
	for rows.Next() {
		var i Provider
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Type,
			&i.Name,
			&i.Config,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateProvider = `-- name: UpdateProvider :one
UPDATE providers
SET config = $2, name = $3
WHERE id = $1
RETURNING id, user_id, type, name, config, created_at, updated_at, deleted_at
`

type UpdateProviderParams struct {
	ID     int64           `json:"id"`
	Config json.RawMessage `json:"config"`
	Name   string          `json:"name"`
}

func (q *Queries) UpdateProvider(ctx context.Context, arg UpdateProviderParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, updateProvider, arg.ID, arg.Config, arg.Name)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const updateUserProvider = `-- name: UpdateUserProvider :one
UPDATE providers
SET config = $3, name = $4
WHERE id = $1 AND user_id = $2
RETURNING id, user_id, type, name, config, created_at, updated_at, deleted_at
`

type UpdateUserProviderParams struct {
	ID     int64           `json:"id"`
	UserID int64           `json:"user_id"`
	Config json.RawMessage `json:"config"`
	Name   string          `json:"name"`
}

func (q *Queries) UpdateUserProvider(ctx context.Context, arg UpdateUserProviderParams) (Provider, error) {
	row := q.db.QueryRowContext(ctx, updateUserProvider,
		arg.ID,
		arg.UserID,
		arg.Config,
		arg.Name,
	)
	var i Provider
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Type,
		&i.Name,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}
