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

// EntityDkgKeyUpdate is the builder for updating EntityDkgKey entities.
type EntityDkgKeyUpdate struct {
	config
	hooks    []Hook
	mutation *EntityDkgKeyMutation
}

// Where appends a list predicates to the EntityDkgKeyUpdate builder.
func (edku *EntityDkgKeyUpdate) Where(ps ...predicate.EntityDkgKey) *EntityDkgKeyUpdate {
	edku.mutation.Where(ps...)
	return edku
}

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

// Mutation returns the EntityDkgKeyMutation object of the builder.
func (edku *EntityDkgKeyUpdate) Mutation() *EntityDkgKeyMutation {
	return edku.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (edku *EntityDkgKeyUpdate) check() error {
	if edku.mutation.SigningKeyshareCleared() && len(edku.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "EntityDkgKey.signing_keyshare"`)
	}
	return nil
}

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

// EntityDkgKeyUpdateOne is the builder for updating a single EntityDkgKey entity.
type EntityDkgKeyUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *EntityDkgKeyMutation
}

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

// Mutation returns the EntityDkgKeyMutation object of the builder.
func (edkuo *EntityDkgKeyUpdateOne) Mutation() *EntityDkgKeyMutation {
	return edkuo.mutation
}

// Where appends a list predicates to the EntityDkgKeyUpdate builder.
func (edkuo *EntityDkgKeyUpdateOne) Where(ps ...predicate.EntityDkgKey) *EntityDkgKeyUpdateOne {
	edkuo.mutation.Where(ps...)
	return edkuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (edkuo *EntityDkgKeyUpdateOne) check() error {
	if edkuo.mutation.SigningKeyshareCleared() && len(edkuo.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "EntityDkgKey.signing_keyshare"`)
	}
	return nil
}

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