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

// AllowListItemUpdate is the builder for updating AllowListItem entities.
type AllowListItemUpdate struct {
	config
	hooks    []Hook
	mutation *AllowListItemMutation
}

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

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

// SetExpiresAt sets the "expires_at" field.
func (_u *AllowListItemUpdate) SetExpiresAt(v time.Time) *AllowListItemUpdate {
	_u.mutation.SetExpiresAt(v)
	return _u
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *AllowListItemUpdate) SetNillableExpiresAt(v *time.Time) *AllowListItemUpdate {
	if v != nil {
		_u.SetExpiresAt(*v)
	}
	return _u
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (_u *AllowListItemUpdate) ClearExpiresAt() *AllowListItemUpdate {
	_u.mutation.ClearExpiresAt()
	return _u
}

// AddAllowlistIDs adds the "allowlist" edge to the AllowList entity by IDs.
func (_u *AllowListItemUpdate) AddAllowlistIDs(ids ...int) *AllowListItemUpdate {
	_u.mutation.AddAllowlistIDs(ids...)
	return _u
}

// AddAllowlist adds the "allowlist" edges to the AllowList entity.
func (_u *AllowListItemUpdate) AddAllowlist(v ...*AllowList) *AllowListItemUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowlistIDs(ids...)
}

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

// ClearAllowlist clears all "allowlist" edges to the AllowList entity.
func (_u *AllowListItemUpdate) ClearAllowlist() *AllowListItemUpdate {
	_u.mutation.ClearAllowlist()
	return _u
}

// RemoveAllowlistIDs removes the "allowlist" edge to AllowList entities by IDs.
func (_u *AllowListItemUpdate) RemoveAllowlistIDs(ids ...int) *AllowListItemUpdate {
	_u.mutation.RemoveAllowlistIDs(ids...)
	return _u
}

// RemoveAllowlist removes "allowlist" edges to AllowList entities.
func (_u *AllowListItemUpdate) RemoveAllowlist(v ...*AllowList) *AllowListItemUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowlistIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *AllowListItemUpdate) 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 *AllowListItemUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AllowListItemUpdate) 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 *AllowListItemUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := allowlistitem.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *AllowListItemUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(allowlistitem.Table, allowlistitem.Columns, sqlgraph.NewFieldSpec(allowlistitem.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(allowlistitem.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.ExpiresAt(); ok {
		_spec.SetField(allowlistitem.FieldExpiresAt, field.TypeTime, value)
	}
	if _u.mutation.ExpiresAtCleared() {
		_spec.ClearField(allowlistitem.FieldExpiresAt, field.TypeTime)
	}
	if _u.mutation.CommentCleared() {
		_spec.ClearField(allowlistitem.FieldComment, field.TypeString)
	}
	if _u.mutation.StartIPCleared() {
		_spec.ClearField(allowlistitem.FieldStartIP, field.TypeInt64)
	}
	if _u.mutation.EndIPCleared() {
		_spec.ClearField(allowlistitem.FieldEndIP, field.TypeInt64)
	}
	if _u.mutation.StartSuffixCleared() {
		_spec.ClearField(allowlistitem.FieldStartSuffix, field.TypeInt64)
	}
	if _u.mutation.EndSuffixCleared() {
		_spec.ClearField(allowlistitem.FieldEndSuffix, field.TypeInt64)
	}
	if _u.mutation.IPSizeCleared() {
		_spec.ClearField(allowlistitem.FieldIPSize, field.TypeInt64)
	}
	if _u.mutation.AllowlistCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowlistIDs(); len(nodes) > 0 && !_u.mutation.AllowlistCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AllowlistIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.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{allowlistitem.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AllowListItemUpdateOne is the builder for updating a single AllowListItem entity.
type AllowListItemUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AllowListItemMutation
}

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

// SetExpiresAt sets the "expires_at" field.
func (_u *AllowListItemUpdateOne) SetExpiresAt(v time.Time) *AllowListItemUpdateOne {
	_u.mutation.SetExpiresAt(v)
	return _u
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *AllowListItemUpdateOne) SetNillableExpiresAt(v *time.Time) *AllowListItemUpdateOne {
	if v != nil {
		_u.SetExpiresAt(*v)
	}
	return _u
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (_u *AllowListItemUpdateOne) ClearExpiresAt() *AllowListItemUpdateOne {
	_u.mutation.ClearExpiresAt()
	return _u
}

// AddAllowlistIDs adds the "allowlist" edge to the AllowList entity by IDs.
func (_u *AllowListItemUpdateOne) AddAllowlistIDs(ids ...int) *AllowListItemUpdateOne {
	_u.mutation.AddAllowlistIDs(ids...)
	return _u
}

// AddAllowlist adds the "allowlist" edges to the AllowList entity.
func (_u *AllowListItemUpdateOne) AddAllowlist(v ...*AllowList) *AllowListItemUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowlistIDs(ids...)
}

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

// ClearAllowlist clears all "allowlist" edges to the AllowList entity.
func (_u *AllowListItemUpdateOne) ClearAllowlist() *AllowListItemUpdateOne {
	_u.mutation.ClearAllowlist()
	return _u
}

// RemoveAllowlistIDs removes the "allowlist" edge to AllowList entities by IDs.
func (_u *AllowListItemUpdateOne) RemoveAllowlistIDs(ids ...int) *AllowListItemUpdateOne {
	_u.mutation.RemoveAllowlistIDs(ids...)
	return _u
}

// RemoveAllowlist removes "allowlist" edges to AllowList entities.
func (_u *AllowListItemUpdateOne) RemoveAllowlist(v ...*AllowList) *AllowListItemUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowlistIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AllowListItemUpdateOne) 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 *AllowListItemUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := allowlistitem.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *AllowListItemUpdateOne) sqlSave(ctx context.Context) (_node *AllowListItem, err error) {
	_spec := sqlgraph.NewUpdateSpec(allowlistitem.Table, allowlistitem.Columns, sqlgraph.NewFieldSpec(allowlistitem.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AllowListItem.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, allowlistitem.FieldID)
		for _, f := range fields {
			if !allowlistitem.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != allowlistitem.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(allowlistitem.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.ExpiresAt(); ok {
		_spec.SetField(allowlistitem.FieldExpiresAt, field.TypeTime, value)
	}
	if _u.mutation.ExpiresAtCleared() {
		_spec.ClearField(allowlistitem.FieldExpiresAt, field.TypeTime)
	}
	if _u.mutation.CommentCleared() {
		_spec.ClearField(allowlistitem.FieldComment, field.TypeString)
	}
	if _u.mutation.StartIPCleared() {
		_spec.ClearField(allowlistitem.FieldStartIP, field.TypeInt64)
	}
	if _u.mutation.EndIPCleared() {
		_spec.ClearField(allowlistitem.FieldEndIP, field.TypeInt64)
	}
	if _u.mutation.StartSuffixCleared() {
		_spec.ClearField(allowlistitem.FieldStartSuffix, field.TypeInt64)
	}
	if _u.mutation.EndSuffixCleared() {
		_spec.ClearField(allowlistitem.FieldEndSuffix, field.TypeInt64)
	}
	if _u.mutation.IPSizeCleared() {
		_spec.ClearField(allowlistitem.FieldIPSize, field.TypeInt64)
	}
	if _u.mutation.AllowlistCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowlistIDs(); len(nodes) > 0 && !_u.mutation.AllowlistCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AllowlistIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   allowlistitem.AllowlistTable,
			Columns: allowlistitem.AllowlistPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &AllowListItem{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{allowlistitem.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
