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

// TokenTransactionPeerSignatureUpdate is the builder for updating TokenTransactionPeerSignature entities.
type TokenTransactionPeerSignatureUpdate struct {
	config
	hooks    []Hook
	mutation *TokenTransactionPeerSignatureMutation
}

// Where appends a list predicates to the TokenTransactionPeerSignatureUpdate builder.
func (ttpsu *TokenTransactionPeerSignatureUpdate) Where(ps ...predicate.TokenTransactionPeerSignature) *TokenTransactionPeerSignatureUpdate {
	ttpsu.mutation.Where(ps...)
	return ttpsu
}

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

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (ttpsu *TokenTransactionPeerSignatureUpdate) SetOperatorIdentityPublicKey(k keys.Public) *TokenTransactionPeerSignatureUpdate {
	ttpsu.mutation.SetOperatorIdentityPublicKey(k)
	return ttpsu
}

// SetNillableOperatorIdentityPublicKey sets the "operator_identity_public_key" field if the given value is not nil.
func (ttpsu *TokenTransactionPeerSignatureUpdate) SetNillableOperatorIdentityPublicKey(k *keys.Public) *TokenTransactionPeerSignatureUpdate {
	if k != nil {
		ttpsu.SetOperatorIdentityPublicKey(*k)
	}
	return ttpsu
}

// SetSignature sets the "signature" field.
func (ttpsu *TokenTransactionPeerSignatureUpdate) SetSignature(b []byte) *TokenTransactionPeerSignatureUpdate {
	ttpsu.mutation.SetSignature(b)
	return ttpsu
}

// SetTokenTransactionID sets the "token_transaction" edge to the TokenTransaction entity by ID.
func (ttpsu *TokenTransactionPeerSignatureUpdate) SetTokenTransactionID(id uuid.UUID) *TokenTransactionPeerSignatureUpdate {
	ttpsu.mutation.SetTokenTransactionID(id)
	return ttpsu
}

// SetTokenTransaction sets the "token_transaction" edge to the TokenTransaction entity.
func (ttpsu *TokenTransactionPeerSignatureUpdate) SetTokenTransaction(t *TokenTransaction) *TokenTransactionPeerSignatureUpdate {
	return ttpsu.SetTokenTransactionID(t.ID)
}

// Mutation returns the TokenTransactionPeerSignatureMutation object of the builder.
func (ttpsu *TokenTransactionPeerSignatureUpdate) Mutation() *TokenTransactionPeerSignatureMutation {
	return ttpsu.mutation
}

// ClearTokenTransaction clears the "token_transaction" edge to the TokenTransaction entity.
func (ttpsu *TokenTransactionPeerSignatureUpdate) ClearTokenTransaction() *TokenTransactionPeerSignatureUpdate {
	ttpsu.mutation.ClearTokenTransaction()
	return ttpsu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (ttpsu *TokenTransactionPeerSignatureUpdate) check() error {
	if v, ok := ttpsu.mutation.Signature(); ok {
		if err := tokentransactionpeersignature.SignatureValidator(v); err != nil {
			return &ValidationError{Name: "signature", err: fmt.Errorf(`ent: validator failed for field "TokenTransactionPeerSignature.signature": %w`, err)}
		}
	}
	if ttpsu.mutation.TokenTransactionCleared() && len(ttpsu.mutation.TokenTransactionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenTransactionPeerSignature.token_transaction"`)
	}
	return nil
}

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

// TokenTransactionPeerSignatureUpdateOne is the builder for updating a single TokenTransactionPeerSignature entity.
type TokenTransactionPeerSignatureUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenTransactionPeerSignatureMutation
}

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

// SetOperatorIdentityPublicKey sets the "operator_identity_public_key" field.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) SetOperatorIdentityPublicKey(k keys.Public) *TokenTransactionPeerSignatureUpdateOne {
	ttpsuo.mutation.SetOperatorIdentityPublicKey(k)
	return ttpsuo
}

// SetNillableOperatorIdentityPublicKey sets the "operator_identity_public_key" field if the given value is not nil.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) SetNillableOperatorIdentityPublicKey(k *keys.Public) *TokenTransactionPeerSignatureUpdateOne {
	if k != nil {
		ttpsuo.SetOperatorIdentityPublicKey(*k)
	}
	return ttpsuo
}

// SetSignature sets the "signature" field.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) SetSignature(b []byte) *TokenTransactionPeerSignatureUpdateOne {
	ttpsuo.mutation.SetSignature(b)
	return ttpsuo
}

// SetTokenTransactionID sets the "token_transaction" edge to the TokenTransaction entity by ID.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) SetTokenTransactionID(id uuid.UUID) *TokenTransactionPeerSignatureUpdateOne {
	ttpsuo.mutation.SetTokenTransactionID(id)
	return ttpsuo
}

// SetTokenTransaction sets the "token_transaction" edge to the TokenTransaction entity.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) SetTokenTransaction(t *TokenTransaction) *TokenTransactionPeerSignatureUpdateOne {
	return ttpsuo.SetTokenTransactionID(t.ID)
}

// Mutation returns the TokenTransactionPeerSignatureMutation object of the builder.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) Mutation() *TokenTransactionPeerSignatureMutation {
	return ttpsuo.mutation
}

// ClearTokenTransaction clears the "token_transaction" edge to the TokenTransaction entity.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) ClearTokenTransaction() *TokenTransactionPeerSignatureUpdateOne {
	ttpsuo.mutation.ClearTokenTransaction()
	return ttpsuo
}

// Where appends a list predicates to the TokenTransactionPeerSignatureUpdate builder.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) Where(ps ...predicate.TokenTransactionPeerSignature) *TokenTransactionPeerSignatureUpdateOne {
	ttpsuo.mutation.Where(ps...)
	return ttpsuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (ttpsuo *TokenTransactionPeerSignatureUpdateOne) check() error {
	if v, ok := ttpsuo.mutation.Signature(); ok {
		if err := tokentransactionpeersignature.SignatureValidator(v); err != nil {
			return &ValidationError{Name: "signature", err: fmt.Errorf(`ent: validator failed for field "TokenTransactionPeerSignature.signature": %w`, err)}
		}
	}
	if ttpsuo.mutation.TokenTransactionCleared() && len(ttpsuo.mutation.TokenTransactionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenTransactionPeerSignature.token_transaction"`)
	}
	return nil
}

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