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

// SigningKeyshareUpdate is the builder for updating SigningKeyshare entities.
type SigningKeyshareUpdate struct {
	config
	hooks    []Hook
	mutation *SigningKeyshareMutation
}

// Where appends a list predicates to the SigningKeyshareUpdate builder.
func (sku *SigningKeyshareUpdate) Where(ps ...predicate.SigningKeyshare) *SigningKeyshareUpdate {
	sku.mutation.Where(ps...)
	return sku
}

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

// SetStatus sets the "status" field.
func (sku *SigningKeyshareUpdate) SetStatus(sks schematype.SigningKeyshareStatus) *SigningKeyshareUpdate {
	sku.mutation.SetStatus(sks)
	return sku
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (sku *SigningKeyshareUpdate) SetNillableStatus(sks *schematype.SigningKeyshareStatus) *SigningKeyshareUpdate {
	if sks != nil {
		sku.SetStatus(*sks)
	}
	return sku
}

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

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

// SetPublicShares sets the "public_shares" field.
func (sku *SigningKeyshareUpdate) SetPublicShares(m map[string]keys.Public) *SigningKeyshareUpdate {
	sku.mutation.SetPublicShares(m)
	return sku
}

// SetPublicKey sets the "public_key" field.
func (sku *SigningKeyshareUpdate) SetPublicKey(k keys.Public) *SigningKeyshareUpdate {
	sku.mutation.SetPublicKey(k)
	return sku
}

// SetNillablePublicKey sets the "public_key" field if the given value is not nil.
func (sku *SigningKeyshareUpdate) SetNillablePublicKey(k *keys.Public) *SigningKeyshareUpdate {
	if k != nil {
		sku.SetPublicKey(*k)
	}
	return sku
}

// SetMinSigners sets the "min_signers" field.
func (sku *SigningKeyshareUpdate) SetMinSigners(i int32) *SigningKeyshareUpdate {
	sku.mutation.ResetMinSigners()
	sku.mutation.SetMinSigners(i)
	return sku
}

// SetNillableMinSigners sets the "min_signers" field if the given value is not nil.
func (sku *SigningKeyshareUpdate) SetNillableMinSigners(i *int32) *SigningKeyshareUpdate {
	if i != nil {
		sku.SetMinSigners(*i)
	}
	return sku
}

// AddMinSigners adds i to the "min_signers" field.
func (sku *SigningKeyshareUpdate) AddMinSigners(i int32) *SigningKeyshareUpdate {
	sku.mutation.AddMinSigners(i)
	return sku
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (sku *SigningKeyshareUpdate) SetCoordinatorIndex(u uint64) *SigningKeyshareUpdate {
	sku.mutation.ResetCoordinatorIndex()
	sku.mutation.SetCoordinatorIndex(u)
	return sku
}

// SetNillableCoordinatorIndex sets the "coordinator_index" field if the given value is not nil.
func (sku *SigningKeyshareUpdate) SetNillableCoordinatorIndex(u *uint64) *SigningKeyshareUpdate {
	if u != nil {
		sku.SetCoordinatorIndex(*u)
	}
	return sku
}

// AddCoordinatorIndex adds u to the "coordinator_index" field.
func (sku *SigningKeyshareUpdate) AddCoordinatorIndex(u int64) *SigningKeyshareUpdate {
	sku.mutation.AddCoordinatorIndex(u)
	return sku
}

// Mutation returns the SigningKeyshareMutation object of the builder.
func (sku *SigningKeyshareUpdate) Mutation() *SigningKeyshareMutation {
	return sku.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (sku *SigningKeyshareUpdate) check() error {
	if v, ok := sku.mutation.Status(); ok {
		if err := signingkeyshare.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "SigningKeyshare.status": %w`, err)}
		}
	}
	return nil
}

func (sku *SigningKeyshareUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := sku.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(signingkeyshare.Table, signingkeyshare.Columns, sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID))
	if ps := sku.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := sku.mutation.UpdateTime(); ok {
		_spec.SetField(signingkeyshare.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := sku.mutation.Status(); ok {
		_spec.SetField(signingkeyshare.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := sku.mutation.SecretShare(); ok {
		_spec.SetField(signingkeyshare.FieldSecretShare, field.TypeBytes, value)
	}
	if value, ok := sku.mutation.PublicShares(); ok {
		_spec.SetField(signingkeyshare.FieldPublicShares, field.TypeJSON, value)
	}
	if value, ok := sku.mutation.PublicKey(); ok {
		_spec.SetField(signingkeyshare.FieldPublicKey, field.TypeBytes, value)
	}
	if value, ok := sku.mutation.MinSigners(); ok {
		_spec.SetField(signingkeyshare.FieldMinSigners, field.TypeInt32, value)
	}
	if value, ok := sku.mutation.AddedMinSigners(); ok {
		_spec.AddField(signingkeyshare.FieldMinSigners, field.TypeInt32, value)
	}
	if value, ok := sku.mutation.CoordinatorIndex(); ok {
		_spec.SetField(signingkeyshare.FieldCoordinatorIndex, field.TypeUint64, value)
	}
	if value, ok := sku.mutation.AddedCoordinatorIndex(); ok {
		_spec.AddField(signingkeyshare.FieldCoordinatorIndex, field.TypeUint64, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, sku.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signingkeyshare.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	sku.mutation.done = true
	return n, nil
}

// SigningKeyshareUpdateOne is the builder for updating a single SigningKeyshare entity.
type SigningKeyshareUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SigningKeyshareMutation
}

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

// SetStatus sets the "status" field.
func (skuo *SigningKeyshareUpdateOne) SetStatus(sks schematype.SigningKeyshareStatus) *SigningKeyshareUpdateOne {
	skuo.mutation.SetStatus(sks)
	return skuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (skuo *SigningKeyshareUpdateOne) SetNillableStatus(sks *schematype.SigningKeyshareStatus) *SigningKeyshareUpdateOne {
	if sks != nil {
		skuo.SetStatus(*sks)
	}
	return skuo
}

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

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

// SetPublicShares sets the "public_shares" field.
func (skuo *SigningKeyshareUpdateOne) SetPublicShares(m map[string]keys.Public) *SigningKeyshareUpdateOne {
	skuo.mutation.SetPublicShares(m)
	return skuo
}

// SetPublicKey sets the "public_key" field.
func (skuo *SigningKeyshareUpdateOne) SetPublicKey(k keys.Public) *SigningKeyshareUpdateOne {
	skuo.mutation.SetPublicKey(k)
	return skuo
}

// SetNillablePublicKey sets the "public_key" field if the given value is not nil.
func (skuo *SigningKeyshareUpdateOne) SetNillablePublicKey(k *keys.Public) *SigningKeyshareUpdateOne {
	if k != nil {
		skuo.SetPublicKey(*k)
	}
	return skuo
}

// SetMinSigners sets the "min_signers" field.
func (skuo *SigningKeyshareUpdateOne) SetMinSigners(i int32) *SigningKeyshareUpdateOne {
	skuo.mutation.ResetMinSigners()
	skuo.mutation.SetMinSigners(i)
	return skuo
}

// SetNillableMinSigners sets the "min_signers" field if the given value is not nil.
func (skuo *SigningKeyshareUpdateOne) SetNillableMinSigners(i *int32) *SigningKeyshareUpdateOne {
	if i != nil {
		skuo.SetMinSigners(*i)
	}
	return skuo
}

// AddMinSigners adds i to the "min_signers" field.
func (skuo *SigningKeyshareUpdateOne) AddMinSigners(i int32) *SigningKeyshareUpdateOne {
	skuo.mutation.AddMinSigners(i)
	return skuo
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (skuo *SigningKeyshareUpdateOne) SetCoordinatorIndex(u uint64) *SigningKeyshareUpdateOne {
	skuo.mutation.ResetCoordinatorIndex()
	skuo.mutation.SetCoordinatorIndex(u)
	return skuo
}

// SetNillableCoordinatorIndex sets the "coordinator_index" field if the given value is not nil.
func (skuo *SigningKeyshareUpdateOne) SetNillableCoordinatorIndex(u *uint64) *SigningKeyshareUpdateOne {
	if u != nil {
		skuo.SetCoordinatorIndex(*u)
	}
	return skuo
}

// AddCoordinatorIndex adds u to the "coordinator_index" field.
func (skuo *SigningKeyshareUpdateOne) AddCoordinatorIndex(u int64) *SigningKeyshareUpdateOne {
	skuo.mutation.AddCoordinatorIndex(u)
	return skuo
}

// Mutation returns the SigningKeyshareMutation object of the builder.
func (skuo *SigningKeyshareUpdateOne) Mutation() *SigningKeyshareMutation {
	return skuo.mutation
}

// Where appends a list predicates to the SigningKeyshareUpdate builder.
func (skuo *SigningKeyshareUpdateOne) Where(ps ...predicate.SigningKeyshare) *SigningKeyshareUpdateOne {
	skuo.mutation.Where(ps...)
	return skuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (skuo *SigningKeyshareUpdateOne) check() error {
	if v, ok := skuo.mutation.Status(); ok {
		if err := signingkeyshare.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "SigningKeyshare.status": %w`, err)}
		}
	}
	return nil
}

func (skuo *SigningKeyshareUpdateOne) sqlSave(ctx context.Context) (_node *SigningKeyshare, err error) {
	if err := skuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(signingkeyshare.Table, signingkeyshare.Columns, sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID))
	id, ok := skuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SigningKeyshare.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := skuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, signingkeyshare.FieldID)
		for _, f := range fields {
			if !signingkeyshare.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != signingkeyshare.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := skuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := skuo.mutation.UpdateTime(); ok {
		_spec.SetField(signingkeyshare.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := skuo.mutation.Status(); ok {
		_spec.SetField(signingkeyshare.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := skuo.mutation.SecretShare(); ok {
		_spec.SetField(signingkeyshare.FieldSecretShare, field.TypeBytes, value)
	}
	if value, ok := skuo.mutation.PublicShares(); ok {
		_spec.SetField(signingkeyshare.FieldPublicShares, field.TypeJSON, value)
	}
	if value, ok := skuo.mutation.PublicKey(); ok {
		_spec.SetField(signingkeyshare.FieldPublicKey, field.TypeBytes, value)
	}
	if value, ok := skuo.mutation.MinSigners(); ok {
		_spec.SetField(signingkeyshare.FieldMinSigners, field.TypeInt32, value)
	}
	if value, ok := skuo.mutation.AddedMinSigners(); ok {
		_spec.AddField(signingkeyshare.FieldMinSigners, field.TypeInt32, value)
	}
	if value, ok := skuo.mutation.CoordinatorIndex(); ok {
		_spec.SetField(signingkeyshare.FieldCoordinatorIndex, field.TypeUint64, value)
	}
	if value, ok := skuo.mutation.AddedCoordinatorIndex(); ok {
		_spec.AddField(signingkeyshare.FieldCoordinatorIndex, field.TypeUint64, value)
	}
	_node = &SigningKeyshare{config: skuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, skuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signingkeyshare.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	skuo.mutation.done = true
	return _node, nil
}
