// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/common/keys"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
	"github.com/lightsparkdev/spark/so/ent/tokentransactionpeersignature"
)

// TokenTransactionPeerSignatureCreate is the builder for creating a TokenTransactionPeerSignature entity.
type TokenTransactionPeerSignatureCreate struct {
	config
	mutation *TokenTransactionPeerSignatureMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreateTime sets the "create_time" field.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetCreateTime(t time.Time) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetCreateTime(t)
	return ttpsc
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetNillableCreateTime(t *time.Time) *TokenTransactionPeerSignatureCreate {
	if t != nil {
		ttpsc.SetCreateTime(*t)
	}
	return ttpsc
}

// SetUpdateTime sets the "update_time" field.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetUpdateTime(t time.Time) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetUpdateTime(t)
	return ttpsc
}

// SetNillableUpdateTime sets the "update_time" field if the given value is not nil.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetNillableUpdateTime(t *time.Time) *TokenTransactionPeerSignatureCreate {
	if t != nil {
		ttpsc.SetUpdateTime(*t)
	}
	return ttpsc
}

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetOperatorIdentityPublicKey(k keys.Public) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetOperatorIdentityPublicKey(k)
	return ttpsc
}

// SetSignature sets the "signature" field.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetSignature(b []byte) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetSignature(b)
	return ttpsc
}

// SetID sets the "id" field.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetID(u uuid.UUID) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetID(u)
	return ttpsc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetNillableID(u *uuid.UUID) *TokenTransactionPeerSignatureCreate {
	if u != nil {
		ttpsc.SetID(*u)
	}
	return ttpsc
}

// SetTokenTransactionID sets the "token_transaction" edge to the TokenTransaction entity by ID.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetTokenTransactionID(id uuid.UUID) *TokenTransactionPeerSignatureCreate {
	ttpsc.mutation.SetTokenTransactionID(id)
	return ttpsc
}

// SetTokenTransaction sets the "token_transaction" edge to the TokenTransaction entity.
func (ttpsc *TokenTransactionPeerSignatureCreate) SetTokenTransaction(t *TokenTransaction) *TokenTransactionPeerSignatureCreate {
	return ttpsc.SetTokenTransactionID(t.ID)
}

// Mutation returns the TokenTransactionPeerSignatureMutation object of the builder.
func (ttpsc *TokenTransactionPeerSignatureCreate) Mutation() *TokenTransactionPeerSignatureMutation {
	return ttpsc.mutation
}

// Save creates the TokenTransactionPeerSignature in the database.
func (ttpsc *TokenTransactionPeerSignatureCreate) Save(ctx context.Context) (*TokenTransactionPeerSignature, error) {
	ttpsc.defaults()
	return withHooks(ctx, ttpsc.sqlSave, ttpsc.mutation, ttpsc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (ttpsc *TokenTransactionPeerSignatureCreate) SaveX(ctx context.Context) *TokenTransactionPeerSignature {
	v, err := ttpsc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (ttpsc *TokenTransactionPeerSignatureCreate) Exec(ctx context.Context) error {
	_, err := ttpsc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ttpsc *TokenTransactionPeerSignatureCreate) ExecX(ctx context.Context) {
	if err := ttpsc.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (ttpsc *TokenTransactionPeerSignatureCreate) defaults() {
	if _, ok := ttpsc.mutation.CreateTime(); !ok {
		v := tokentransactionpeersignature.DefaultCreateTime()
		ttpsc.mutation.SetCreateTime(v)
	}
	if _, ok := ttpsc.mutation.UpdateTime(); !ok {
		v := tokentransactionpeersignature.DefaultUpdateTime()
		ttpsc.mutation.SetUpdateTime(v)
	}
	if _, ok := ttpsc.mutation.ID(); !ok {
		v := tokentransactionpeersignature.DefaultID()
		ttpsc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ttpsc *TokenTransactionPeerSignatureCreate) check() error {
	if _, ok := ttpsc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "TokenTransactionPeerSignature.create_time"`)}
	}
	if _, ok := ttpsc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "TokenTransactionPeerSignature.update_time"`)}
	}
	if _, ok := ttpsc.mutation.OperatorIdentityPublicKey(); !ok {
		return &ValidationError{Name: "operator_identity_public_key", err: errors.New(`ent: missing required field "TokenTransactionPeerSignature.operator_identity_public_key"`)}
	}
	if _, ok := ttpsc.mutation.Signature(); !ok {
		return &ValidationError{Name: "signature", err: errors.New(`ent: missing required field "TokenTransactionPeerSignature.signature"`)}
	}
	if v, ok := ttpsc.mutation.Signature(); ok {
		if err := tokentransactionpeersignature.SignatureValidator(v); err != nil {
			return &ValidationError{Name: "signature", err: fmt.Errorf(`ent: validator failed for field "TokenTransactionPeerSignature.signature": %w`, err)}
		}
	}
	if len(ttpsc.mutation.TokenTransactionIDs()) == 0 {
		return &ValidationError{Name: "token_transaction", err: errors.New(`ent: missing required edge "TokenTransactionPeerSignature.token_transaction"`)}
	}
	return nil
}

func (ttpsc *TokenTransactionPeerSignatureCreate) sqlSave(ctx context.Context) (*TokenTransactionPeerSignature, error) {
	if err := ttpsc.check(); err != nil {
		return nil, err
	}
	_node, _spec := ttpsc.createSpec()
	if err := sqlgraph.CreateNode(ctx, ttpsc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	ttpsc.mutation.id = &_node.ID
	ttpsc.mutation.done = true
	return _node, nil
}

func (ttpsc *TokenTransactionPeerSignatureCreate) createSpec() (*TokenTransactionPeerSignature, *sqlgraph.CreateSpec) {
	var (
		_node = &TokenTransactionPeerSignature{config: ttpsc.config}
		_spec = sqlgraph.NewCreateSpec(tokentransactionpeersignature.Table, sqlgraph.NewFieldSpec(tokentransactionpeersignature.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = ttpsc.conflict
	if id, ok := ttpsc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := ttpsc.mutation.CreateTime(); ok {
		_spec.SetField(tokentransactionpeersignature.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := ttpsc.mutation.UpdateTime(); ok {
		_spec.SetField(tokentransactionpeersignature.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := ttpsc.mutation.OperatorIdentityPublicKey(); ok {
		_spec.SetField(tokentransactionpeersignature.FieldOperatorIdentityPublicKey, field.TypeBytes, value)
		_node.OperatorIdentityPublicKey = value
	}
	if value, ok := ttpsc.mutation.Signature(); ok {
		_spec.SetField(tokentransactionpeersignature.FieldSignature, field.TypeBytes, value)
		_node.Signature = value
	}
	if nodes := ttpsc.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   tokentransactionpeersignature.TokenTransactionTable,
			Columns: []string{tokentransactionpeersignature.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.token_transaction_peer_signatures = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.TokenTransactionPeerSignature.Create().
//		SetCreateTime(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.TokenTransactionPeerSignatureUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (ttpsc *TokenTransactionPeerSignatureCreate) OnConflict(opts ...sql.ConflictOption) *TokenTransactionPeerSignatureUpsertOne {
	ttpsc.conflict = opts
	return &TokenTransactionPeerSignatureUpsertOne{
		create: ttpsc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (ttpsc *TokenTransactionPeerSignatureCreate) OnConflictColumns(columns ...string) *TokenTransactionPeerSignatureUpsertOne {
	ttpsc.conflict = append(ttpsc.conflict, sql.ConflictColumns(columns...))
	return &TokenTransactionPeerSignatureUpsertOne{
		create: ttpsc,
	}
}

type (
	// TokenTransactionPeerSignatureUpsertOne is the builder for "upsert"-ing
	//  one TokenTransactionPeerSignature node.
	TokenTransactionPeerSignatureUpsertOne struct {
		create *TokenTransactionPeerSignatureCreate
	}

	// TokenTransactionPeerSignatureUpsert is the "OnConflict" setter.
	TokenTransactionPeerSignatureUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdateTime sets the "update_time" field.
func (u *TokenTransactionPeerSignatureUpsert) SetUpdateTime(v time.Time) *TokenTransactionPeerSignatureUpsert {
	u.Set(tokentransactionpeersignature.FieldUpdateTime, v)
	return u
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsert) UpdateUpdateTime() *TokenTransactionPeerSignatureUpsert {
	u.SetExcluded(tokentransactionpeersignature.FieldUpdateTime)
	return u
}

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (u *TokenTransactionPeerSignatureUpsert) SetOperatorIdentityPublicKey(v keys.Public) *TokenTransactionPeerSignatureUpsert {
	u.Set(tokentransactionpeersignature.FieldOperatorIdentityPublicKey, v)
	return u
}

// UpdateOperatorIdentityPublicKey sets the "operator_identity_public_key" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsert) UpdateOperatorIdentityPublicKey() *TokenTransactionPeerSignatureUpsert {
	u.SetExcluded(tokentransactionpeersignature.FieldOperatorIdentityPublicKey)
	return u
}

// SetSignature sets the "signature" field.
func (u *TokenTransactionPeerSignatureUpsert) SetSignature(v []byte) *TokenTransactionPeerSignatureUpsert {
	u.Set(tokentransactionpeersignature.FieldSignature, v)
	return u
}

// UpdateSignature sets the "signature" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsert) UpdateSignature() *TokenTransactionPeerSignatureUpsert {
	u.SetExcluded(tokentransactionpeersignature.FieldSignature)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokentransactionpeersignature.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenTransactionPeerSignatureUpsertOne) UpdateNewValues() *TokenTransactionPeerSignatureUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(tokentransactionpeersignature.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(tokentransactionpeersignature.FieldCreateTime)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *TokenTransactionPeerSignatureUpsertOne) Ignore() *TokenTransactionPeerSignatureUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TokenTransactionPeerSignatureUpsertOne) DoNothing() *TokenTransactionPeerSignatureUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the TokenTransactionPeerSignatureCreate.OnConflict
// documentation for more info.
func (u *TokenTransactionPeerSignatureUpsertOne) Update(set func(*TokenTransactionPeerSignatureUpsert)) *TokenTransactionPeerSignatureUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&TokenTransactionPeerSignatureUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdateTime sets the "update_time" field.
func (u *TokenTransactionPeerSignatureUpsertOne) SetUpdateTime(v time.Time) *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertOne) UpdateUpdateTime() *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (u *TokenTransactionPeerSignatureUpsertOne) SetOperatorIdentityPublicKey(v keys.Public) *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetOperatorIdentityPublicKey(v)
	})
}

// UpdateOperatorIdentityPublicKey sets the "operator_identity_public_key" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertOne) UpdateOperatorIdentityPublicKey() *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateOperatorIdentityPublicKey()
	})
}

// SetSignature sets the "signature" field.
func (u *TokenTransactionPeerSignatureUpsertOne) SetSignature(v []byte) *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetSignature(v)
	})
}

// UpdateSignature sets the "signature" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertOne) UpdateSignature() *TokenTransactionPeerSignatureUpsertOne {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateSignature()
	})
}

// Exec executes the query.
func (u *TokenTransactionPeerSignatureUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TokenTransactionPeerSignatureCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *TokenTransactionPeerSignatureUpsertOne) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *TokenTransactionPeerSignatureUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: TokenTransactionPeerSignatureUpsertOne.ID is not supported by MySQL driver. Use TokenTransactionPeerSignatureUpsertOne.Exec instead")
	}
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *TokenTransactionPeerSignatureUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// TokenTransactionPeerSignatureCreateBulk is the builder for creating many TokenTransactionPeerSignature entities in bulk.
type TokenTransactionPeerSignatureCreateBulk struct {
	config
	err      error
	builders []*TokenTransactionPeerSignatureCreate
	conflict []sql.ConflictOption
}

// Save creates the TokenTransactionPeerSignature entities in the database.
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) Save(ctx context.Context) ([]*TokenTransactionPeerSignature, error) {
	if ttpscb.err != nil {
		return nil, ttpscb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(ttpscb.builders))
	nodes := make([]*TokenTransactionPeerSignature, len(ttpscb.builders))
	mutators := make([]Mutator, len(ttpscb.builders))
	for i := range ttpscb.builders {
		func(i int, root context.Context) {
			builder := ttpscb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*TokenTransactionPeerSignatureMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, ttpscb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = ttpscb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, ttpscb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, ttpscb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) SaveX(ctx context.Context) []*TokenTransactionPeerSignature {
	v, err := ttpscb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) Exec(ctx context.Context) error {
	_, err := ttpscb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) ExecX(ctx context.Context) {
	if err := ttpscb.Exec(ctx); err != nil {
		panic(err)
	}
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.TokenTransactionPeerSignature.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.TokenTransactionPeerSignatureUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) OnConflict(opts ...sql.ConflictOption) *TokenTransactionPeerSignatureUpsertBulk {
	ttpscb.conflict = opts
	return &TokenTransactionPeerSignatureUpsertBulk{
		create: ttpscb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (ttpscb *TokenTransactionPeerSignatureCreateBulk) OnConflictColumns(columns ...string) *TokenTransactionPeerSignatureUpsertBulk {
	ttpscb.conflict = append(ttpscb.conflict, sql.ConflictColumns(columns...))
	return &TokenTransactionPeerSignatureUpsertBulk{
		create: ttpscb,
	}
}

// TokenTransactionPeerSignatureUpsertBulk is the builder for "upsert"-ing
// a bulk of TokenTransactionPeerSignature nodes.
type TokenTransactionPeerSignatureUpsertBulk struct {
	create *TokenTransactionPeerSignatureCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokentransactionpeersignature.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenTransactionPeerSignatureUpsertBulk) UpdateNewValues() *TokenTransactionPeerSignatureUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(tokentransactionpeersignature.FieldID)
			}
			if _, exists := b.mutation.CreateTime(); exists {
				s.SetIgnore(tokentransactionpeersignature.FieldCreateTime)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.TokenTransactionPeerSignature.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *TokenTransactionPeerSignatureUpsertBulk) Ignore() *TokenTransactionPeerSignatureUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TokenTransactionPeerSignatureUpsertBulk) DoNothing() *TokenTransactionPeerSignatureUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the TokenTransactionPeerSignatureCreateBulk.OnConflict
// documentation for more info.
func (u *TokenTransactionPeerSignatureUpsertBulk) Update(set func(*TokenTransactionPeerSignatureUpsert)) *TokenTransactionPeerSignatureUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&TokenTransactionPeerSignatureUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdateTime sets the "update_time" field.
func (u *TokenTransactionPeerSignatureUpsertBulk) SetUpdateTime(v time.Time) *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertBulk) UpdateUpdateTime() *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (u *TokenTransactionPeerSignatureUpsertBulk) SetOperatorIdentityPublicKey(v keys.Public) *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetOperatorIdentityPublicKey(v)
	})
}

// UpdateOperatorIdentityPublicKey sets the "operator_identity_public_key" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertBulk) UpdateOperatorIdentityPublicKey() *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateOperatorIdentityPublicKey()
	})
}

// SetSignature sets the "signature" field.
func (u *TokenTransactionPeerSignatureUpsertBulk) SetSignature(v []byte) *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.SetSignature(v)
	})
}

// UpdateSignature sets the "signature" field to the value that was provided on create.
func (u *TokenTransactionPeerSignatureUpsertBulk) UpdateSignature() *TokenTransactionPeerSignatureUpsertBulk {
	return u.Update(func(s *TokenTransactionPeerSignatureUpsert) {
		s.UpdateSignature()
	})
}

// Exec executes the query.
func (u *TokenTransactionPeerSignatureUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the TokenTransactionPeerSignatureCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TokenTransactionPeerSignatureCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *TokenTransactionPeerSignatureUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
