// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/actioncachestatistics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/missdetail"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
)

// MissDetailUpdate is the builder for updating MissDetail entities.
type MissDetailUpdate struct {
	config
	hooks    []Hook
	mutation *MissDetailMutation
}

// Where appends a list predicates to the MissDetailUpdate builder.
func (mdu *MissDetailUpdate) Where(ps ...predicate.MissDetail) *MissDetailUpdate {
	mdu.mutation.Where(ps...)
	return mdu
}

// SetReason sets the "reason" field.
func (mdu *MissDetailUpdate) SetReason(m missdetail.Reason) *MissDetailUpdate {
	mdu.mutation.SetReason(m)
	return mdu
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (mdu *MissDetailUpdate) SetNillableReason(m *missdetail.Reason) *MissDetailUpdate {
	if m != nil {
		mdu.SetReason(*m)
	}
	return mdu
}

// ClearReason clears the value of the "reason" field.
func (mdu *MissDetailUpdate) ClearReason() *MissDetailUpdate {
	mdu.mutation.ClearReason()
	return mdu
}

// SetCount sets the "count" field.
func (mdu *MissDetailUpdate) SetCount(i int32) *MissDetailUpdate {
	mdu.mutation.ResetCount()
	mdu.mutation.SetCount(i)
	return mdu
}

// SetNillableCount sets the "count" field if the given value is not nil.
func (mdu *MissDetailUpdate) SetNillableCount(i *int32) *MissDetailUpdate {
	if i != nil {
		mdu.SetCount(*i)
	}
	return mdu
}

// AddCount adds i to the "count" field.
func (mdu *MissDetailUpdate) AddCount(i int32) *MissDetailUpdate {
	mdu.mutation.AddCount(i)
	return mdu
}

// ClearCount clears the value of the "count" field.
func (mdu *MissDetailUpdate) ClearCount() *MissDetailUpdate {
	mdu.mutation.ClearCount()
	return mdu
}

// SetActionCacheStatisticsID sets the "action_cache_statistics" edge to the ActionCacheStatistics entity by ID.
func (mdu *MissDetailUpdate) SetActionCacheStatisticsID(id int) *MissDetailUpdate {
	mdu.mutation.SetActionCacheStatisticsID(id)
	return mdu
}

// SetNillableActionCacheStatisticsID sets the "action_cache_statistics" edge to the ActionCacheStatistics entity by ID if the given value is not nil.
func (mdu *MissDetailUpdate) SetNillableActionCacheStatisticsID(id *int) *MissDetailUpdate {
	if id != nil {
		mdu = mdu.SetActionCacheStatisticsID(*id)
	}
	return mdu
}

// SetActionCacheStatistics sets the "action_cache_statistics" edge to the ActionCacheStatistics entity.
func (mdu *MissDetailUpdate) SetActionCacheStatistics(a *ActionCacheStatistics) *MissDetailUpdate {
	return mdu.SetActionCacheStatisticsID(a.ID)
}

// Mutation returns the MissDetailMutation object of the builder.
func (mdu *MissDetailUpdate) Mutation() *MissDetailMutation {
	return mdu.mutation
}

// ClearActionCacheStatistics clears the "action_cache_statistics" edge to the ActionCacheStatistics entity.
func (mdu *MissDetailUpdate) ClearActionCacheStatistics() *MissDetailUpdate {
	mdu.mutation.ClearActionCacheStatistics()
	return mdu
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (mdu *MissDetailUpdate) check() error {
	if v, ok := mdu.mutation.Reason(); ok {
		if err := missdetail.ReasonValidator(v); err != nil {
			return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "MissDetail.reason": %w`, err)}
		}
	}
	return nil
}

func (mdu *MissDetailUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := mdu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(missdetail.Table, missdetail.Columns, sqlgraph.NewFieldSpec(missdetail.FieldID, field.TypeInt))
	if ps := mdu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mdu.mutation.Reason(); ok {
		_spec.SetField(missdetail.FieldReason, field.TypeEnum, value)
	}
	if mdu.mutation.ReasonCleared() {
		_spec.ClearField(missdetail.FieldReason, field.TypeEnum)
	}
	if value, ok := mdu.mutation.Count(); ok {
		_spec.SetField(missdetail.FieldCount, field.TypeInt32, value)
	}
	if value, ok := mdu.mutation.AddedCount(); ok {
		_spec.AddField(missdetail.FieldCount, field.TypeInt32, value)
	}
	if mdu.mutation.CountCleared() {
		_spec.ClearField(missdetail.FieldCount, field.TypeInt32)
	}
	if mdu.mutation.ActionCacheStatisticsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   missdetail.ActionCacheStatisticsTable,
			Columns: []string{missdetail.ActionCacheStatisticsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actioncachestatistics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mdu.mutation.ActionCacheStatisticsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   missdetail.ActionCacheStatisticsTable,
			Columns: []string{missdetail.ActionCacheStatisticsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actioncachestatistics.FieldID, field.TypeInt),
			},
		}
		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, mdu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{missdetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	mdu.mutation.done = true
	return n, nil
}

// MissDetailUpdateOne is the builder for updating a single MissDetail entity.
type MissDetailUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MissDetailMutation
}

// SetReason sets the "reason" field.
func (mduo *MissDetailUpdateOne) SetReason(m missdetail.Reason) *MissDetailUpdateOne {
	mduo.mutation.SetReason(m)
	return mduo
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (mduo *MissDetailUpdateOne) SetNillableReason(m *missdetail.Reason) *MissDetailUpdateOne {
	if m != nil {
		mduo.SetReason(*m)
	}
	return mduo
}

// ClearReason clears the value of the "reason" field.
func (mduo *MissDetailUpdateOne) ClearReason() *MissDetailUpdateOne {
	mduo.mutation.ClearReason()
	return mduo
}

// SetCount sets the "count" field.
func (mduo *MissDetailUpdateOne) SetCount(i int32) *MissDetailUpdateOne {
	mduo.mutation.ResetCount()
	mduo.mutation.SetCount(i)
	return mduo
}

// SetNillableCount sets the "count" field if the given value is not nil.
func (mduo *MissDetailUpdateOne) SetNillableCount(i *int32) *MissDetailUpdateOne {
	if i != nil {
		mduo.SetCount(*i)
	}
	return mduo
}

// AddCount adds i to the "count" field.
func (mduo *MissDetailUpdateOne) AddCount(i int32) *MissDetailUpdateOne {
	mduo.mutation.AddCount(i)
	return mduo
}

// ClearCount clears the value of the "count" field.
func (mduo *MissDetailUpdateOne) ClearCount() *MissDetailUpdateOne {
	mduo.mutation.ClearCount()
	return mduo
}

// SetActionCacheStatisticsID sets the "action_cache_statistics" edge to the ActionCacheStatistics entity by ID.
func (mduo *MissDetailUpdateOne) SetActionCacheStatisticsID(id int) *MissDetailUpdateOne {
	mduo.mutation.SetActionCacheStatisticsID(id)
	return mduo
}

// SetNillableActionCacheStatisticsID sets the "action_cache_statistics" edge to the ActionCacheStatistics entity by ID if the given value is not nil.
func (mduo *MissDetailUpdateOne) SetNillableActionCacheStatisticsID(id *int) *MissDetailUpdateOne {
	if id != nil {
		mduo = mduo.SetActionCacheStatisticsID(*id)
	}
	return mduo
}

// SetActionCacheStatistics sets the "action_cache_statistics" edge to the ActionCacheStatistics entity.
func (mduo *MissDetailUpdateOne) SetActionCacheStatistics(a *ActionCacheStatistics) *MissDetailUpdateOne {
	return mduo.SetActionCacheStatisticsID(a.ID)
}

// Mutation returns the MissDetailMutation object of the builder.
func (mduo *MissDetailUpdateOne) Mutation() *MissDetailMutation {
	return mduo.mutation
}

// ClearActionCacheStatistics clears the "action_cache_statistics" edge to the ActionCacheStatistics entity.
func (mduo *MissDetailUpdateOne) ClearActionCacheStatistics() *MissDetailUpdateOne {
	mduo.mutation.ClearActionCacheStatistics()
	return mduo
}

// Where appends a list predicates to the MissDetailUpdate builder.
func (mduo *MissDetailUpdateOne) Where(ps ...predicate.MissDetail) *MissDetailUpdateOne {
	mduo.mutation.Where(ps...)
	return mduo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (mduo *MissDetailUpdateOne) check() error {
	if v, ok := mduo.mutation.Reason(); ok {
		if err := missdetail.ReasonValidator(v); err != nil {
			return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "MissDetail.reason": %w`, err)}
		}
	}
	return nil
}

func (mduo *MissDetailUpdateOne) sqlSave(ctx context.Context) (_node *MissDetail, err error) {
	if err := mduo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(missdetail.Table, missdetail.Columns, sqlgraph.NewFieldSpec(missdetail.FieldID, field.TypeInt))
	id, ok := mduo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "MissDetail.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := mduo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, missdetail.FieldID)
		for _, f := range fields {
			if !missdetail.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != missdetail.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := mduo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mduo.mutation.Reason(); ok {
		_spec.SetField(missdetail.FieldReason, field.TypeEnum, value)
	}
	if mduo.mutation.ReasonCleared() {
		_spec.ClearField(missdetail.FieldReason, field.TypeEnum)
	}
	if value, ok := mduo.mutation.Count(); ok {
		_spec.SetField(missdetail.FieldCount, field.TypeInt32, value)
	}
	if value, ok := mduo.mutation.AddedCount(); ok {
		_spec.AddField(missdetail.FieldCount, field.TypeInt32, value)
	}
	if mduo.mutation.CountCleared() {
		_spec.ClearField(missdetail.FieldCount, field.TypeInt32)
	}
	if mduo.mutation.ActionCacheStatisticsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   missdetail.ActionCacheStatisticsTable,
			Columns: []string{missdetail.ActionCacheStatisticsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actioncachestatistics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mduo.mutation.ActionCacheStatisticsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   missdetail.ActionCacheStatisticsTable,
			Columns: []string{missdetail.ActionCacheStatisticsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actioncachestatistics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &MissDetail{config: mduo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, mduo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{missdetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	mduo.mutation.done = true
	return _node, nil
}
