// 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/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/signingkeyshare"
	"github.com/lightsparkdev/spark/so/ent/tokencreate"
	"github.com/lightsparkdev/spark/so/ent/tokenoutput"
	"github.com/lightsparkdev/spark/so/ent/tokenpartialrevocationsecretshare"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
)

// TokenOutputCreate is the builder for creating a TokenOutput entity.
type TokenOutputCreate struct {
	config
	mutation *TokenOutputMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (toc *TokenOutputCreate) SetStatus(sos schematype.TokenOutputStatus) *TokenOutputCreate {
	toc.mutation.SetStatus(sos)
	return toc
}

// SetOwnerPublicKey sets the "owner_public_key" field.
func (toc *TokenOutputCreate) SetOwnerPublicKey(k keys.Public) *TokenOutputCreate {
	toc.mutation.SetOwnerPublicKey(k)
	return toc
}

// SetWithdrawBondSats sets the "withdraw_bond_sats" field.
func (toc *TokenOutputCreate) SetWithdrawBondSats(u uint64) *TokenOutputCreate {
	toc.mutation.SetWithdrawBondSats(u)
	return toc
}

// SetWithdrawRelativeBlockLocktime sets the "withdraw_relative_block_locktime" field.
func (toc *TokenOutputCreate) SetWithdrawRelativeBlockLocktime(u uint64) *TokenOutputCreate {
	toc.mutation.SetWithdrawRelativeBlockLocktime(u)
	return toc
}

// SetWithdrawRevocationCommitment sets the "withdraw_revocation_commitment" field.
func (toc *TokenOutputCreate) SetWithdrawRevocationCommitment(b []byte) *TokenOutputCreate {
	toc.mutation.SetWithdrawRevocationCommitment(b)
	return toc
}

// SetTokenPublicKey sets the "token_public_key" field.
func (toc *TokenOutputCreate) SetTokenPublicKey(k keys.Public) *TokenOutputCreate {
	toc.mutation.SetTokenPublicKey(k)
	return toc
}

// SetNillableTokenPublicKey sets the "token_public_key" field if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableTokenPublicKey(k *keys.Public) *TokenOutputCreate {
	if k != nil {
		toc.SetTokenPublicKey(*k)
	}
	return toc
}

// SetTokenAmount sets the "token_amount" field.
func (toc *TokenOutputCreate) SetTokenAmount(b []byte) *TokenOutputCreate {
	toc.mutation.SetTokenAmount(b)
	return toc
}

// SetCreatedTransactionOutputVout sets the "created_transaction_output_vout" field.
func (toc *TokenOutputCreate) SetCreatedTransactionOutputVout(i int32) *TokenOutputCreate {
	toc.mutation.SetCreatedTransactionOutputVout(i)
	return toc
}

// SetSpentOwnershipSignature sets the "spent_ownership_signature" field.
func (toc *TokenOutputCreate) SetSpentOwnershipSignature(b []byte) *TokenOutputCreate {
	toc.mutation.SetSpentOwnershipSignature(b)
	return toc
}

// SetSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field.
func (toc *TokenOutputCreate) SetSpentOperatorSpecificOwnershipSignature(b []byte) *TokenOutputCreate {
	toc.mutation.SetSpentOperatorSpecificOwnershipSignature(b)
	return toc
}

// SetSpentTransactionInputVout sets the "spent_transaction_input_vout" field.
func (toc *TokenOutputCreate) SetSpentTransactionInputVout(i int32) *TokenOutputCreate {
	toc.mutation.SetSpentTransactionInputVout(i)
	return toc
}

// SetNillableSpentTransactionInputVout sets the "spent_transaction_input_vout" field if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableSpentTransactionInputVout(i *int32) *TokenOutputCreate {
	if i != nil {
		toc.SetSpentTransactionInputVout(*i)
	}
	return toc
}

// SetSpentRevocationSecret sets the "spent_revocation_secret" field.
func (toc *TokenOutputCreate) SetSpentRevocationSecret(k keys.Private) *TokenOutputCreate {
	toc.mutation.SetSpentRevocationSecret(k)
	return toc
}

// SetNillableSpentRevocationSecret sets the "spent_revocation_secret" field if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableSpentRevocationSecret(k *keys.Private) *TokenOutputCreate {
	if k != nil {
		toc.SetSpentRevocationSecret(*k)
	}
	return toc
}

// SetConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field.
func (toc *TokenOutputCreate) SetConfirmedWithdrawBlockHash(b []byte) *TokenOutputCreate {
	toc.mutation.SetConfirmedWithdrawBlockHash(b)
	return toc
}

// SetNetwork sets the "network" field.
func (toc *TokenOutputCreate) SetNetwork(s schematype.Network) *TokenOutputCreate {
	toc.mutation.SetNetwork(s)
	return toc
}

// SetNillableNetwork sets the "network" field if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableNetwork(s *schematype.Network) *TokenOutputCreate {
	if s != nil {
		toc.SetNetwork(*s)
	}
	return toc
}

// SetTokenIdentifier sets the "token_identifier" field.
func (toc *TokenOutputCreate) SetTokenIdentifier(b []byte) *TokenOutputCreate {
	toc.mutation.SetTokenIdentifier(b)
	return toc
}

// SetTokenCreateID sets the "token_create_id" field.
func (toc *TokenOutputCreate) SetTokenCreateID(u uuid.UUID) *TokenOutputCreate {
	toc.mutation.SetTokenCreateID(u)
	return toc
}

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

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

// SetRevocationKeyshareID sets the "revocation_keyshare" edge to the SigningKeyshare entity by ID.
func (toc *TokenOutputCreate) SetRevocationKeyshareID(id uuid.UUID) *TokenOutputCreate {
	toc.mutation.SetRevocationKeyshareID(id)
	return toc
}

// SetRevocationKeyshare sets the "revocation_keyshare" edge to the SigningKeyshare entity.
func (toc *TokenOutputCreate) SetRevocationKeyshare(s *SigningKeyshare) *TokenOutputCreate {
	return toc.SetRevocationKeyshareID(s.ID)
}

// SetOutputCreatedTokenTransactionID sets the "output_created_token_transaction" edge to the TokenTransaction entity by ID.
func (toc *TokenOutputCreate) SetOutputCreatedTokenTransactionID(id uuid.UUID) *TokenOutputCreate {
	toc.mutation.SetOutputCreatedTokenTransactionID(id)
	return toc
}

// SetNillableOutputCreatedTokenTransactionID sets the "output_created_token_transaction" edge to the TokenTransaction entity by ID if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableOutputCreatedTokenTransactionID(id *uuid.UUID) *TokenOutputCreate {
	if id != nil {
		toc = toc.SetOutputCreatedTokenTransactionID(*id)
	}
	return toc
}

// SetOutputCreatedTokenTransaction sets the "output_created_token_transaction" edge to the TokenTransaction entity.
func (toc *TokenOutputCreate) SetOutputCreatedTokenTransaction(t *TokenTransaction) *TokenOutputCreate {
	return toc.SetOutputCreatedTokenTransactionID(t.ID)
}

// SetOutputSpentTokenTransactionID sets the "output_spent_token_transaction" edge to the TokenTransaction entity by ID.
func (toc *TokenOutputCreate) SetOutputSpentTokenTransactionID(id uuid.UUID) *TokenOutputCreate {
	toc.mutation.SetOutputSpentTokenTransactionID(id)
	return toc
}

// SetNillableOutputSpentTokenTransactionID sets the "output_spent_token_transaction" edge to the TokenTransaction entity by ID if the given value is not nil.
func (toc *TokenOutputCreate) SetNillableOutputSpentTokenTransactionID(id *uuid.UUID) *TokenOutputCreate {
	if id != nil {
		toc = toc.SetOutputSpentTokenTransactionID(*id)
	}
	return toc
}

// SetOutputSpentTokenTransaction sets the "output_spent_token_transaction" edge to the TokenTransaction entity.
func (toc *TokenOutputCreate) SetOutputSpentTokenTransaction(t *TokenTransaction) *TokenOutputCreate {
	return toc.SetOutputSpentTokenTransactionID(t.ID)
}

// AddOutputSpentStartedTokenTransactionIDs adds the "output_spent_started_token_transactions" edge to the TokenTransaction entity by IDs.
func (toc *TokenOutputCreate) AddOutputSpentStartedTokenTransactionIDs(ids ...uuid.UUID) *TokenOutputCreate {
	toc.mutation.AddOutputSpentStartedTokenTransactionIDs(ids...)
	return toc
}

// AddOutputSpentStartedTokenTransactions adds the "output_spent_started_token_transactions" edges to the TokenTransaction entity.
func (toc *TokenOutputCreate) AddOutputSpentStartedTokenTransactions(t ...*TokenTransaction) *TokenOutputCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return toc.AddOutputSpentStartedTokenTransactionIDs(ids...)
}

// AddTokenPartialRevocationSecretShareIDs adds the "token_partial_revocation_secret_shares" edge to the TokenPartialRevocationSecretShare entity by IDs.
func (toc *TokenOutputCreate) AddTokenPartialRevocationSecretShareIDs(ids ...uuid.UUID) *TokenOutputCreate {
	toc.mutation.AddTokenPartialRevocationSecretShareIDs(ids...)
	return toc
}

// AddTokenPartialRevocationSecretShares adds the "token_partial_revocation_secret_shares" edges to the TokenPartialRevocationSecretShare entity.
func (toc *TokenOutputCreate) AddTokenPartialRevocationSecretShares(t ...*TokenPartialRevocationSecretShare) *TokenOutputCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return toc.AddTokenPartialRevocationSecretShareIDs(ids...)
}

// SetTokenCreate sets the "token_create" edge to the TokenCreate entity.
func (toc *TokenOutputCreate) SetTokenCreate(t *TokenCreate) *TokenOutputCreate {
	return toc.SetTokenCreateID(t.ID)
}

// Mutation returns the TokenOutputMutation object of the builder.
func (toc *TokenOutputCreate) Mutation() *TokenOutputMutation {
	return toc.mutation
}

// Save creates the TokenOutput in the database.
func (toc *TokenOutputCreate) Save(ctx context.Context) (*TokenOutput, error) {
	if err := toc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, toc.sqlSave, toc.mutation, toc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (toc *TokenOutputCreate) defaults() error {
	if _, ok := toc.mutation.CreateTime(); !ok {
		if tokenoutput.DefaultCreateTime == nil {
			return fmt.Errorf("ent: uninitialized tokenoutput.DefaultCreateTime (forgotten import ent/runtime?)")
		}
		v := tokenoutput.DefaultCreateTime()
		toc.mutation.SetCreateTime(v)
	}
	if _, ok := toc.mutation.UpdateTime(); !ok {
		if tokenoutput.DefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized tokenoutput.DefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := tokenoutput.DefaultUpdateTime()
		toc.mutation.SetUpdateTime(v)
	}
	if _, ok := toc.mutation.ID(); !ok {
		if tokenoutput.DefaultID == nil {
			return fmt.Errorf("ent: uninitialized tokenoutput.DefaultID (forgotten import ent/runtime?)")
		}
		v := tokenoutput.DefaultID()
		toc.mutation.SetID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (toc *TokenOutputCreate) check() error {
	if _, ok := toc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "TokenOutput.create_time"`)}
	}
	if _, ok := toc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "TokenOutput.update_time"`)}
	}
	if _, ok := toc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "TokenOutput.status"`)}
	}
	if v, ok := toc.mutation.Status(); ok {
		if err := tokenoutput.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TokenOutput.status": %w`, err)}
		}
	}
	if _, ok := toc.mutation.OwnerPublicKey(); !ok {
		return &ValidationError{Name: "owner_public_key", err: errors.New(`ent: missing required field "TokenOutput.owner_public_key"`)}
	}
	if _, ok := toc.mutation.WithdrawBondSats(); !ok {
		return &ValidationError{Name: "withdraw_bond_sats", err: errors.New(`ent: missing required field "TokenOutput.withdraw_bond_sats"`)}
	}
	if _, ok := toc.mutation.WithdrawRelativeBlockLocktime(); !ok {
		return &ValidationError{Name: "withdraw_relative_block_locktime", err: errors.New(`ent: missing required field "TokenOutput.withdraw_relative_block_locktime"`)}
	}
	if _, ok := toc.mutation.WithdrawRevocationCommitment(); !ok {
		return &ValidationError{Name: "withdraw_revocation_commitment", err: errors.New(`ent: missing required field "TokenOutput.withdraw_revocation_commitment"`)}
	}
	if _, ok := toc.mutation.TokenAmount(); !ok {
		return &ValidationError{Name: "token_amount", err: errors.New(`ent: missing required field "TokenOutput.token_amount"`)}
	}
	if v, ok := toc.mutation.TokenAmount(); ok {
		if err := tokenoutput.TokenAmountValidator(v); err != nil {
			return &ValidationError{Name: "token_amount", err: fmt.Errorf(`ent: validator failed for field "TokenOutput.token_amount": %w`, err)}
		}
	}
	if _, ok := toc.mutation.CreatedTransactionOutputVout(); !ok {
		return &ValidationError{Name: "created_transaction_output_vout", err: errors.New(`ent: missing required field "TokenOutput.created_transaction_output_vout"`)}
	}
	if v, ok := toc.mutation.Network(); ok {
		if err := tokenoutput.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "TokenOutput.network": %w`, err)}
		}
	}
	if _, ok := toc.mutation.TokenIdentifier(); !ok {
		return &ValidationError{Name: "token_identifier", err: errors.New(`ent: missing required field "TokenOutput.token_identifier"`)}
	}
	if _, ok := toc.mutation.TokenCreateID(); !ok {
		return &ValidationError{Name: "token_create_id", err: errors.New(`ent: missing required field "TokenOutput.token_create_id"`)}
	}
	if len(toc.mutation.RevocationKeyshareIDs()) == 0 {
		return &ValidationError{Name: "revocation_keyshare", err: errors.New(`ent: missing required edge "TokenOutput.revocation_keyshare"`)}
	}
	if len(toc.mutation.TokenCreateIDs()) == 0 {
		return &ValidationError{Name: "token_create", err: errors.New(`ent: missing required edge "TokenOutput.token_create"`)}
	}
	return nil
}

func (toc *TokenOutputCreate) sqlSave(ctx context.Context) (*TokenOutput, error) {
	if err := toc.check(); err != nil {
		return nil, err
	}
	_node, _spec := toc.createSpec()
	if err := sqlgraph.CreateNode(ctx, toc.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
		}
	}
	toc.mutation.id = &_node.ID
	toc.mutation.done = true
	return _node, nil
}

func (toc *TokenOutputCreate) createSpec() (*TokenOutput, *sqlgraph.CreateSpec) {
	var (
		_node = &TokenOutput{config: toc.config}
		_spec = sqlgraph.NewCreateSpec(tokenoutput.Table, sqlgraph.NewFieldSpec(tokenoutput.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = toc.conflict
	if id, ok := toc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := toc.mutation.CreateTime(); ok {
		_spec.SetField(tokenoutput.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := toc.mutation.UpdateTime(); ok {
		_spec.SetField(tokenoutput.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := toc.mutation.Status(); ok {
		_spec.SetField(tokenoutput.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := toc.mutation.OwnerPublicKey(); ok {
		_spec.SetField(tokenoutput.FieldOwnerPublicKey, field.TypeBytes, value)
		_node.OwnerPublicKey = value
	}
	if value, ok := toc.mutation.WithdrawBondSats(); ok {
		_spec.SetField(tokenoutput.FieldWithdrawBondSats, field.TypeUint64, value)
		_node.WithdrawBondSats = value
	}
	if value, ok := toc.mutation.WithdrawRelativeBlockLocktime(); ok {
		_spec.SetField(tokenoutput.FieldWithdrawRelativeBlockLocktime, field.TypeUint64, value)
		_node.WithdrawRelativeBlockLocktime = value
	}
	if value, ok := toc.mutation.WithdrawRevocationCommitment(); ok {
		_spec.SetField(tokenoutput.FieldWithdrawRevocationCommitment, field.TypeBytes, value)
		_node.WithdrawRevocationCommitment = value
	}
	if value, ok := toc.mutation.TokenPublicKey(); ok {
		_spec.SetField(tokenoutput.FieldTokenPublicKey, field.TypeBytes, value)
		_node.TokenPublicKey = value
	}
	if value, ok := toc.mutation.TokenAmount(); ok {
		_spec.SetField(tokenoutput.FieldTokenAmount, field.TypeBytes, value)
		_node.TokenAmount = value
	}
	if value, ok := toc.mutation.CreatedTransactionOutputVout(); ok {
		_spec.SetField(tokenoutput.FieldCreatedTransactionOutputVout, field.TypeInt32, value)
		_node.CreatedTransactionOutputVout = value
	}
	if value, ok := toc.mutation.SpentOwnershipSignature(); ok {
		_spec.SetField(tokenoutput.FieldSpentOwnershipSignature, field.TypeBytes, value)
		_node.SpentOwnershipSignature = value
	}
	if value, ok := toc.mutation.SpentOperatorSpecificOwnershipSignature(); ok {
		_spec.SetField(tokenoutput.FieldSpentOperatorSpecificOwnershipSignature, field.TypeBytes, value)
		_node.SpentOperatorSpecificOwnershipSignature = value
	}
	if value, ok := toc.mutation.SpentTransactionInputVout(); ok {
		_spec.SetField(tokenoutput.FieldSpentTransactionInputVout, field.TypeInt32, value)
		_node.SpentTransactionInputVout = value
	}
	if value, ok := toc.mutation.SpentRevocationSecret(); ok {
		_spec.SetField(tokenoutput.FieldSpentRevocationSecret, field.TypeBytes, value)
		_node.SpentRevocationSecret = value
	}
	if value, ok := toc.mutation.ConfirmedWithdrawBlockHash(); ok {
		_spec.SetField(tokenoutput.FieldConfirmedWithdrawBlockHash, field.TypeBytes, value)
		_node.ConfirmedWithdrawBlockHash = value
	}
	if value, ok := toc.mutation.Network(); ok {
		_spec.SetField(tokenoutput.FieldNetwork, field.TypeEnum, value)
		_node.Network = value
	}
	if value, ok := toc.mutation.TokenIdentifier(); ok {
		_spec.SetField(tokenoutput.FieldTokenIdentifier, field.TypeBytes, value)
		_node.TokenIdentifier = value
	}
	if nodes := toc.mutation.RevocationKeyshareIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tokenoutput.RevocationKeyshareTable,
			Columns: []string{tokenoutput.RevocationKeyshareColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.token_output_revocation_keyshare = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := toc.mutation.OutputCreatedTokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tokenoutput.OutputCreatedTokenTransactionTable,
			Columns: []string{tokenoutput.OutputCreatedTokenTransactionColumn},
			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_output_output_created_token_transaction = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := toc.mutation.OutputSpentTokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tokenoutput.OutputSpentTokenTransactionTable,
			Columns: []string{tokenoutput.OutputSpentTokenTransactionColumn},
			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_output_output_spent_token_transaction = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := toc.mutation.OutputSpentStartedTokenTransactionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   tokenoutput.OutputSpentStartedTokenTransactionsTable,
			Columns: tokenoutput.OutputSpentStartedTokenTransactionsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := toc.mutation.TokenPartialRevocationSecretSharesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   tokenoutput.TokenPartialRevocationSecretSharesTable,
			Columns: []string{tokenoutput.TokenPartialRevocationSecretSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokenpartialrevocationsecretshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := toc.mutation.TokenCreateIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   tokenoutput.TokenCreateTable,
			Columns: []string{tokenoutput.TokenCreateColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokencreate.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.TokenCreateID = 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.TokenOutput.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.TokenOutputUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (toc *TokenOutputCreate) OnConflict(opts ...sql.ConflictOption) *TokenOutputUpsertOne {
	toc.conflict = opts
	return &TokenOutputUpsertOne{
		create: toc,
	}
}

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

type (
	// TokenOutputUpsertOne is the builder for "upsert"-ing
	//  one TokenOutput node.
	TokenOutputUpsertOne struct {
		create *TokenOutputCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *TokenOutputUpsert) SetStatus(v schematype.TokenOutputStatus) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateStatus() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldStatus)
	return u
}

// SetSpentOwnershipSignature sets the "spent_ownership_signature" field.
func (u *TokenOutputUpsert) SetSpentOwnershipSignature(v []byte) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldSpentOwnershipSignature, v)
	return u
}

// UpdateSpentOwnershipSignature sets the "spent_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateSpentOwnershipSignature() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldSpentOwnershipSignature)
	return u
}

// ClearSpentOwnershipSignature clears the value of the "spent_ownership_signature" field.
func (u *TokenOutputUpsert) ClearSpentOwnershipSignature() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldSpentOwnershipSignature)
	return u
}

// SetSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsert) SetSpentOperatorSpecificOwnershipSignature(v []byte) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldSpentOperatorSpecificOwnershipSignature, v)
	return u
}

// UpdateSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldSpentOperatorSpecificOwnershipSignature)
	return u
}

// ClearSpentOperatorSpecificOwnershipSignature clears the value of the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsert) ClearSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldSpentOperatorSpecificOwnershipSignature)
	return u
}

// SetSpentTransactionInputVout sets the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsert) SetSpentTransactionInputVout(v int32) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldSpentTransactionInputVout, v)
	return u
}

// UpdateSpentTransactionInputVout sets the "spent_transaction_input_vout" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateSpentTransactionInputVout() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldSpentTransactionInputVout)
	return u
}

// AddSpentTransactionInputVout adds v to the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsert) AddSpentTransactionInputVout(v int32) *TokenOutputUpsert {
	u.Add(tokenoutput.FieldSpentTransactionInputVout, v)
	return u
}

// ClearSpentTransactionInputVout clears the value of the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsert) ClearSpentTransactionInputVout() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldSpentTransactionInputVout)
	return u
}

// SetSpentRevocationSecret sets the "spent_revocation_secret" field.
func (u *TokenOutputUpsert) SetSpentRevocationSecret(v keys.Private) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldSpentRevocationSecret, v)
	return u
}

// UpdateSpentRevocationSecret sets the "spent_revocation_secret" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateSpentRevocationSecret() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldSpentRevocationSecret)
	return u
}

// ClearSpentRevocationSecret clears the value of the "spent_revocation_secret" field.
func (u *TokenOutputUpsert) ClearSpentRevocationSecret() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldSpentRevocationSecret)
	return u
}

// SetConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsert) SetConfirmedWithdrawBlockHash(v []byte) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldConfirmedWithdrawBlockHash, v)
	return u
}

// UpdateConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateConfirmedWithdrawBlockHash() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldConfirmedWithdrawBlockHash)
	return u
}

// ClearConfirmedWithdrawBlockHash clears the value of the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsert) ClearConfirmedWithdrawBlockHash() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldConfirmedWithdrawBlockHash)
	return u
}

// SetNetwork sets the "network" field.
func (u *TokenOutputUpsert) SetNetwork(v schematype.Network) *TokenOutputUpsert {
	u.Set(tokenoutput.FieldNetwork, v)
	return u
}

// UpdateNetwork sets the "network" field to the value that was provided on create.
func (u *TokenOutputUpsert) UpdateNetwork() *TokenOutputUpsert {
	u.SetExcluded(tokenoutput.FieldNetwork)
	return u
}

// ClearNetwork clears the value of the "network" field.
func (u *TokenOutputUpsert) ClearNetwork() *TokenOutputUpsert {
	u.SetNull(tokenoutput.FieldNetwork)
	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.TokenOutput.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokenoutput.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenOutputUpsertOne) UpdateNewValues() *TokenOutputUpsertOne {
	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(tokenoutput.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(tokenoutput.FieldCreateTime)
		}
		if _, exists := u.create.mutation.OwnerPublicKey(); exists {
			s.SetIgnore(tokenoutput.FieldOwnerPublicKey)
		}
		if _, exists := u.create.mutation.WithdrawBondSats(); exists {
			s.SetIgnore(tokenoutput.FieldWithdrawBondSats)
		}
		if _, exists := u.create.mutation.WithdrawRelativeBlockLocktime(); exists {
			s.SetIgnore(tokenoutput.FieldWithdrawRelativeBlockLocktime)
		}
		if _, exists := u.create.mutation.WithdrawRevocationCommitment(); exists {
			s.SetIgnore(tokenoutput.FieldWithdrawRevocationCommitment)
		}
		if _, exists := u.create.mutation.TokenPublicKey(); exists {
			s.SetIgnore(tokenoutput.FieldTokenPublicKey)
		}
		if _, exists := u.create.mutation.TokenAmount(); exists {
			s.SetIgnore(tokenoutput.FieldTokenAmount)
		}
		if _, exists := u.create.mutation.CreatedTransactionOutputVout(); exists {
			s.SetIgnore(tokenoutput.FieldCreatedTransactionOutputVout)
		}
		if _, exists := u.create.mutation.TokenIdentifier(); exists {
			s.SetIgnore(tokenoutput.FieldTokenIdentifier)
		}
		if _, exists := u.create.mutation.TokenCreateID(); exists {
			s.SetIgnore(tokenoutput.FieldTokenCreateID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.TokenOutput.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *TokenOutputUpsertOne) Ignore() *TokenOutputUpsertOne {
	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 *TokenOutputUpsertOne) DoNothing() *TokenOutputUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetStatus sets the "status" field.
func (u *TokenOutputUpsertOne) SetStatus(v schematype.TokenOutputStatus) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateStatus() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateStatus()
	})
}

// SetSpentOwnershipSignature sets the "spent_ownership_signature" field.
func (u *TokenOutputUpsertOne) SetSpentOwnershipSignature(v []byte) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentOwnershipSignature(v)
	})
}

// UpdateSpentOwnershipSignature sets the "spent_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateSpentOwnershipSignature() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentOwnershipSignature()
	})
}

// ClearSpentOwnershipSignature clears the value of the "spent_ownership_signature" field.
func (u *TokenOutputUpsertOne) ClearSpentOwnershipSignature() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentOwnershipSignature()
	})
}

// SetSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsertOne) SetSpentOperatorSpecificOwnershipSignature(v []byte) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentOperatorSpecificOwnershipSignature(v)
	})
}

// UpdateSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentOperatorSpecificOwnershipSignature()
	})
}

// ClearSpentOperatorSpecificOwnershipSignature clears the value of the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsertOne) ClearSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentOperatorSpecificOwnershipSignature()
	})
}

// SetSpentTransactionInputVout sets the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertOne) SetSpentTransactionInputVout(v int32) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentTransactionInputVout(v)
	})
}

// AddSpentTransactionInputVout adds v to the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertOne) AddSpentTransactionInputVout(v int32) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.AddSpentTransactionInputVout(v)
	})
}

// UpdateSpentTransactionInputVout sets the "spent_transaction_input_vout" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateSpentTransactionInputVout() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentTransactionInputVout()
	})
}

// ClearSpentTransactionInputVout clears the value of the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertOne) ClearSpentTransactionInputVout() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentTransactionInputVout()
	})
}

// SetSpentRevocationSecret sets the "spent_revocation_secret" field.
func (u *TokenOutputUpsertOne) SetSpentRevocationSecret(v keys.Private) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentRevocationSecret(v)
	})
}

// UpdateSpentRevocationSecret sets the "spent_revocation_secret" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateSpentRevocationSecret() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentRevocationSecret()
	})
}

// ClearSpentRevocationSecret clears the value of the "spent_revocation_secret" field.
func (u *TokenOutputUpsertOne) ClearSpentRevocationSecret() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentRevocationSecret()
	})
}

// SetConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsertOne) SetConfirmedWithdrawBlockHash(v []byte) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetConfirmedWithdrawBlockHash(v)
	})
}

// UpdateConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateConfirmedWithdrawBlockHash() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateConfirmedWithdrawBlockHash()
	})
}

// ClearConfirmedWithdrawBlockHash clears the value of the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsertOne) ClearConfirmedWithdrawBlockHash() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearConfirmedWithdrawBlockHash()
	})
}

// SetNetwork sets the "network" field.
func (u *TokenOutputUpsertOne) SetNetwork(v schematype.Network) *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetNetwork(v)
	})
}

// UpdateNetwork sets the "network" field to the value that was provided on create.
func (u *TokenOutputUpsertOne) UpdateNetwork() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateNetwork()
	})
}

// ClearNetwork clears the value of the "network" field.
func (u *TokenOutputUpsertOne) ClearNetwork() *TokenOutputUpsertOne {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearNetwork()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *TokenOutputUpsertOne) 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 *TokenOutputUpsertOne) 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: TokenOutputUpsertOne.ID is not supported by MySQL driver. Use TokenOutputUpsertOne.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 *TokenOutputUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the TokenOutput entities in the database.
func (tocb *TokenOutputCreateBulk) Save(ctx context.Context) ([]*TokenOutput, error) {
	if tocb.err != nil {
		return nil, tocb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(tocb.builders))
	nodes := make([]*TokenOutput, len(tocb.builders))
	mutators := make([]Mutator, len(tocb.builders))
	for i := range tocb.builders {
		func(i int, root context.Context) {
			builder := tocb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*TokenOutputMutation)
				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, tocb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = tocb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, tocb.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, tocb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.TokenOutput.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.TokenOutputUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (tocb *TokenOutputCreateBulk) OnConflict(opts ...sql.ConflictOption) *TokenOutputUpsertBulk {
	tocb.conflict = opts
	return &TokenOutputUpsertBulk{
		create: tocb,
	}
}

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

// TokenOutputUpsertBulk is the builder for "upsert"-ing
// a bulk of TokenOutput nodes.
type TokenOutputUpsertBulk struct {
	create *TokenOutputCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.TokenOutput.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokenoutput.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenOutputUpsertBulk) UpdateNewValues() *TokenOutputUpsertBulk {
	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(tokenoutput.FieldID)
			}
			if _, exists := b.mutation.CreateTime(); exists {
				s.SetIgnore(tokenoutput.FieldCreateTime)
			}
			if _, exists := b.mutation.OwnerPublicKey(); exists {
				s.SetIgnore(tokenoutput.FieldOwnerPublicKey)
			}
			if _, exists := b.mutation.WithdrawBondSats(); exists {
				s.SetIgnore(tokenoutput.FieldWithdrawBondSats)
			}
			if _, exists := b.mutation.WithdrawRelativeBlockLocktime(); exists {
				s.SetIgnore(tokenoutput.FieldWithdrawRelativeBlockLocktime)
			}
			if _, exists := b.mutation.WithdrawRevocationCommitment(); exists {
				s.SetIgnore(tokenoutput.FieldWithdrawRevocationCommitment)
			}
			if _, exists := b.mutation.TokenPublicKey(); exists {
				s.SetIgnore(tokenoutput.FieldTokenPublicKey)
			}
			if _, exists := b.mutation.TokenAmount(); exists {
				s.SetIgnore(tokenoutput.FieldTokenAmount)
			}
			if _, exists := b.mutation.CreatedTransactionOutputVout(); exists {
				s.SetIgnore(tokenoutput.FieldCreatedTransactionOutputVout)
			}
			if _, exists := b.mutation.TokenIdentifier(); exists {
				s.SetIgnore(tokenoutput.FieldTokenIdentifier)
			}
			if _, exists := b.mutation.TokenCreateID(); exists {
				s.SetIgnore(tokenoutput.FieldTokenCreateID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.TokenOutput.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *TokenOutputUpsertBulk) Ignore() *TokenOutputUpsertBulk {
	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 *TokenOutputUpsertBulk) DoNothing() *TokenOutputUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetStatus sets the "status" field.
func (u *TokenOutputUpsertBulk) SetStatus(v schematype.TokenOutputStatus) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateStatus() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateStatus()
	})
}

// SetSpentOwnershipSignature sets the "spent_ownership_signature" field.
func (u *TokenOutputUpsertBulk) SetSpentOwnershipSignature(v []byte) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentOwnershipSignature(v)
	})
}

// UpdateSpentOwnershipSignature sets the "spent_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateSpentOwnershipSignature() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentOwnershipSignature()
	})
}

// ClearSpentOwnershipSignature clears the value of the "spent_ownership_signature" field.
func (u *TokenOutputUpsertBulk) ClearSpentOwnershipSignature() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentOwnershipSignature()
	})
}

// SetSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsertBulk) SetSpentOperatorSpecificOwnershipSignature(v []byte) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentOperatorSpecificOwnershipSignature(v)
	})
}

// UpdateSpentOperatorSpecificOwnershipSignature sets the "spent_operator_specific_ownership_signature" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentOperatorSpecificOwnershipSignature()
	})
}

// ClearSpentOperatorSpecificOwnershipSignature clears the value of the "spent_operator_specific_ownership_signature" field.
func (u *TokenOutputUpsertBulk) ClearSpentOperatorSpecificOwnershipSignature() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentOperatorSpecificOwnershipSignature()
	})
}

// SetSpentTransactionInputVout sets the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertBulk) SetSpentTransactionInputVout(v int32) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentTransactionInputVout(v)
	})
}

// AddSpentTransactionInputVout adds v to the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertBulk) AddSpentTransactionInputVout(v int32) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.AddSpentTransactionInputVout(v)
	})
}

// UpdateSpentTransactionInputVout sets the "spent_transaction_input_vout" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateSpentTransactionInputVout() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentTransactionInputVout()
	})
}

// ClearSpentTransactionInputVout clears the value of the "spent_transaction_input_vout" field.
func (u *TokenOutputUpsertBulk) ClearSpentTransactionInputVout() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentTransactionInputVout()
	})
}

// SetSpentRevocationSecret sets the "spent_revocation_secret" field.
func (u *TokenOutputUpsertBulk) SetSpentRevocationSecret(v keys.Private) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetSpentRevocationSecret(v)
	})
}

// UpdateSpentRevocationSecret sets the "spent_revocation_secret" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateSpentRevocationSecret() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateSpentRevocationSecret()
	})
}

// ClearSpentRevocationSecret clears the value of the "spent_revocation_secret" field.
func (u *TokenOutputUpsertBulk) ClearSpentRevocationSecret() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearSpentRevocationSecret()
	})
}

// SetConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsertBulk) SetConfirmedWithdrawBlockHash(v []byte) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetConfirmedWithdrawBlockHash(v)
	})
}

// UpdateConfirmedWithdrawBlockHash sets the "confirmed_withdraw_block_hash" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateConfirmedWithdrawBlockHash() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateConfirmedWithdrawBlockHash()
	})
}

// ClearConfirmedWithdrawBlockHash clears the value of the "confirmed_withdraw_block_hash" field.
func (u *TokenOutputUpsertBulk) ClearConfirmedWithdrawBlockHash() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearConfirmedWithdrawBlockHash()
	})
}

// SetNetwork sets the "network" field.
func (u *TokenOutputUpsertBulk) SetNetwork(v schematype.Network) *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.SetNetwork(v)
	})
}

// UpdateNetwork sets the "network" field to the value that was provided on create.
func (u *TokenOutputUpsertBulk) UpdateNetwork() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.UpdateNetwork()
	})
}

// ClearNetwork clears the value of the "network" field.
func (u *TokenOutputUpsertBulk) ClearNetwork() *TokenOutputUpsertBulk {
	return u.Update(func(s *TokenOutputUpsert) {
		s.ClearNetwork()
	})
}

// Exec executes the query.
func (u *TokenOutputUpsertBulk) 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 TokenOutputCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TokenOutputCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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