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

// TokenFreezeUpdate is the builder for updating TokenFreeze entities.
type TokenFreezeUpdate struct {
	config
	hooks    []Hook
	mutation *TokenFreezeMutation
}

// Where appends a list predicates to the TokenFreezeUpdate builder.
func (tfu *TokenFreezeUpdate) Where(ps ...predicate.TokenFreeze) *TokenFreezeUpdate {
	tfu.mutation.Where(ps...)
	return tfu
}

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

// SetStatus sets the "status" field.
func (tfu *TokenFreezeUpdate) SetStatus(sfs schematype.TokenFreezeStatus) *TokenFreezeUpdate {
	tfu.mutation.SetStatus(sfs)
	return tfu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tfu *TokenFreezeUpdate) SetNillableStatus(sfs *schematype.TokenFreezeStatus) *TokenFreezeUpdate {
	if sfs != nil {
		tfu.SetStatus(*sfs)
	}
	return tfu
}

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (tfu *TokenFreezeUpdate) SetWalletProvidedThawTimestamp(u uint64) *TokenFreezeUpdate {
	tfu.mutation.ResetWalletProvidedThawTimestamp()
	tfu.mutation.SetWalletProvidedThawTimestamp(u)
	return tfu
}

// SetNillableWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field if the given value is not nil.
func (tfu *TokenFreezeUpdate) SetNillableWalletProvidedThawTimestamp(u *uint64) *TokenFreezeUpdate {
	if u != nil {
		tfu.SetWalletProvidedThawTimestamp(*u)
	}
	return tfu
}

// AddWalletProvidedThawTimestamp adds u to the "wallet_provided_thaw_timestamp" field.
func (tfu *TokenFreezeUpdate) AddWalletProvidedThawTimestamp(u int64) *TokenFreezeUpdate {
	tfu.mutation.AddWalletProvidedThawTimestamp(u)
	return tfu
}

// ClearWalletProvidedThawTimestamp clears the value of the "wallet_provided_thaw_timestamp" field.
func (tfu *TokenFreezeUpdate) ClearWalletProvidedThawTimestamp() *TokenFreezeUpdate {
	tfu.mutation.ClearWalletProvidedThawTimestamp()
	return tfu
}

// Mutation returns the TokenFreezeMutation object of the builder.
func (tfu *TokenFreezeUpdate) Mutation() *TokenFreezeMutation {
	return tfu.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tfu *TokenFreezeUpdate) check() error {
	if v, ok := tfu.mutation.Status(); ok {
		if err := tokenfreeze.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TokenFreeze.status": %w`, err)}
		}
	}
	if tfu.mutation.TokenCreateCleared() && len(tfu.mutation.TokenCreateIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenFreeze.token_create"`)
	}
	return nil
}

func (tfu *TokenFreezeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tfu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(tokenfreeze.Table, tokenfreeze.Columns, sqlgraph.NewFieldSpec(tokenfreeze.FieldID, field.TypeUUID))
	if ps := tfu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tfu.mutation.UpdateTime(); ok {
		_spec.SetField(tokenfreeze.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tfu.mutation.Status(); ok {
		_spec.SetField(tokenfreeze.FieldStatus, field.TypeEnum, value)
	}
	if tfu.mutation.TokenPublicKeyCleared() {
		_spec.ClearField(tokenfreeze.FieldTokenPublicKey, field.TypeBytes)
	}
	if value, ok := tfu.mutation.WalletProvidedThawTimestamp(); ok {
		_spec.SetField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64, value)
	}
	if value, ok := tfu.mutation.AddedWalletProvidedThawTimestamp(); ok {
		_spec.AddField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64, value)
	}
	if tfu.mutation.WalletProvidedThawTimestampCleared() {
		_spec.ClearField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, tfu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokenfreeze.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tfu.mutation.done = true
	return n, nil
}

// TokenFreezeUpdateOne is the builder for updating a single TokenFreeze entity.
type TokenFreezeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenFreezeMutation
}

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

// SetStatus sets the "status" field.
func (tfuo *TokenFreezeUpdateOne) SetStatus(sfs schematype.TokenFreezeStatus) *TokenFreezeUpdateOne {
	tfuo.mutation.SetStatus(sfs)
	return tfuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tfuo *TokenFreezeUpdateOne) SetNillableStatus(sfs *schematype.TokenFreezeStatus) *TokenFreezeUpdateOne {
	if sfs != nil {
		tfuo.SetStatus(*sfs)
	}
	return tfuo
}

// SetWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field.
func (tfuo *TokenFreezeUpdateOne) SetWalletProvidedThawTimestamp(u uint64) *TokenFreezeUpdateOne {
	tfuo.mutation.ResetWalletProvidedThawTimestamp()
	tfuo.mutation.SetWalletProvidedThawTimestamp(u)
	return tfuo
}

// SetNillableWalletProvidedThawTimestamp sets the "wallet_provided_thaw_timestamp" field if the given value is not nil.
func (tfuo *TokenFreezeUpdateOne) SetNillableWalletProvidedThawTimestamp(u *uint64) *TokenFreezeUpdateOne {
	if u != nil {
		tfuo.SetWalletProvidedThawTimestamp(*u)
	}
	return tfuo
}

// AddWalletProvidedThawTimestamp adds u to the "wallet_provided_thaw_timestamp" field.
func (tfuo *TokenFreezeUpdateOne) AddWalletProvidedThawTimestamp(u int64) *TokenFreezeUpdateOne {
	tfuo.mutation.AddWalletProvidedThawTimestamp(u)
	return tfuo
}

// ClearWalletProvidedThawTimestamp clears the value of the "wallet_provided_thaw_timestamp" field.
func (tfuo *TokenFreezeUpdateOne) ClearWalletProvidedThawTimestamp() *TokenFreezeUpdateOne {
	tfuo.mutation.ClearWalletProvidedThawTimestamp()
	return tfuo
}

// Mutation returns the TokenFreezeMutation object of the builder.
func (tfuo *TokenFreezeUpdateOne) Mutation() *TokenFreezeMutation {
	return tfuo.mutation
}

// Where appends a list predicates to the TokenFreezeUpdate builder.
func (tfuo *TokenFreezeUpdateOne) Where(ps ...predicate.TokenFreeze) *TokenFreezeUpdateOne {
	tfuo.mutation.Where(ps...)
	return tfuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tfuo *TokenFreezeUpdateOne) check() error {
	if v, ok := tfuo.mutation.Status(); ok {
		if err := tokenfreeze.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TokenFreeze.status": %w`, err)}
		}
	}
	if tfuo.mutation.TokenCreateCleared() && len(tfuo.mutation.TokenCreateIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenFreeze.token_create"`)
	}
	return nil
}

func (tfuo *TokenFreezeUpdateOne) sqlSave(ctx context.Context) (_node *TokenFreeze, err error) {
	if err := tfuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(tokenfreeze.Table, tokenfreeze.Columns, sqlgraph.NewFieldSpec(tokenfreeze.FieldID, field.TypeUUID))
	id, ok := tfuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TokenFreeze.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tfuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, tokenfreeze.FieldID)
		for _, f := range fields {
			if !tokenfreeze.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != tokenfreeze.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tfuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tfuo.mutation.UpdateTime(); ok {
		_spec.SetField(tokenfreeze.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tfuo.mutation.Status(); ok {
		_spec.SetField(tokenfreeze.FieldStatus, field.TypeEnum, value)
	}
	if tfuo.mutation.TokenPublicKeyCleared() {
		_spec.ClearField(tokenfreeze.FieldTokenPublicKey, field.TypeBytes)
	}
	if value, ok := tfuo.mutation.WalletProvidedThawTimestamp(); ok {
		_spec.SetField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64, value)
	}
	if value, ok := tfuo.mutation.AddedWalletProvidedThawTimestamp(); ok {
		_spec.AddField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64, value)
	}
	if tfuo.mutation.WalletProvidedThawTimestampCleared() {
		_spec.ClearField(tokenfreeze.FieldWalletProvidedThawTimestamp, field.TypeUint64)
	}
	_node = &TokenFreeze{config: tfuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tfuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tokenfreeze.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tfuo.mutation.done = true
	return _node, nil
}
