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

// TokenCreateCreate is the builder for creating a TokenCreate entity.
type TokenCreateCreate struct {
	config
	mutation *TokenCreateMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetIssuerPublicKey sets the "issuer_public_key" field.
func (tcc *TokenCreateCreate) SetIssuerPublicKey(k keys.Public) *TokenCreateCreate {
	tcc.mutation.SetIssuerPublicKey(k)
	return tcc
}

// SetTokenName sets the "token_name" field.
func (tcc *TokenCreateCreate) SetTokenName(s string) *TokenCreateCreate {
	tcc.mutation.SetTokenName(s)
	return tcc
}

// SetTokenTicker sets the "token_ticker" field.
func (tcc *TokenCreateCreate) SetTokenTicker(s string) *TokenCreateCreate {
	tcc.mutation.SetTokenTicker(s)
	return tcc
}

// SetDecimals sets the "decimals" field.
func (tcc *TokenCreateCreate) SetDecimals(u uint8) *TokenCreateCreate {
	tcc.mutation.SetDecimals(u)
	return tcc
}

// SetMaxSupply sets the "max_supply" field.
func (tcc *TokenCreateCreate) SetMaxSupply(b []byte) *TokenCreateCreate {
	tcc.mutation.SetMaxSupply(b)
	return tcc
}

// SetIsFreezable sets the "is_freezable" field.
func (tcc *TokenCreateCreate) SetIsFreezable(b bool) *TokenCreateCreate {
	tcc.mutation.SetIsFreezable(b)
	return tcc
}

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

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

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

// SetOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field.
func (tcc *TokenCreateCreate) SetOperatorSpecificIssuerSignature(b []byte) *TokenCreateCreate {
	tcc.mutation.SetOperatorSpecificIssuerSignature(b)
	return tcc
}

// SetCreationEntityPublicKey sets the "creation_entity_public_key" field.
func (tcc *TokenCreateCreate) SetCreationEntityPublicKey(k keys.Public) *TokenCreateCreate {
	tcc.mutation.SetCreationEntityPublicKey(k)
	return tcc
}

// SetWalletProvidedTimestamp sets the "wallet_provided_timestamp" field.
func (tcc *TokenCreateCreate) SetWalletProvidedTimestamp(u uint64) *TokenCreateCreate {
	tcc.mutation.SetWalletProvidedTimestamp(u)
	return tcc
}

// SetNillableWalletProvidedTimestamp sets the "wallet_provided_timestamp" field if the given value is not nil.
func (tcc *TokenCreateCreate) SetNillableWalletProvidedTimestamp(u *uint64) *TokenCreateCreate {
	if u != nil {
		tcc.SetWalletProvidedTimestamp(*u)
	}
	return tcc
}

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

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

// AddTokenTransactionIDs adds the "token_transaction" edge to the TokenTransaction entity by IDs.
func (tcc *TokenCreateCreate) AddTokenTransactionIDs(ids ...uuid.UUID) *TokenCreateCreate {
	tcc.mutation.AddTokenTransactionIDs(ids...)
	return tcc
}

// AddTokenTransaction adds the "token_transaction" edges to the TokenTransaction entity.
func (tcc *TokenCreateCreate) AddTokenTransaction(t ...*TokenTransaction) *TokenCreateCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcc.AddTokenTransactionIDs(ids...)
}

// SetL1TokenCreateID sets the "l1_token_create" edge to the L1TokenCreate entity by ID.
func (tcc *TokenCreateCreate) SetL1TokenCreateID(id uuid.UUID) *TokenCreateCreate {
	tcc.mutation.SetL1TokenCreateID(id)
	return tcc
}

// SetNillableL1TokenCreateID sets the "l1_token_create" edge to the L1TokenCreate entity by ID if the given value is not nil.
func (tcc *TokenCreateCreate) SetNillableL1TokenCreateID(id *uuid.UUID) *TokenCreateCreate {
	if id != nil {
		tcc = tcc.SetL1TokenCreateID(*id)
	}
	return tcc
}

// SetL1TokenCreate sets the "l1_token_create" edge to the L1TokenCreate entity.
func (tcc *TokenCreateCreate) SetL1TokenCreate(l *L1TokenCreate) *TokenCreateCreate {
	return tcc.SetL1TokenCreateID(l.ID)
}

// AddTokenOutputIDs adds the "token_output" edge to the TokenOutput entity by IDs.
func (tcc *TokenCreateCreate) AddTokenOutputIDs(ids ...uuid.UUID) *TokenCreateCreate {
	tcc.mutation.AddTokenOutputIDs(ids...)
	return tcc
}

// AddTokenOutput adds the "token_output" edges to the TokenOutput entity.
func (tcc *TokenCreateCreate) AddTokenOutput(t ...*TokenOutput) *TokenCreateCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcc.AddTokenOutputIDs(ids...)
}

// AddTokenFreezeIDs adds the "token_freeze" edge to the TokenFreeze entity by IDs.
func (tcc *TokenCreateCreate) AddTokenFreezeIDs(ids ...uuid.UUID) *TokenCreateCreate {
	tcc.mutation.AddTokenFreezeIDs(ids...)
	return tcc
}

// AddTokenFreeze adds the "token_freeze" edges to the TokenFreeze entity.
func (tcc *TokenCreateCreate) AddTokenFreeze(t ...*TokenFreeze) *TokenCreateCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcc.AddTokenFreezeIDs(ids...)
}

// Mutation returns the TokenCreateMutation object of the builder.
func (tcc *TokenCreateCreate) Mutation() *TokenCreateMutation {
	return tcc.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tcc *TokenCreateCreate) check() error {
	if _, ok := tcc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "TokenCreate.create_time"`)}
	}
	if _, ok := tcc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "TokenCreate.update_time"`)}
	}
	if _, ok := tcc.mutation.IssuerPublicKey(); !ok {
		return &ValidationError{Name: "issuer_public_key", err: errors.New(`ent: missing required field "TokenCreate.issuer_public_key"`)}
	}
	if _, ok := tcc.mutation.TokenName(); !ok {
		return &ValidationError{Name: "token_name", err: errors.New(`ent: missing required field "TokenCreate.token_name"`)}
	}
	if v, ok := tcc.mutation.TokenName(); ok {
		if err := tokencreate.TokenNameValidator(v); err != nil {
			return &ValidationError{Name: "token_name", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.token_name": %w`, err)}
		}
	}
	if _, ok := tcc.mutation.TokenTicker(); !ok {
		return &ValidationError{Name: "token_ticker", err: errors.New(`ent: missing required field "TokenCreate.token_ticker"`)}
	}
	if v, ok := tcc.mutation.TokenTicker(); ok {
		if err := tokencreate.TokenTickerValidator(v); err != nil {
			return &ValidationError{Name: "token_ticker", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.token_ticker": %w`, err)}
		}
	}
	if _, ok := tcc.mutation.Decimals(); !ok {
		return &ValidationError{Name: "decimals", err: errors.New(`ent: missing required field "TokenCreate.decimals"`)}
	}
	if _, ok := tcc.mutation.MaxSupply(); !ok {
		return &ValidationError{Name: "max_supply", err: errors.New(`ent: missing required field "TokenCreate.max_supply"`)}
	}
	if v, ok := tcc.mutation.MaxSupply(); ok {
		if err := tokencreate.MaxSupplyValidator(v); err != nil {
			return &ValidationError{Name: "max_supply", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.max_supply": %w`, err)}
		}
	}
	if _, ok := tcc.mutation.IsFreezable(); !ok {
		return &ValidationError{Name: "is_freezable", err: errors.New(`ent: missing required field "TokenCreate.is_freezable"`)}
	}
	if _, ok := tcc.mutation.Network(); !ok {
		return &ValidationError{Name: "network", err: errors.New(`ent: missing required field "TokenCreate.network"`)}
	}
	if v, ok := tcc.mutation.Network(); ok {
		if err := tokencreate.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.network": %w`, err)}
		}
	}
	if _, ok := tcc.mutation.TokenIdentifier(); !ok {
		return &ValidationError{Name: "token_identifier", err: errors.New(`ent: missing required field "TokenCreate.token_identifier"`)}
	}
	if v, ok := tcc.mutation.TokenIdentifier(); ok {
		if err := tokencreate.TokenIdentifierValidator(v); err != nil {
			return &ValidationError{Name: "token_identifier", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.token_identifier": %w`, err)}
		}
	}
	if v, ok := tcc.mutation.IssuerSignature(); ok {
		if err := tokencreate.IssuerSignatureValidator(v); err != nil {
			return &ValidationError{Name: "issuer_signature", err: fmt.Errorf(`ent: validator failed for field "TokenCreate.issuer_signature": %w`, err)}
		}
	}
	if _, ok := tcc.mutation.CreationEntityPublicKey(); !ok {
		return &ValidationError{Name: "creation_entity_public_key", err: errors.New(`ent: missing required field "TokenCreate.creation_entity_public_key"`)}
	}
	return nil
}

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

func (tcc *TokenCreateCreate) createSpec() (*TokenCreate, *sqlgraph.CreateSpec) {
	var (
		_node = &TokenCreate{config: tcc.config}
		_spec = sqlgraph.NewCreateSpec(tokencreate.Table, sqlgraph.NewFieldSpec(tokencreate.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = tcc.conflict
	if id, ok := tcc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := tcc.mutation.CreateTime(); ok {
		_spec.SetField(tokencreate.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := tcc.mutation.UpdateTime(); ok {
		_spec.SetField(tokencreate.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := tcc.mutation.IssuerPublicKey(); ok {
		_spec.SetField(tokencreate.FieldIssuerPublicKey, field.TypeBytes, value)
		_node.IssuerPublicKey = value
	}
	if value, ok := tcc.mutation.TokenName(); ok {
		_spec.SetField(tokencreate.FieldTokenName, field.TypeString, value)
		_node.TokenName = value
	}
	if value, ok := tcc.mutation.TokenTicker(); ok {
		_spec.SetField(tokencreate.FieldTokenTicker, field.TypeString, value)
		_node.TokenTicker = value
	}
	if value, ok := tcc.mutation.Decimals(); ok {
		_spec.SetField(tokencreate.FieldDecimals, field.TypeUint8, value)
		_node.Decimals = value
	}
	if value, ok := tcc.mutation.MaxSupply(); ok {
		_spec.SetField(tokencreate.FieldMaxSupply, field.TypeBytes, value)
		_node.MaxSupply = value
	}
	if value, ok := tcc.mutation.IsFreezable(); ok {
		_spec.SetField(tokencreate.FieldIsFreezable, field.TypeBool, value)
		_node.IsFreezable = value
	}
	if value, ok := tcc.mutation.Network(); ok {
		_spec.SetField(tokencreate.FieldNetwork, field.TypeEnum, value)
		_node.Network = value
	}
	if value, ok := tcc.mutation.TokenIdentifier(); ok {
		_spec.SetField(tokencreate.FieldTokenIdentifier, field.TypeBytes, value)
		_node.TokenIdentifier = value
	}
	if value, ok := tcc.mutation.IssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldIssuerSignature, field.TypeBytes, value)
		_node.IssuerSignature = value
	}
	if value, ok := tcc.mutation.OperatorSpecificIssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldOperatorSpecificIssuerSignature, field.TypeBytes, value)
		_node.OperatorSpecificIssuerSignature = value
	}
	if value, ok := tcc.mutation.CreationEntityPublicKey(); ok {
		_spec.SetField(tokencreate.FieldCreationEntityPublicKey, field.TypeBytes, value)
		_node.CreationEntityPublicKey = value
	}
	if value, ok := tcc.mutation.WalletProvidedTimestamp(); ok {
		_spec.SetField(tokencreate.FieldWalletProvidedTimestamp, field.TypeUint64, value)
		_node.WalletProvidedTimestamp = value
	}
	if nodes := tcc.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokencreate.TokenTransactionTable,
			Columns: []string{tokencreate.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)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.L1TokenCreateIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tokencreate.L1TokenCreateTable,
			Columns: []string{tokencreate.L1TokenCreateColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(l1tokencreate.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.token_create_l1_token_create = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.TokenOutputIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   tokencreate.TokenOutputTable,
			Columns: []string{tokencreate.TokenOutputColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokenoutput.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.TokenFreezeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   tokencreate.TokenFreezeTable,
			Columns: []string{tokencreate.TokenFreezeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokenfreeze.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_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.TokenCreate.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.TokenCreateUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (tcc *TokenCreateCreate) OnConflict(opts ...sql.ConflictOption) *TokenCreateUpsertOne {
	tcc.conflict = opts
	return &TokenCreateUpsertOne{
		create: tcc,
	}
}

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

type (
	// TokenCreateUpsertOne is the builder for "upsert"-ing
	//  one TokenCreate node.
	TokenCreateUpsertOne struct {
		create *TokenCreateCreate
	}

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

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

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

// SetIssuerSignature sets the "issuer_signature" field.
func (u *TokenCreateUpsert) SetIssuerSignature(v []byte) *TokenCreateUpsert {
	u.Set(tokencreate.FieldIssuerSignature, v)
	return u
}

// UpdateIssuerSignature sets the "issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsert) UpdateIssuerSignature() *TokenCreateUpsert {
	u.SetExcluded(tokencreate.FieldIssuerSignature)
	return u
}

// ClearIssuerSignature clears the value of the "issuer_signature" field.
func (u *TokenCreateUpsert) ClearIssuerSignature() *TokenCreateUpsert {
	u.SetNull(tokencreate.FieldIssuerSignature)
	return u
}

// SetOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsert) SetOperatorSpecificIssuerSignature(v []byte) *TokenCreateUpsert {
	u.Set(tokencreate.FieldOperatorSpecificIssuerSignature, v)
	return u
}

// UpdateOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsert) UpdateOperatorSpecificIssuerSignature() *TokenCreateUpsert {
	u.SetExcluded(tokencreate.FieldOperatorSpecificIssuerSignature)
	return u
}

// ClearOperatorSpecificIssuerSignature clears the value of the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsert) ClearOperatorSpecificIssuerSignature() *TokenCreateUpsert {
	u.SetNull(tokencreate.FieldOperatorSpecificIssuerSignature)
	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.TokenCreate.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokencreate.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenCreateUpsertOne) UpdateNewValues() *TokenCreateUpsertOne {
	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(tokencreate.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(tokencreate.FieldCreateTime)
		}
		if _, exists := u.create.mutation.IssuerPublicKey(); exists {
			s.SetIgnore(tokencreate.FieldIssuerPublicKey)
		}
		if _, exists := u.create.mutation.TokenName(); exists {
			s.SetIgnore(tokencreate.FieldTokenName)
		}
		if _, exists := u.create.mutation.TokenTicker(); exists {
			s.SetIgnore(tokencreate.FieldTokenTicker)
		}
		if _, exists := u.create.mutation.Decimals(); exists {
			s.SetIgnore(tokencreate.FieldDecimals)
		}
		if _, exists := u.create.mutation.MaxSupply(); exists {
			s.SetIgnore(tokencreate.FieldMaxSupply)
		}
		if _, exists := u.create.mutation.IsFreezable(); exists {
			s.SetIgnore(tokencreate.FieldIsFreezable)
		}
		if _, exists := u.create.mutation.Network(); exists {
			s.SetIgnore(tokencreate.FieldNetwork)
		}
		if _, exists := u.create.mutation.TokenIdentifier(); exists {
			s.SetIgnore(tokencreate.FieldTokenIdentifier)
		}
		if _, exists := u.create.mutation.CreationEntityPublicKey(); exists {
			s.SetIgnore(tokencreate.FieldCreationEntityPublicKey)
		}
		if _, exists := u.create.mutation.WalletProvidedTimestamp(); exists {
			s.SetIgnore(tokencreate.FieldWalletProvidedTimestamp)
		}
	}))
	return u
}

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

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

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

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

// SetIssuerSignature sets the "issuer_signature" field.
func (u *TokenCreateUpsertOne) SetIssuerSignature(v []byte) *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.SetIssuerSignature(v)
	})
}

// UpdateIssuerSignature sets the "issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsertOne) UpdateIssuerSignature() *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.UpdateIssuerSignature()
	})
}

// ClearIssuerSignature clears the value of the "issuer_signature" field.
func (u *TokenCreateUpsertOne) ClearIssuerSignature() *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.ClearIssuerSignature()
	})
}

// SetOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsertOne) SetOperatorSpecificIssuerSignature(v []byte) *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.SetOperatorSpecificIssuerSignature(v)
	})
}

// UpdateOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsertOne) UpdateOperatorSpecificIssuerSignature() *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.UpdateOperatorSpecificIssuerSignature()
	})
}

// ClearOperatorSpecificIssuerSignature clears the value of the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsertOne) ClearOperatorSpecificIssuerSignature() *TokenCreateUpsertOne {
	return u.Update(func(s *TokenCreateUpsert) {
		s.ClearOperatorSpecificIssuerSignature()
	})
}

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

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

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

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

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

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

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

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

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

// TokenCreateUpsertBulk is the builder for "upsert"-ing
// a bulk of TokenCreate nodes.
type TokenCreateUpsertBulk struct {
	create *TokenCreateCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.TokenCreate.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tokencreate.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TokenCreateUpsertBulk) UpdateNewValues() *TokenCreateUpsertBulk {
	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(tokencreate.FieldID)
			}
			if _, exists := b.mutation.CreateTime(); exists {
				s.SetIgnore(tokencreate.FieldCreateTime)
			}
			if _, exists := b.mutation.IssuerPublicKey(); exists {
				s.SetIgnore(tokencreate.FieldIssuerPublicKey)
			}
			if _, exists := b.mutation.TokenName(); exists {
				s.SetIgnore(tokencreate.FieldTokenName)
			}
			if _, exists := b.mutation.TokenTicker(); exists {
				s.SetIgnore(tokencreate.FieldTokenTicker)
			}
			if _, exists := b.mutation.Decimals(); exists {
				s.SetIgnore(tokencreate.FieldDecimals)
			}
			if _, exists := b.mutation.MaxSupply(); exists {
				s.SetIgnore(tokencreate.FieldMaxSupply)
			}
			if _, exists := b.mutation.IsFreezable(); exists {
				s.SetIgnore(tokencreate.FieldIsFreezable)
			}
			if _, exists := b.mutation.Network(); exists {
				s.SetIgnore(tokencreate.FieldNetwork)
			}
			if _, exists := b.mutation.TokenIdentifier(); exists {
				s.SetIgnore(tokencreate.FieldTokenIdentifier)
			}
			if _, exists := b.mutation.CreationEntityPublicKey(); exists {
				s.SetIgnore(tokencreate.FieldCreationEntityPublicKey)
			}
			if _, exists := b.mutation.WalletProvidedTimestamp(); exists {
				s.SetIgnore(tokencreate.FieldWalletProvidedTimestamp)
			}
		}
	}))
	return u
}

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

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

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

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

// SetIssuerSignature sets the "issuer_signature" field.
func (u *TokenCreateUpsertBulk) SetIssuerSignature(v []byte) *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.SetIssuerSignature(v)
	})
}

// UpdateIssuerSignature sets the "issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsertBulk) UpdateIssuerSignature() *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.UpdateIssuerSignature()
	})
}

// ClearIssuerSignature clears the value of the "issuer_signature" field.
func (u *TokenCreateUpsertBulk) ClearIssuerSignature() *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.ClearIssuerSignature()
	})
}

// SetOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsertBulk) SetOperatorSpecificIssuerSignature(v []byte) *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.SetOperatorSpecificIssuerSignature(v)
	})
}

// UpdateOperatorSpecificIssuerSignature sets the "operator_specific_issuer_signature" field to the value that was provided on create.
func (u *TokenCreateUpsertBulk) UpdateOperatorSpecificIssuerSignature() *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.UpdateOperatorSpecificIssuerSignature()
	})
}

// ClearOperatorSpecificIssuerSignature clears the value of the "operator_specific_issuer_signature" field.
func (u *TokenCreateUpsertBulk) ClearOperatorSpecificIssuerSignature() *TokenCreateUpsertBulk {
	return u.Update(func(s *TokenCreateUpsert) {
		s.ClearOperatorSpecificIssuerSignature()
	})
}

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

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