// 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/tokencreate"
	"github.com/lightsparkdev/spark/so/ent/tokenfreeze"
)

// TokenFreezeCreate is the builder for creating a TokenFreeze entity.
type TokenFreezeCreate struct {
	config
	mutation *TokenFreezeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (tfc *TokenFreezeCreate) SetStatus(sfs schematype.TokenFreezeStatus) *TokenFreezeCreate {
	tfc.mutation.SetStatus(sfs)
	return tfc
}

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

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

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

// SetIssuerSignature sets the "issuer_signature" field.
func (tfc *TokenFreezeCreate) SetIssuerSignature(b []byte) *TokenFreezeCreate {
	tfc.mutation.SetIssuerSignature(b)
	return tfc
}

// SetWalletProvidedFreezeTimestamp sets the "wallet_provided_freeze_timestamp" field.
func (tfc *TokenFreezeCreate) SetWalletProvidedFreezeTimestamp(u uint64) *TokenFreezeCreate {
	tfc.mutation.SetWalletProvidedFreezeTimestamp(u)
	return tfc
}

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (tfc *TokenFreezeCreate) SetWalletProvidedThawTimestamp(u uint64) *TokenFreezeCreate {
	tfc.mutation.SetWalletProvidedThawTimestamp(u)
	return tfc
}

// SetNillableWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field if the given value is not nil.
func (tfc *TokenFreezeCreate) SetNillableWalletProvidedThawTimestamp(u *uint64) *TokenFreezeCreate {
	if u != nil {
		tfc.SetWalletProvidedThawTimestamp(*u)
	}
	return tfc
}

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

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

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

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

// Mutation returns the TokenFreezeMutation object of the builder.
func (tfc *TokenFreezeCreate) Mutation() *TokenFreezeMutation {
	return tfc.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tfc *TokenFreezeCreate) check() error {
	if _, ok := tfc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "TokenFreeze.create_time"`)}
	}
	if _, ok := tfc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "TokenFreeze.update_time"`)}
	}
	if _, ok := tfc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "TokenFreeze.status"`)}
	}
	if v, ok := tfc.mutation.Status(); ok {
		if err := tokenfreeze.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TokenFreeze.status": %w`, err)}
		}
	}
	if _, ok := tfc.mutation.OwnerPublicKey(); !ok {
		return &ValidationError{Name: "owner_public_key", err: errors.New(`ent: missing required field "TokenFreeze.owner_public_key"`)}
	}
	if _, ok := tfc.mutation.IssuerSignature(); !ok {
		return &ValidationError{Name: "issuer_signature", err: errors.New(`ent: missing required field "TokenFreeze.issuer_signature"`)}
	}
	if v, ok := tfc.mutation.IssuerSignature(); ok {
		if err := tokenfreeze.IssuerSignatureValidator(v); err != nil {
			return &ValidationError{Name: "issuer_signature", err: fmt.Errorf(`ent: validator failed for field "TokenFreeze.issuer_signature": %w`, err)}
		}
	}
	if _, ok := tfc.mutation.WalletProvidedFreezeTimestamp(); !ok {
		return &ValidationError{Name: "wallet_provided_freeze_timestamp", err: errors.New(`ent: missing required field "TokenFreeze.wallet_provided_freeze_timestamp"`)}
	}
	if _, ok := tfc.mutation.TokenCreateID(); !ok {
		return &ValidationError{Name: "token_create_id", err: errors.New(`ent: missing required field "TokenFreeze.token_create_id"`)}
	}
	if len(tfc.mutation.TokenCreateIDs()) == 0 {
		return &ValidationError{Name: "token_create", err: errors.New(`ent: missing required edge "TokenFreeze.token_create"`)}
	}
	return nil
}

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

func (tfc *TokenFreezeCreate) createSpec() (*TokenFreeze, *sqlgraph.CreateSpec) {
	var (
		_node = &TokenFreeze{config: tfc.config}
		_spec = sqlgraph.NewCreateSpec(tokenfreeze.Table, sqlgraph.NewFieldSpec(tokenfreeze.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = tfc.conflict
	if id, ok := tfc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := tfc.mutation.CreateTime(); ok {
		_spec.SetField(tokenfreeze.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := tfc.mutation.UpdateTime(); ok {
		_spec.SetField(tokenfreeze.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := tfc.mutation.Status(); ok {
		_spec.SetField(tokenfreeze.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := tfc.mutation.OwnerPublicKey(); ok {
		_spec.SetField(tokenfreeze.FieldOwnerPublicKey, field.TypeBytes, value)
		_node.OwnerPublicKey = value
	}
	if value, ok := tfc.mutation.TokenPublicKey(); ok {
		_spec.SetField(tokenfreeze.FieldTokenPublicKey, field.TypeBytes, value)
		_node.TokenPublicKey = value
	}
	if value, ok := tfc.mutation.IssuerSignature(); ok {
		_spec.SetField(tokenfreeze.FieldIssuerSignature, field.TypeBytes, value)
		_node.IssuerSignature = value
	}
	if value, ok := tfc.mutation.WalletProvidedFreezeTimestamp(); ok {
		_spec.SetField(tokenfreeze.FieldWalletProvidedFreezeTimestamp, field.TypeUint64, value)
		_node.WalletProvidedFreezeTimestamp = value
	}
	if value, ok := tfc.mutation.WalletProvidedThawTimestamp(); ok {
		_spec.SetField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64, value)
		_node.WalletProvidedThawTimestamp = value
	}
	if nodes := tfc.mutation.TokenCreateIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   tokenfreeze.TokenCreateTable,
			Columns: []string{tokenfreeze.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.TokenFreeze.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.TokenFreezeUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (tfc *TokenFreezeCreate) OnConflict(opts ...sql.ConflictOption) *TokenFreezeUpsertOne {
	tfc.conflict = opts
	return &TokenFreezeUpsertOne{
		create: tfc,
	}
}

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

type (
	// TokenFreezeUpsertOne is the builder for "upsert"-ing
	//  one TokenFreeze node.
	TokenFreezeUpsertOne struct {
		create *TokenFreezeCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *TokenFreezeUpsert) SetStatus(v schematype.TokenFreezeStatus) *TokenFreezeUpsert {
	u.Set(tokenfreeze.FieldStatus, v)
	return u
}

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

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsert) SetWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsert {
	u.Set(tokenfreeze.FieldWalletProvidedThawTimestamp, v)
	return u
}

// UpdateWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field to the value that was provided on create.
func (u *TokenFreezeUpsert) UpdateWalletProvidedThawTimestamp() *TokenFreezeUpsert {
	u.SetExcluded(tokenfreeze.FieldWalletProvidedThawTimestamp)
	return u
}

// AddWalletProvidedThawTimestamp adds v to the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsert) AddWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsert {
	u.Add(tokenfreeze.FieldWalletProvidedThawTimestamp, v)
	return u
}

// ClearWalletProvidedThawTimestamp clears the value of the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsert) ClearWalletProvidedThawTimestamp() *TokenFreezeUpsert {
	u.SetNull(tokenfreeze.FieldWalletProvidedThawTimestamp)
	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.TokenFreeze.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokenfreeze.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenFreezeUpsertOne) UpdateNewValues() *TokenFreezeUpsertOne {
	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(tokenfreeze.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(tokenfreeze.FieldCreateTime)
		}
		if _, exists := u.create.mutation.OwnerPublicKey(); exists {
			s.SetIgnore(tokenfreeze.FieldOwnerPublicKey)
		}
		if _, exists := u.create.mutation.TokenPublicKey(); exists {
			s.SetIgnore(tokenfreeze.FieldTokenPublicKey)
		}
		if _, exists := u.create.mutation.IssuerSignature(); exists {
			s.SetIgnore(tokenfreeze.FieldIssuerSignature)
		}
		if _, exists := u.create.mutation.WalletProvidedFreezeTimestamp(); exists {
			s.SetIgnore(tokenfreeze.FieldWalletProvidedFreezeTimestamp)
		}
		if _, exists := u.create.mutation.TokenCreateID(); exists {
			s.SetIgnore(tokenfreeze.FieldTokenCreateID)
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *TokenFreezeUpsertOne) SetStatus(v schematype.TokenFreezeStatus) *TokenFreezeUpsertOne {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.SetStatus(v)
	})
}

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

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertOne) SetWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsertOne {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.SetWalletProvidedThawTimestamp(v)
	})
}

// AddWalletProvidedThawTimestamp adds v to the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertOne) AddWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsertOne {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.AddWalletProvidedThawTimestamp(v)
	})
}

// UpdateWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field to the value that was provided on create.
func (u *TokenFreezeUpsertOne) UpdateWalletProvidedThawTimestamp() *TokenFreezeUpsertOne {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.UpdateWalletProvidedThawTimestamp()
	})
}

// ClearWalletProvidedThawTimestamp clears the value of the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertOne) ClearWalletProvidedThawTimestamp() *TokenFreezeUpsertOne {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.ClearWalletProvidedThawTimestamp()
	})
}

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

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

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

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

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

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

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

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

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

// TokenFreezeUpsertBulk is the builder for "upsert"-ing
// a bulk of TokenFreeze nodes.
type TokenFreezeUpsertBulk struct {
	create *TokenFreezeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.TokenFreeze.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokenfreeze.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenFreezeUpsertBulk) UpdateNewValues() *TokenFreezeUpsertBulk {
	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(tokenfreeze.FieldID)
			}
			if _, exists := b.mutation.CreateTime(); exists {
				s.SetIgnore(tokenfreeze.FieldCreateTime)
			}
			if _, exists := b.mutation.OwnerPublicKey(); exists {
				s.SetIgnore(tokenfreeze.FieldOwnerPublicKey)
			}
			if _, exists := b.mutation.TokenPublicKey(); exists {
				s.SetIgnore(tokenfreeze.FieldTokenPublicKey)
			}
			if _, exists := b.mutation.IssuerSignature(); exists {
				s.SetIgnore(tokenfreeze.FieldIssuerSignature)
			}
			if _, exists := b.mutation.WalletProvidedFreezeTimestamp(); exists {
				s.SetIgnore(tokenfreeze.FieldWalletProvidedFreezeTimestamp)
			}
			if _, exists := b.mutation.TokenCreateID(); exists {
				s.SetIgnore(tokenfreeze.FieldTokenCreateID)
			}
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *TokenFreezeUpsertBulk) SetStatus(v schematype.TokenFreezeStatus) *TokenFreezeUpsertBulk {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.SetStatus(v)
	})
}

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

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertBulk) SetWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsertBulk {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.SetWalletProvidedThawTimestamp(v)
	})
}

// AddWalletProvidedThawTimestamp adds v to the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertBulk) AddWalletProvidedThawTimestamp(v uint64) *TokenFreezeUpsertBulk {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.AddWalletProvidedThawTimestamp(v)
	})
}

// UpdateWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field to the value that was provided on create.
func (u *TokenFreezeUpsertBulk) UpdateWalletProvidedThawTimestamp() *TokenFreezeUpsertBulk {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.UpdateWalletProvidedThawTimestamp()
	})
}

// ClearWalletProvidedThawTimestamp clears the value of the "wallet_provided_thaw_timestamp" field.
func (u *TokenFreezeUpsertBulk) ClearWalletProvidedThawTimestamp() *TokenFreezeUpsertBulk {
	return u.Update(func(s *TokenFreezeUpsert) {
		s.ClearWalletProvidedThawTimestamp()
	})
}

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

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