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

// SigningNonceUpdate is the builder for updating SigningNonce entities.
type SigningNonceUpdate struct {
	config
	hooks    []Hook
	mutation *SigningNonceMutation
}

// Where appends a list predicates to the SigningNonceUpdate builder.
func (snu *SigningNonceUpdate) Where(ps ...predicate.SigningNonce) *SigningNonceUpdate {
	snu.mutation.Where(ps...)
	return snu
}

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

// SetMessage sets the "message" field.
func (snu *SigningNonceUpdate) SetMessage(b []byte) *SigningNonceUpdate {
	snu.mutation.SetMessage(b)
	return snu
}

// ClearMessage clears the value of the "message" field.
func (snu *SigningNonceUpdate) ClearMessage() *SigningNonceUpdate {
	snu.mutation.ClearMessage()
	return snu
}

// SetRetryFingerprint sets the "retry_fingerprint" field.
func (snu *SigningNonceUpdate) SetRetryFingerprint(b []byte) *SigningNonceUpdate {
	snu.mutation.SetRetryFingerprint(b)
	return snu
}

// ClearRetryFingerprint clears the value of the "retry_fingerprint" field.
func (snu *SigningNonceUpdate) ClearRetryFingerprint() *SigningNonceUpdate {
	snu.mutation.ClearRetryFingerprint()
	return snu
}

// Mutation returns the SigningNonceMutation object of the builder.
func (snu *SigningNonceUpdate) Mutation() *SigningNonceMutation {
	return snu.mutation
}

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

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

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

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

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

func (snu *SigningNonceUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(signingnonce.Table, signingnonce.Columns, sqlgraph.NewFieldSpec(signingnonce.FieldID, field.TypeUUID))
	if ps := snu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := snu.mutation.UpdateTime(); ok {
		_spec.SetField(signingnonce.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := snu.mutation.Message(); ok {
		_spec.SetField(signingnonce.FieldMessage, field.TypeBytes, value)
	}
	if snu.mutation.MessageCleared() {
		_spec.ClearField(signingnonce.FieldMessage, field.TypeBytes)
	}
	if value, ok := snu.mutation.RetryFingerprint(); ok {
		_spec.SetField(signingnonce.FieldRetryFingerprint, field.TypeBytes, value)
	}
	if snu.mutation.RetryFingerprintCleared() {
		_spec.ClearField(signingnonce.FieldRetryFingerprint, field.TypeBytes)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, snu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signingnonce.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	snu.mutation.done = true
	return n, nil
}

// SigningNonceUpdateOne is the builder for updating a single SigningNonce entity.
type SigningNonceUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SigningNonceMutation
}

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

// SetMessage sets the "message" field.
func (snuo *SigningNonceUpdateOne) SetMessage(b []byte) *SigningNonceUpdateOne {
	snuo.mutation.SetMessage(b)
	return snuo
}

// ClearMessage clears the value of the "message" field.
func (snuo *SigningNonceUpdateOne) ClearMessage() *SigningNonceUpdateOne {
	snuo.mutation.ClearMessage()
	return snuo
}

// SetRetryFingerprint sets the "retry_fingerprint" field.
func (snuo *SigningNonceUpdateOne) SetRetryFingerprint(b []byte) *SigningNonceUpdateOne {
	snuo.mutation.SetRetryFingerprint(b)
	return snuo
}

// ClearRetryFingerprint clears the value of the "retry_fingerprint" field.
func (snuo *SigningNonceUpdateOne) ClearRetryFingerprint() *SigningNonceUpdateOne {
	snuo.mutation.ClearRetryFingerprint()
	return snuo
}

// Mutation returns the SigningNonceMutation object of the builder.
func (snuo *SigningNonceUpdateOne) Mutation() *SigningNonceMutation {
	return snuo.mutation
}

// Where appends a list predicates to the SigningNonceUpdate builder.
func (snuo *SigningNonceUpdateOne) Where(ps ...predicate.SigningNonce) *SigningNonceUpdateOne {
	snuo.mutation.Where(ps...)
	return snuo
}

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

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

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

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

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

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

func (snuo *SigningNonceUpdateOne) sqlSave(ctx context.Context) (_node *SigningNonce, err error) {
	_spec := sqlgraph.NewUpdateSpec(signingnonce.Table, signingnonce.Columns, sqlgraph.NewFieldSpec(signingnonce.FieldID, field.TypeUUID))
	id, ok := snuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SigningNonce.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := snuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, signingnonce.FieldID)
		for _, f := range fields {
			if !signingnonce.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != signingnonce.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := snuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := snuo.mutation.UpdateTime(); ok {
		_spec.SetField(signingnonce.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := snuo.mutation.Message(); ok {
		_spec.SetField(signingnonce.FieldMessage, field.TypeBytes, value)
	}
	if snuo.mutation.MessageCleared() {
		_spec.ClearField(signingnonce.FieldMessage, field.TypeBytes)
	}
	if value, ok := snuo.mutation.RetryFingerprint(); ok {
		_spec.SetField(signingnonce.FieldRetryFingerprint, field.TypeBytes, value)
	}
	if snuo.mutation.RetryFingerprintCleared() {
		_spec.ClearField(signingnonce.FieldRetryFingerprint, field.TypeBytes)
	}
	_node = &SigningNonce{config: snuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, snuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signingnonce.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	snuo.mutation.done = true
	return _node, nil
}
