// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/so/ent/l1tokencreate"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"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"
)

// TokenCreateUpdate is the builder for updating TokenCreate entities.
type TokenCreateUpdate struct {
	config
	hooks    []Hook
	mutation *TokenCreateMutation
}

// Where appends a list predicates to the TokenCreateUpdate builder.
func (tcu *TokenCreateUpdate) Where(ps ...predicate.TokenCreate) *TokenCreateUpdate {
	tcu.mutation.Where(ps...)
	return tcu
}

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

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

// ClearIssuerSignature clears the value of the "issuer_signature" field.
func (tcu *TokenCreateUpdate) ClearIssuerSignature() *TokenCreateUpdate {
	tcu.mutation.ClearIssuerSignature()
	return tcu
}

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

// ClearOperatorSpecificIssuerSignature clears the value of the "operator_specific_issuer_signature" field.
func (tcu *TokenCreateUpdate) ClearOperatorSpecificIssuerSignature() *TokenCreateUpdate {
	tcu.mutation.ClearOperatorSpecificIssuerSignature()
	return tcu
}

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

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

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

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

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

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

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

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

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

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

// ClearTokenTransaction clears all "token_transaction" edges to the TokenTransaction entity.
func (tcu *TokenCreateUpdate) ClearTokenTransaction() *TokenCreateUpdate {
	tcu.mutation.ClearTokenTransaction()
	return tcu
}

// RemoveTokenTransactionIDs removes the "token_transaction" edge to TokenTransaction entities by IDs.
func (tcu *TokenCreateUpdate) RemoveTokenTransactionIDs(ids ...uuid.UUID) *TokenCreateUpdate {
	tcu.mutation.RemoveTokenTransactionIDs(ids...)
	return tcu
}

// RemoveTokenTransaction removes "token_transaction" edges to TokenTransaction entities.
func (tcu *TokenCreateUpdate) RemoveTokenTransaction(t ...*TokenTransaction) *TokenCreateUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcu.RemoveTokenTransactionIDs(ids...)
}

// ClearL1TokenCreate clears the "l1_token_create" edge to the L1TokenCreate entity.
func (tcu *TokenCreateUpdate) ClearL1TokenCreate() *TokenCreateUpdate {
	tcu.mutation.ClearL1TokenCreate()
	return tcu
}

// ClearTokenOutput clears all "token_output" edges to the TokenOutput entity.
func (tcu *TokenCreateUpdate) ClearTokenOutput() *TokenCreateUpdate {
	tcu.mutation.ClearTokenOutput()
	return tcu
}

// RemoveTokenOutputIDs removes the "token_output" edge to TokenOutput entities by IDs.
func (tcu *TokenCreateUpdate) RemoveTokenOutputIDs(ids ...uuid.UUID) *TokenCreateUpdate {
	tcu.mutation.RemoveTokenOutputIDs(ids...)
	return tcu
}

// RemoveTokenOutput removes "token_output" edges to TokenOutput entities.
func (tcu *TokenCreateUpdate) RemoveTokenOutput(t ...*TokenOutput) *TokenCreateUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcu.RemoveTokenOutputIDs(ids...)
}

// ClearTokenFreeze clears all "token_freeze" edges to the TokenFreeze entity.
func (tcu *TokenCreateUpdate) ClearTokenFreeze() *TokenCreateUpdate {
	tcu.mutation.ClearTokenFreeze()
	return tcu
}

// RemoveTokenFreezeIDs removes the "token_freeze" edge to TokenFreeze entities by IDs.
func (tcu *TokenCreateUpdate) RemoveTokenFreezeIDs(ids ...uuid.UUID) *TokenCreateUpdate {
	tcu.mutation.RemoveTokenFreezeIDs(ids...)
	return tcu
}

// RemoveTokenFreeze removes "token_freeze" edges to TokenFreeze entities.
func (tcu *TokenCreateUpdate) RemoveTokenFreeze(t ...*TokenFreeze) *TokenCreateUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcu.RemoveTokenFreezeIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (tcu *TokenCreateUpdate) Save(ctx context.Context) (int, error) {
	tcu.defaults()
	return withHooks(ctx, tcu.sqlSave, tcu.mutation, tcu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tcu *TokenCreateUpdate) SaveX(ctx context.Context) int {
	affected, err := tcu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (tcu *TokenCreateUpdate) defaults() {
	if _, ok := tcu.mutation.UpdateTime(); !ok {
		v := tokencreate.UpdateDefaultUpdateTime()
		tcu.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (tcu *TokenCreateUpdate) check() error {
	if v, ok := tcu.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)}
		}
	}
	return nil
}

func (tcu *TokenCreateUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tcu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(tokencreate.Table, tokencreate.Columns, sqlgraph.NewFieldSpec(tokencreate.FieldID, field.TypeUUID))
	if ps := tcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcu.mutation.UpdateTime(); ok {
		_spec.SetField(tokencreate.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tcu.mutation.IssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldIssuerSignature, field.TypeBytes, value)
	}
	if tcu.mutation.IssuerSignatureCleared() {
		_spec.ClearField(tokencreate.FieldIssuerSignature, field.TypeBytes)
	}
	if value, ok := tcu.mutation.OperatorSpecificIssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldOperatorSpecificIssuerSignature, field.TypeBytes, value)
	}
	if tcu.mutation.OperatorSpecificIssuerSignatureCleared() {
		_spec.ClearField(tokencreate.FieldOperatorSpecificIssuerSignature, field.TypeBytes)
	}
	if tcu.mutation.WalletProvidedTimestampCleared() {
		_spec.ClearField(tokencreate.FieldWalletProvidedTimestamp, field.TypeUint64)
	}
	if tcu.mutation.TokenTransactionCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !tcu.mutation.TokenTransactionCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.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.Add = append(_spec.Edges.Add, edge)
	}
	if tcu.mutation.L1TokenCreateCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcu.mutation.TokenOutputCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.RemovedTokenOutputIDs(); len(nodes) > 0 && !tcu.mutation.TokenOutputCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.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.Add = append(_spec.Edges.Add, edge)
	}
	if tcu.mutation.TokenFreezeCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.RemovedTokenFreezeIDs(); len(nodes) > 0 && !tcu.mutation.TokenFreezeCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.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.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, tcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokencreate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tcu.mutation.done = true
	return n, nil
}

// TokenCreateUpdateOne is the builder for updating a single TokenCreate entity.
type TokenCreateUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenCreateMutation
}

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

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

// ClearIssuerSignature clears the value of the "issuer_signature" field.
func (tcuo *TokenCreateUpdateOne) ClearIssuerSignature() *TokenCreateUpdateOne {
	tcuo.mutation.ClearIssuerSignature()
	return tcuo
}

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

// ClearOperatorSpecificIssuerSignature clears the value of the "operator_specific_issuer_signature" field.
func (tcuo *TokenCreateUpdateOne) ClearOperatorSpecificIssuerSignature() *TokenCreateUpdateOne {
	tcuo.mutation.ClearOperatorSpecificIssuerSignature()
	return tcuo
}

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

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

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

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

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

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

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

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

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

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

// ClearTokenTransaction clears all "token_transaction" edges to the TokenTransaction entity.
func (tcuo *TokenCreateUpdateOne) ClearTokenTransaction() *TokenCreateUpdateOne {
	tcuo.mutation.ClearTokenTransaction()
	return tcuo
}

// RemoveTokenTransactionIDs removes the "token_transaction" edge to TokenTransaction entities by IDs.
func (tcuo *TokenCreateUpdateOne) RemoveTokenTransactionIDs(ids ...uuid.UUID) *TokenCreateUpdateOne {
	tcuo.mutation.RemoveTokenTransactionIDs(ids...)
	return tcuo
}

// RemoveTokenTransaction removes "token_transaction" edges to TokenTransaction entities.
func (tcuo *TokenCreateUpdateOne) RemoveTokenTransaction(t ...*TokenTransaction) *TokenCreateUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcuo.RemoveTokenTransactionIDs(ids...)
}

// ClearL1TokenCreate clears the "l1_token_create" edge to the L1TokenCreate entity.
func (tcuo *TokenCreateUpdateOne) ClearL1TokenCreate() *TokenCreateUpdateOne {
	tcuo.mutation.ClearL1TokenCreate()
	return tcuo
}

// ClearTokenOutput clears all "token_output" edges to the TokenOutput entity.
func (tcuo *TokenCreateUpdateOne) ClearTokenOutput() *TokenCreateUpdateOne {
	tcuo.mutation.ClearTokenOutput()
	return tcuo
}

// RemoveTokenOutputIDs removes the "token_output" edge to TokenOutput entities by IDs.
func (tcuo *TokenCreateUpdateOne) RemoveTokenOutputIDs(ids ...uuid.UUID) *TokenCreateUpdateOne {
	tcuo.mutation.RemoveTokenOutputIDs(ids...)
	return tcuo
}

// RemoveTokenOutput removes "token_output" edges to TokenOutput entities.
func (tcuo *TokenCreateUpdateOne) RemoveTokenOutput(t ...*TokenOutput) *TokenCreateUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcuo.RemoveTokenOutputIDs(ids...)
}

// ClearTokenFreeze clears all "token_freeze" edges to the TokenFreeze entity.
func (tcuo *TokenCreateUpdateOne) ClearTokenFreeze() *TokenCreateUpdateOne {
	tcuo.mutation.ClearTokenFreeze()
	return tcuo
}

// RemoveTokenFreezeIDs removes the "token_freeze" edge to TokenFreeze entities by IDs.
func (tcuo *TokenCreateUpdateOne) RemoveTokenFreezeIDs(ids ...uuid.UUID) *TokenCreateUpdateOne {
	tcuo.mutation.RemoveTokenFreezeIDs(ids...)
	return tcuo
}

// RemoveTokenFreeze removes "token_freeze" edges to TokenFreeze entities.
func (tcuo *TokenCreateUpdateOne) RemoveTokenFreeze(t ...*TokenFreeze) *TokenCreateUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcuo.RemoveTokenFreezeIDs(ids...)
}

// Where appends a list predicates to the TokenCreateUpdate builder.
func (tcuo *TokenCreateUpdateOne) Where(ps ...predicate.TokenCreate) *TokenCreateUpdateOne {
	tcuo.mutation.Where(ps...)
	return tcuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (tcuo *TokenCreateUpdateOne) Select(field string, fields ...string) *TokenCreateUpdateOne {
	tcuo.fields = append([]string{field}, fields...)
	return tcuo
}

// Save executes the query and returns the updated TokenCreate entity.
func (tcuo *TokenCreateUpdateOne) Save(ctx context.Context) (*TokenCreate, error) {
	tcuo.defaults()
	return withHooks(ctx, tcuo.sqlSave, tcuo.mutation, tcuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tcuo *TokenCreateUpdateOne) SaveX(ctx context.Context) *TokenCreate {
	node, err := tcuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (tcuo *TokenCreateUpdateOne) Exec(ctx context.Context) error {
	_, err := tcuo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (tcuo *TokenCreateUpdateOne) defaults() {
	if _, ok := tcuo.mutation.UpdateTime(); !ok {
		v := tokencreate.UpdateDefaultUpdateTime()
		tcuo.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (tcuo *TokenCreateUpdateOne) check() error {
	if v, ok := tcuo.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)}
		}
	}
	return nil
}

func (tcuo *TokenCreateUpdateOne) sqlSave(ctx context.Context) (_node *TokenCreate, err error) {
	if err := tcuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(tokencreate.Table, tokencreate.Columns, sqlgraph.NewFieldSpec(tokencreate.FieldID, field.TypeUUID))
	id, ok := tcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TokenCreate.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, tokencreate.FieldID)
		for _, f := range fields {
			if !tokencreate.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != tokencreate.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcuo.mutation.UpdateTime(); ok {
		_spec.SetField(tokencreate.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tcuo.mutation.IssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldIssuerSignature, field.TypeBytes, value)
	}
	if tcuo.mutation.IssuerSignatureCleared() {
		_spec.ClearField(tokencreate.FieldIssuerSignature, field.TypeBytes)
	}
	if value, ok := tcuo.mutation.OperatorSpecificIssuerSignature(); ok {
		_spec.SetField(tokencreate.FieldOperatorSpecificIssuerSignature, field.TypeBytes, value)
	}
	if tcuo.mutation.OperatorSpecificIssuerSignatureCleared() {
		_spec.ClearField(tokencreate.FieldOperatorSpecificIssuerSignature, field.TypeBytes)
	}
	if tcuo.mutation.WalletProvidedTimestampCleared() {
		_spec.ClearField(tokencreate.FieldWalletProvidedTimestamp, field.TypeUint64)
	}
	if tcuo.mutation.TokenTransactionCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !tcuo.mutation.TokenTransactionCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.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.Add = append(_spec.Edges.Add, edge)
	}
	if tcuo.mutation.L1TokenCreateCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcuo.mutation.TokenOutputCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.RemovedTokenOutputIDs(); len(nodes) > 0 && !tcuo.mutation.TokenOutputCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.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.Add = append(_spec.Edges.Add, edge)
	}
	if tcuo.mutation.TokenFreezeCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.RemovedTokenFreezeIDs(); len(nodes) > 0 && !tcuo.mutation.TokenFreezeCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.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.Add = append(_spec.Edges.Add, edge)
	}
	_node = &TokenCreate{config: tcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokencreate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tcuo.mutation.done = true
	return _node, nil
}
