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

// TokenPartialRevocationSecretShareUpdate is the builder for updating TokenPartialRevocationSecretShare entities.
type TokenPartialRevocationSecretShareUpdate struct {
	config
	hooks    []Hook
	mutation *TokenPartialRevocationSecretShareMutation
}

// Where appends a list predicates to the TokenPartialRevocationSecretShareUpdate builder.
func (tprssu *TokenPartialRevocationSecretShareUpdate) Where(ps ...predicate.TokenPartialRevocationSecretShare) *TokenPartialRevocationSecretShareUpdate {
	tprssu.mutation.Where(ps...)
	return tprssu
}

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

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

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

// SetSecretShare sets the "secret_share" field.
func (tprssu *TokenPartialRevocationSecretShareUpdate) SetSecretShare(k keys.Private) *TokenPartialRevocationSecretShareUpdate {
	tprssu.mutation.SetSecretShare(k)
	return tprssu
}

// SetNillableSecretShare sets the "secret_share" field if the given value is not nil.
func (tprssu *TokenPartialRevocationSecretShareUpdate) SetNillableSecretShare(k *keys.Private) *TokenPartialRevocationSecretShareUpdate {
	if k != nil {
		tprssu.SetSecretShare(*k)
	}
	return tprssu
}

// SetTokenOutputID sets the "token_output" edge to the TokenOutput entity by ID.
func (tprssu *TokenPartialRevocationSecretShareUpdate) SetTokenOutputID(id uuid.UUID) *TokenPartialRevocationSecretShareUpdate {
	tprssu.mutation.SetTokenOutputID(id)
	return tprssu
}

// SetTokenOutput sets the "token_output" edge to the TokenOutput entity.
func (tprssu *TokenPartialRevocationSecretShareUpdate) SetTokenOutput(t *TokenOutput) *TokenPartialRevocationSecretShareUpdate {
	return tprssu.SetTokenOutputID(t.ID)
}

// Mutation returns the TokenPartialRevocationSecretShareMutation object of the builder.
func (tprssu *TokenPartialRevocationSecretShareUpdate) Mutation() *TokenPartialRevocationSecretShareMutation {
	return tprssu.mutation
}

// ClearTokenOutput clears the "token_output" edge to the TokenOutput entity.
func (tprssu *TokenPartialRevocationSecretShareUpdate) ClearTokenOutput() *TokenPartialRevocationSecretShareUpdate {
	tprssu.mutation.ClearTokenOutput()
	return tprssu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tprssu *TokenPartialRevocationSecretShareUpdate) check() error {
	if tprssu.mutation.TokenOutputCleared() && len(tprssu.mutation.TokenOutputIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenPartialRevocationSecretShare.token_output"`)
	}
	return nil
}

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

// TokenPartialRevocationSecretShareUpdateOne is the builder for updating a single TokenPartialRevocationSecretShare entity.
type TokenPartialRevocationSecretShareUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenPartialRevocationSecretShareMutation
}

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

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

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

// SetSecretShare sets the "secret_share" field.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) SetSecretShare(k keys.Private) *TokenPartialRevocationSecretShareUpdateOne {
	tprssuo.mutation.SetSecretShare(k)
	return tprssuo
}

// SetNillableSecretShare sets the "secret_share" field if the given value is not nil.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) SetNillableSecretShare(k *keys.Private) *TokenPartialRevocationSecretShareUpdateOne {
	if k != nil {
		tprssuo.SetSecretShare(*k)
	}
	return tprssuo
}

// SetTokenOutputID sets the "token_output" edge to the TokenOutput entity by ID.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) SetTokenOutputID(id uuid.UUID) *TokenPartialRevocationSecretShareUpdateOne {
	tprssuo.mutation.SetTokenOutputID(id)
	return tprssuo
}

// SetTokenOutput sets the "token_output" edge to the TokenOutput entity.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) SetTokenOutput(t *TokenOutput) *TokenPartialRevocationSecretShareUpdateOne {
	return tprssuo.SetTokenOutputID(t.ID)
}

// Mutation returns the TokenPartialRevocationSecretShareMutation object of the builder.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) Mutation() *TokenPartialRevocationSecretShareMutation {
	return tprssuo.mutation
}

// ClearTokenOutput clears the "token_output" edge to the TokenOutput entity.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) ClearTokenOutput() *TokenPartialRevocationSecretShareUpdateOne {
	tprssuo.mutation.ClearTokenOutput()
	return tprssuo
}

// Where appends a list predicates to the TokenPartialRevocationSecretShareUpdate builder.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) Where(ps ...predicate.TokenPartialRevocationSecretShare) *TokenPartialRevocationSecretShareUpdateOne {
	tprssuo.mutation.Where(ps...)
	return tprssuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tprssuo *TokenPartialRevocationSecretShareUpdateOne) check() error {
	if tprssuo.mutation.TokenOutputCleared() && len(tprssuo.mutation.TokenOutputIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TokenPartialRevocationSecretShare.token_output"`)
	}
	return nil
}

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