// 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/predicate"
	"github.com/lightsparkdev/spark/so/ent/tokenmint"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
)

// TokenMintUpdate is the builder for updating TokenMint entities.
type TokenMintUpdate struct {
	config
	hooks    []Hook
	mutation *TokenMintMutation
}

// Where appends a list predicates to the TokenMintUpdate builder.
func (tmu *TokenMintUpdate) Where(ps ...predicate.TokenMint) *TokenMintUpdate {
	tmu.mutation.Where(ps...)
	return tmu
}

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

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

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

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

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

// Mutation returns the TokenMintMutation object of the builder.
func (tmu *TokenMintUpdate) Mutation() *TokenMintMutation {
	return tmu.mutation
}

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

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

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

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

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

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

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

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

func (tmu *TokenMintUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(tokenmint.Table, tokenmint.Columns, sqlgraph.NewFieldSpec(tokenmint.FieldID, field.TypeUUID))
	if ps := tmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tmu.mutation.UpdateTime(); ok {
		_spec.SetField(tokenmint.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tmu.mutation.OperatorSpecificIssuerSignature(); ok {
		_spec.SetField(tokenmint.FieldOperatorSpecificIssuerSignature, field.TypeBytes, value)
	}
	if tmu.mutation.OperatorSpecificIssuerSignatureCleared() {
		_spec.ClearField(tokenmint.FieldOperatorSpecificIssuerSignature, field.TypeBytes)
	}
	if tmu.mutation.TokenIdentifierCleared() {
		_spec.ClearField(tokenmint.FieldTokenIdentifier, field.TypeBytes)
	}
	if tmu.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tmu.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !tmu.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.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 := tmu.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.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 n, err = sqlgraph.UpdateNodes(ctx, tmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokenmint.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tmu.mutation.done = true
	return n, nil
}

// TokenMintUpdateOne is the builder for updating a single TokenMint entity.
type TokenMintUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenMintMutation
}

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

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

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

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

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

// Mutation returns the TokenMintMutation object of the builder.
func (tmuo *TokenMintUpdateOne) Mutation() *TokenMintMutation {
	return tmuo.mutation
}

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

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

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

// Where appends a list predicates to the TokenMintUpdate builder.
func (tmuo *TokenMintUpdateOne) Where(ps ...predicate.TokenMint) *TokenMintUpdateOne {
	tmuo.mutation.Where(ps...)
	return tmuo
}

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

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

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

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

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

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

func (tmuo *TokenMintUpdateOne) sqlSave(ctx context.Context) (_node *TokenMint, err error) {
	_spec := sqlgraph.NewUpdateSpec(tokenmint.Table, tokenmint.Columns, sqlgraph.NewFieldSpec(tokenmint.FieldID, field.TypeUUID))
	id, ok := tmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TokenMint.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, tokenmint.FieldID)
		for _, f := range fields {
			if !tokenmint.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != tokenmint.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tmuo.mutation.UpdateTime(); ok {
		_spec.SetField(tokenmint.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tmuo.mutation.OperatorSpecificIssuerSignature(); ok {
		_spec.SetField(tokenmint.FieldOperatorSpecificIssuerSignature, field.TypeBytes, value)
	}
	if tmuo.mutation.OperatorSpecificIssuerSignatureCleared() {
		_spec.ClearField(tokenmint.FieldOperatorSpecificIssuerSignature, field.TypeBytes)
	}
	if tmuo.mutation.TokenIdentifierCleared() {
		_spec.ClearField(tokenmint.FieldTokenIdentifier, field.TypeBytes)
	}
	if tmuo.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tmuo.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !tmuo.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.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 := tmuo.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tokenmint.TokenTransactionTable,
			Columns: []string{tokenmint.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)
	}
	_node = &TokenMint{config: tmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokenmint.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tmuo.mutation.done = true
	return _node, nil
}
