// 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/TSELab/astra/internal/store/ent/predicate"
	"github.com/TSELab/astra/internal/store/ent/step"
)

// StepUpdate is the builder for updating Step entities.
type StepUpdate struct {
	config
	hooks    []Hook
	mutation *StepMutation
}

// Where appends a list predicates to the StepUpdate builder.
func (_u *StepUpdate) Where(ps ...predicate.Step) *StepUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetAstraID sets the "astra_id" field.
func (_u *StepUpdate) SetAstraID(v string) *StepUpdate {
	_u.mutation.SetAstraID(v)
	return _u
}

// SetNillableAstraID sets the "astra_id" field if the given value is not nil.
func (_u *StepUpdate) SetNillableAstraID(v *string) *StepUpdate {
	if v != nil {
		_u.SetAstraID(*v)
	}
	return _u
}

// SetCommand sets the "command" field.
func (_u *StepUpdate) SetCommand(v string) *StepUpdate {
	_u.mutation.SetCommand(v)
	return _u
}

// SetNillableCommand sets the "command" field if the given value is not nil.
func (_u *StepUpdate) SetNillableCommand(v *string) *StepUpdate {
	if v != nil {
		_u.SetCommand(*v)
	}
	return _u
}

// SetEnvironment sets the "environment" field.
func (_u *StepUpdate) SetEnvironment(v map[string]string) *StepUpdate {
	_u.mutation.SetEnvironment(v)
	return _u
}

// SetArch sets the "Arch" field.
func (_u *StepUpdate) SetArch(v string) *StepUpdate {
	_u.mutation.SetArch(v)
	return _u
}

// SetNillableArch sets the "Arch" field if the given value is not nil.
func (_u *StepUpdate) SetNillableArch(v *string) *StepUpdate {
	if v != nil {
		_u.SetArch(*v)
	}
	return _u
}

// SetTimestamp sets the "timestamp" field.
func (_u *StepUpdate) SetTimestamp(v string) *StepUpdate {
	_u.mutation.SetTimestamp(v)
	return _u
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (_u *StepUpdate) SetNillableTimestamp(v *string) *StepUpdate {
	if v != nil {
		_u.SetTimestamp(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *StepUpdate) SetMetadata(v map[string]string) *StepUpdate {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *StepUpdate) ClearMetadata() *StepUpdate {
	_u.mutation.ClearMetadata()
	return _u
}

// SetCompleteness sets the "completeness" field.
func (_u *StepUpdate) SetCompleteness(v step.Completeness) *StepUpdate {
	_u.mutation.SetCompleteness(v)
	return _u
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_u *StepUpdate) SetNillableCompleteness(v *step.Completeness) *StepUpdate {
	if v != nil {
		_u.SetCompleteness(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *StepUpdate) SetCreatedAt(v time.Time) *StepUpdate {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *StepUpdate) SetNillableCreatedAt(v *time.Time) *StepUpdate {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

// Mutation returns the StepMutation object of the builder.
func (_u *StepUpdate) Mutation() *StepMutation {
	return _u.mutation
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *StepUpdate) check() error {
	if v, ok := _u.mutation.AstraID(); ok {
		if err := step.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Step.astra_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Completeness(); ok {
		if err := step.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Step.completeness": %w`, err)}
		}
	}
	return nil
}

func (_u *StepUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(step.Table, step.Columns, sqlgraph.NewFieldSpec(step.FieldID, field.TypeInt))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.AstraID(); ok {
		_spec.SetField(step.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Command(); ok {
		_spec.SetField(step.FieldCommand, field.TypeString, value)
	}
	if value, ok := _u.mutation.Environment(); ok {
		_spec.SetField(step.FieldEnvironment, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.Arch(); ok {
		_spec.SetField(step.FieldArch, field.TypeString, value)
	}
	if value, ok := _u.mutation.Timestamp(); ok {
		_spec.SetField(step.FieldTimestamp, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(step.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(step.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(step.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(step.FieldCreatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{step.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// StepUpdateOne is the builder for updating a single Step entity.
type StepUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *StepMutation
}

// SetAstraID sets the "astra_id" field.
func (_u *StepUpdateOne) SetAstraID(v string) *StepUpdateOne {
	_u.mutation.SetAstraID(v)
	return _u
}

// SetNillableAstraID sets the "astra_id" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableAstraID(v *string) *StepUpdateOne {
	if v != nil {
		_u.SetAstraID(*v)
	}
	return _u
}

// SetCommand sets the "command" field.
func (_u *StepUpdateOne) SetCommand(v string) *StepUpdateOne {
	_u.mutation.SetCommand(v)
	return _u
}

// SetNillableCommand sets the "command" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableCommand(v *string) *StepUpdateOne {
	if v != nil {
		_u.SetCommand(*v)
	}
	return _u
}

// SetEnvironment sets the "environment" field.
func (_u *StepUpdateOne) SetEnvironment(v map[string]string) *StepUpdateOne {
	_u.mutation.SetEnvironment(v)
	return _u
}

// SetArch sets the "Arch" field.
func (_u *StepUpdateOne) SetArch(v string) *StepUpdateOne {
	_u.mutation.SetArch(v)
	return _u
}

// SetNillableArch sets the "Arch" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableArch(v *string) *StepUpdateOne {
	if v != nil {
		_u.SetArch(*v)
	}
	return _u
}

// SetTimestamp sets the "timestamp" field.
func (_u *StepUpdateOne) SetTimestamp(v string) *StepUpdateOne {
	_u.mutation.SetTimestamp(v)
	return _u
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableTimestamp(v *string) *StepUpdateOne {
	if v != nil {
		_u.SetTimestamp(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *StepUpdateOne) SetMetadata(v map[string]string) *StepUpdateOne {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *StepUpdateOne) ClearMetadata() *StepUpdateOne {
	_u.mutation.ClearMetadata()
	return _u
}

// SetCompleteness sets the "completeness" field.
func (_u *StepUpdateOne) SetCompleteness(v step.Completeness) *StepUpdateOne {
	_u.mutation.SetCompleteness(v)
	return _u
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableCompleteness(v *step.Completeness) *StepUpdateOne {
	if v != nil {
		_u.SetCompleteness(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *StepUpdateOne) SetCreatedAt(v time.Time) *StepUpdateOne {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *StepUpdateOne) SetNillableCreatedAt(v *time.Time) *StepUpdateOne {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

// Mutation returns the StepMutation object of the builder.
func (_u *StepUpdateOne) Mutation() *StepMutation {
	return _u.mutation
}

// Where appends a list predicates to the StepUpdate builder.
func (_u *StepUpdateOne) Where(ps ...predicate.Step) *StepUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated Step entity.
func (_u *StepUpdateOne) Save(ctx context.Context) (*Step, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *StepUpdateOne) check() error {
	if v, ok := _u.mutation.AstraID(); ok {
		if err := step.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Step.astra_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Completeness(); ok {
		if err := step.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Step.completeness": %w`, err)}
		}
	}
	return nil
}

func (_u *StepUpdateOne) sqlSave(ctx context.Context) (_node *Step, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(step.Table, step.Columns, sqlgraph.NewFieldSpec(step.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Step.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, step.FieldID)
		for _, f := range fields {
			if !step.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != step.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.AstraID(); ok {
		_spec.SetField(step.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Command(); ok {
		_spec.SetField(step.FieldCommand, field.TypeString, value)
	}
	if value, ok := _u.mutation.Environment(); ok {
		_spec.SetField(step.FieldEnvironment, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.Arch(); ok {
		_spec.SetField(step.FieldArch, field.TypeString, value)
	}
	if value, ok := _u.mutation.Timestamp(); ok {
		_spec.SetField(step.FieldTimestamp, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(step.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(step.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(step.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(step.FieldCreatedAt, field.TypeTime, value)
	}
	_node = &Step{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{step.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
