// 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/crowdsecurity/crowdsec/pkg/database/ent/alert"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)

// DecisionUpdate is the builder for updating Decision entities.
type DecisionUpdate struct {
	config
	hooks    []Hook
	mutation *DecisionMutation
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_u *DecisionUpdate) SetUpdatedAt(v time.Time) *DecisionUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetUntil sets the "until" field.
func (_u *DecisionUpdate) SetUntil(v time.Time) *DecisionUpdate {
	_u.mutation.SetUntil(v)
	return _u
}

// SetNillableUntil sets the "until" field if the given value is not nil.
func (_u *DecisionUpdate) SetNillableUntil(v *time.Time) *DecisionUpdate {
	if v != nil {
		_u.SetUntil(*v)
	}
	return _u
}

// ClearUntil clears the value of the "until" field.
func (_u *DecisionUpdate) ClearUntil() *DecisionUpdate {
	_u.mutation.ClearUntil()
	return _u
}

// SetAlertDecisions sets the "alert_decisions" field.
func (_u *DecisionUpdate) SetAlertDecisions(v int) *DecisionUpdate {
	_u.mutation.SetAlertDecisions(v)
	return _u
}

// SetNillableAlertDecisions sets the "alert_decisions" field if the given value is not nil.
func (_u *DecisionUpdate) SetNillableAlertDecisions(v *int) *DecisionUpdate {
	if v != nil {
		_u.SetAlertDecisions(*v)
	}
	return _u
}

// ClearAlertDecisions clears the value of the "alert_decisions" field.
func (_u *DecisionUpdate) ClearAlertDecisions() *DecisionUpdate {
	_u.mutation.ClearAlertDecisions()
	return _u
}

// SetOwnerID sets the "owner" edge to the Alert entity by ID.
func (_u *DecisionUpdate) SetOwnerID(id int) *DecisionUpdate {
	_u.mutation.SetOwnerID(id)
	return _u
}

// SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
func (_u *DecisionUpdate) SetNillableOwnerID(id *int) *DecisionUpdate {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

// SetOwner sets the "owner" edge to the Alert entity.
func (_u *DecisionUpdate) SetOwner(v *Alert) *DecisionUpdate {
	return _u.SetOwnerID(v.ID)
}

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

// ClearOwner clears the "owner" edge to the Alert entity.
func (_u *DecisionUpdate) ClearOwner() *DecisionUpdate {
	_u.mutation.ClearOwner()
	return _u
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *DecisionUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := decision.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *DecisionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(decision.Table, decision.Columns, sqlgraph.NewFieldSpec(decision.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.UpdatedAt(); ok {
		_spec.SetField(decision.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Until(); ok {
		_spec.SetField(decision.FieldUntil, field.TypeTime, value)
	}
	if _u.mutation.UntilCleared() {
		_spec.ClearField(decision.FieldUntil, field.TypeTime)
	}
	if _u.mutation.StartIPCleared() {
		_spec.ClearField(decision.FieldStartIP, field.TypeInt64)
	}
	if _u.mutation.EndIPCleared() {
		_spec.ClearField(decision.FieldEndIP, field.TypeInt64)
	}
	if _u.mutation.StartSuffixCleared() {
		_spec.ClearField(decision.FieldStartSuffix, field.TypeInt64)
	}
	if _u.mutation.EndSuffixCleared() {
		_spec.ClearField(decision.FieldEndSuffix, field.TypeInt64)
	}
	if _u.mutation.IPSizeCleared() {
		_spec.ClearField(decision.FieldIPSize, field.TypeInt64)
	}
	if _u.mutation.UUIDCleared() {
		_spec.ClearField(decision.FieldUUID, field.TypeString)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   decision.OwnerTable,
			Columns: []string{decision.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   decision.OwnerTable,
			Columns: []string{decision.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{decision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// DecisionUpdateOne is the builder for updating a single Decision entity.
type DecisionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DecisionMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *DecisionUpdateOne) SetUpdatedAt(v time.Time) *DecisionUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetUntil sets the "until" field.
func (_u *DecisionUpdateOne) SetUntil(v time.Time) *DecisionUpdateOne {
	_u.mutation.SetUntil(v)
	return _u
}

// SetNillableUntil sets the "until" field if the given value is not nil.
func (_u *DecisionUpdateOne) SetNillableUntil(v *time.Time) *DecisionUpdateOne {
	if v != nil {
		_u.SetUntil(*v)
	}
	return _u
}

// ClearUntil clears the value of the "until" field.
func (_u *DecisionUpdateOne) ClearUntil() *DecisionUpdateOne {
	_u.mutation.ClearUntil()
	return _u
}

// SetAlertDecisions sets the "alert_decisions" field.
func (_u *DecisionUpdateOne) SetAlertDecisions(v int) *DecisionUpdateOne {
	_u.mutation.SetAlertDecisions(v)
	return _u
}

// SetNillableAlertDecisions sets the "alert_decisions" field if the given value is not nil.
func (_u *DecisionUpdateOne) SetNillableAlertDecisions(v *int) *DecisionUpdateOne {
	if v != nil {
		_u.SetAlertDecisions(*v)
	}
	return _u
}

// ClearAlertDecisions clears the value of the "alert_decisions" field.
func (_u *DecisionUpdateOne) ClearAlertDecisions() *DecisionUpdateOne {
	_u.mutation.ClearAlertDecisions()
	return _u
}

// SetOwnerID sets the "owner" edge to the Alert entity by ID.
func (_u *DecisionUpdateOne) SetOwnerID(id int) *DecisionUpdateOne {
	_u.mutation.SetOwnerID(id)
	return _u
}

// SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
func (_u *DecisionUpdateOne) SetNillableOwnerID(id *int) *DecisionUpdateOne {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

// SetOwner sets the "owner" edge to the Alert entity.
func (_u *DecisionUpdateOne) SetOwner(v *Alert) *DecisionUpdateOne {
	return _u.SetOwnerID(v.ID)
}

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

// ClearOwner clears the "owner" edge to the Alert entity.
func (_u *DecisionUpdateOne) ClearOwner() *DecisionUpdateOne {
	_u.mutation.ClearOwner()
	return _u
}

// Where appends a list predicates to the DecisionUpdate builder.
func (_u *DecisionUpdateOne) Where(ps ...predicate.Decision) *DecisionUpdateOne {
	_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 *DecisionUpdateOne) Select(field string, fields ...string) *DecisionUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *DecisionUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := decision.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *DecisionUpdateOne) sqlSave(ctx context.Context) (_node *Decision, err error) {
	_spec := sqlgraph.NewUpdateSpec(decision.Table, decision.Columns, sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Decision.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, decision.FieldID)
		for _, f := range fields {
			if !decision.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != decision.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.UpdatedAt(); ok {
		_spec.SetField(decision.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Until(); ok {
		_spec.SetField(decision.FieldUntil, field.TypeTime, value)
	}
	if _u.mutation.UntilCleared() {
		_spec.ClearField(decision.FieldUntil, field.TypeTime)
	}
	if _u.mutation.StartIPCleared() {
		_spec.ClearField(decision.FieldStartIP, field.TypeInt64)
	}
	if _u.mutation.EndIPCleared() {
		_spec.ClearField(decision.FieldEndIP, field.TypeInt64)
	}
	if _u.mutation.StartSuffixCleared() {
		_spec.ClearField(decision.FieldStartSuffix, field.TypeInt64)
	}
	if _u.mutation.EndSuffixCleared() {
		_spec.ClearField(decision.FieldEndSuffix, field.TypeInt64)
	}
	if _u.mutation.IPSizeCleared() {
		_spec.ClearField(decision.FieldIPSize, field.TypeInt64)
	}
	if _u.mutation.UUIDCleared() {
		_spec.ClearField(decision.FieldUUID, field.TypeString)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   decision.OwnerTable,
			Columns: []string{decision.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   decision.OwnerTable,
			Columns: []string{decision.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Decision{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{decision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
