// 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/signingcommitment"
)

// SigningCommitmentUpdate is the builder for updating SigningCommitment entities.
type SigningCommitmentUpdate struct {
	config
	hooks    []Hook
	mutation *SigningCommitmentMutation
}

// Where appends a list predicates to the SigningCommitmentUpdate builder.
func (scu *SigningCommitmentUpdate) Where(ps ...predicate.SigningCommitment) *SigningCommitmentUpdate {
	scu.mutation.Where(ps...)
	return scu
}

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

// SetStatus sets the "status" field.
func (scu *SigningCommitmentUpdate) SetStatus(scs schematype.SigningCommitmentStatus) *SigningCommitmentUpdate {
	scu.mutation.SetStatus(scs)
	return scu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (scu *SigningCommitmentUpdate) SetNillableStatus(scs *schematype.SigningCommitmentStatus) *SigningCommitmentUpdate {
	if scs != nil {
		scu.SetStatus(*scs)
	}
	return scu
}

// Mutation returns the SigningCommitmentMutation object of the builder.
func (scu *SigningCommitmentUpdate) Mutation() *SigningCommitmentMutation {
	return scu.mutation
}

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

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

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

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

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

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

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

// SigningCommitmentUpdateOne is the builder for updating a single SigningCommitment entity.
type SigningCommitmentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SigningCommitmentMutation
}

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

// SetStatus sets the "status" field.
func (scuo *SigningCommitmentUpdateOne) SetStatus(scs schematype.SigningCommitmentStatus) *SigningCommitmentUpdateOne {
	scuo.mutation.SetStatus(scs)
	return scuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (scuo *SigningCommitmentUpdateOne) SetNillableStatus(scs *schematype.SigningCommitmentStatus) *SigningCommitmentUpdateOne {
	if scs != nil {
		scuo.SetStatus(*scs)
	}
	return scuo
}

// Mutation returns the SigningCommitmentMutation object of the builder.
func (scuo *SigningCommitmentUpdateOne) Mutation() *SigningCommitmentMutation {
	return scuo.mutation
}

// Where appends a list predicates to the SigningCommitmentUpdate builder.
func (scuo *SigningCommitmentUpdateOne) Where(ps ...predicate.SigningCommitment) *SigningCommitmentUpdateOne {
	scuo.mutation.Where(ps...)
	return scuo
}

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

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

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

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

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

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

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

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