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

package sqlc

import (
	"context"

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

const createBotEmailBinding = `-- name: CreateBotEmailBinding :one
INSERT INTO bot_email_bindings (bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config)
VALUES (
  $1,
  $2,
  $3,
  $4,
  $5,
  $6,
  $7
)
RETURNING id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at
`

type CreateBotEmailBindingParams struct {
	BotID           pgtype.UUID `json:"bot_id"`
	EmailProviderID pgtype.UUID `json:"email_provider_id"`
	EmailAddress    string      `json:"email_address"`
	CanRead         bool        `json:"can_read"`
	CanWrite        bool        `json:"can_write"`
	CanDelete       bool        `json:"can_delete"`
	Config          []byte      `json:"config"`
}

func (q *Queries) CreateBotEmailBinding(ctx context.Context, arg CreateBotEmailBindingParams) (BotEmailBinding, error) {
	row := q.db.QueryRow(ctx, createBotEmailBinding,
		arg.BotID,
		arg.EmailProviderID,
		arg.EmailAddress,
		arg.CanRead,
		arg.CanWrite,
		arg.CanDelete,
		arg.Config,
	)
	var i BotEmailBinding
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.EmailProviderID,
		&i.EmailAddress,
		&i.CanRead,
		&i.CanWrite,
		&i.CanDelete,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteBotEmailBinding = `-- name: DeleteBotEmailBinding :exec
DELETE FROM bot_email_bindings WHERE id = $1
`

func (q *Queries) DeleteBotEmailBinding(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteBotEmailBinding, id)
	return err
}

const getBotEmailBindingByBotAndProvider = `-- name: GetBotEmailBindingByBotAndProvider :one
SELECT id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at FROM bot_email_bindings
WHERE bot_id = $1 AND email_provider_id = $2
`

type GetBotEmailBindingByBotAndProviderParams struct {
	BotID           pgtype.UUID `json:"bot_id"`
	EmailProviderID pgtype.UUID `json:"email_provider_id"`
}

func (q *Queries) GetBotEmailBindingByBotAndProvider(ctx context.Context, arg GetBotEmailBindingByBotAndProviderParams) (BotEmailBinding, error) {
	row := q.db.QueryRow(ctx, getBotEmailBindingByBotAndProvider, arg.BotID, arg.EmailProviderID)
	var i BotEmailBinding
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.EmailProviderID,
		&i.EmailAddress,
		&i.CanRead,
		&i.CanWrite,
		&i.CanDelete,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getBotEmailBindingByID = `-- name: GetBotEmailBindingByID :one
SELECT id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at FROM bot_email_bindings WHERE id = $1
`

func (q *Queries) GetBotEmailBindingByID(ctx context.Context, id pgtype.UUID) (BotEmailBinding, error) {
	row := q.db.QueryRow(ctx, getBotEmailBindingByID, id)
	var i BotEmailBinding
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.EmailProviderID,
		&i.EmailAddress,
		&i.CanRead,
		&i.CanWrite,
		&i.CanDelete,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listBotEmailBindings = `-- name: ListBotEmailBindings :many
SELECT id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at FROM bot_email_bindings
WHERE bot_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListBotEmailBindings(ctx context.Context, botID pgtype.UUID) ([]BotEmailBinding, error) {
	rows, err := q.db.Query(ctx, listBotEmailBindings, botID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotEmailBinding
	for rows.Next() {
		var i BotEmailBinding
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.EmailProviderID,
			&i.EmailAddress,
			&i.CanRead,
			&i.CanWrite,
			&i.CanDelete,
			&i.Config,
			&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 listBotEmailBindingsByProvider = `-- name: ListBotEmailBindingsByProvider :many
SELECT id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at FROM bot_email_bindings
WHERE email_provider_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListBotEmailBindingsByProvider(ctx context.Context, emailProviderID pgtype.UUID) ([]BotEmailBinding, error) {
	rows, err := q.db.Query(ctx, listBotEmailBindingsByProvider, emailProviderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotEmailBinding
	for rows.Next() {
		var i BotEmailBinding
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.EmailProviderID,
			&i.EmailAddress,
			&i.CanRead,
			&i.CanWrite,
			&i.CanDelete,
			&i.Config,
			&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 listReadableBindingsByProvider = `-- name: ListReadableBindingsByProvider :many
SELECT id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at FROM bot_email_bindings
WHERE email_provider_id = $1 AND can_read = TRUE
ORDER BY created_at DESC
`

func (q *Queries) ListReadableBindingsByProvider(ctx context.Context, emailProviderID pgtype.UUID) ([]BotEmailBinding, error) {
	rows, err := q.db.Query(ctx, listReadableBindingsByProvider, emailProviderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotEmailBinding
	for rows.Next() {
		var i BotEmailBinding
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.EmailProviderID,
			&i.EmailAddress,
			&i.CanRead,
			&i.CanWrite,
			&i.CanDelete,
			&i.Config,
			&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 updateBotEmailBinding = `-- name: UpdateBotEmailBinding :one
UPDATE bot_email_bindings
SET
  email_address = $1,
  can_read = $2,
  can_write = $3,
  can_delete = $4,
  config = $5,
  updated_at = now()
WHERE id = $6
RETURNING id, bot_id, email_provider_id, email_address, can_read, can_write, can_delete, config, created_at, updated_at
`

type UpdateBotEmailBindingParams struct {
	EmailAddress string      `json:"email_address"`
	CanRead      bool        `json:"can_read"`
	CanWrite     bool        `json:"can_write"`
	CanDelete    bool        `json:"can_delete"`
	Config       []byte      `json:"config"`
	ID           pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateBotEmailBinding(ctx context.Context, arg UpdateBotEmailBindingParams) (BotEmailBinding, error) {
	row := q.db.QueryRow(ctx, updateBotEmailBinding,
		arg.EmailAddress,
		arg.CanRead,
		arg.CanWrite,
		arg.CanDelete,
		arg.Config,
		arg.ID,
	)
	var i BotEmailBinding
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.EmailProviderID,
		&i.EmailAddress,
		&i.CanRead,
		&i.CanWrite,
		&i.CanDelete,
		&i.Config,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
