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

// AllowListUpdate is the builder for updating AllowList entities.
type AllowListUpdate struct {
	config
	hooks    []Hook
	mutation *AllowListMutation
}

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

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

// SetName sets the "name" field.
func (_u *AllowListUpdate) SetName(v string) *AllowListUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *AllowListUpdate) SetNillableName(v *string) *AllowListUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetFromConsole sets the "from_console" field.
func (_u *AllowListUpdate) SetFromConsole(v bool) *AllowListUpdate {
	_u.mutation.SetFromConsole(v)
	return _u
}

// SetNillableFromConsole sets the "from_console" field if the given value is not nil.
func (_u *AllowListUpdate) SetNillableFromConsole(v *bool) *AllowListUpdate {
	if v != nil {
		_u.SetFromConsole(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *AllowListUpdate) SetDescription(v string) *AllowListUpdate {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *AllowListUpdate) SetNillableDescription(v *string) *AllowListUpdate {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *AllowListUpdate) ClearDescription() *AllowListUpdate {
	_u.mutation.ClearDescription()
	return _u
}

// AddAllowlistItemIDs adds the "allowlist_items" edge to the AllowListItem entity by IDs.
func (_u *AllowListUpdate) AddAllowlistItemIDs(ids ...int) *AllowListUpdate {
	_u.mutation.AddAllowlistItemIDs(ids...)
	return _u
}

// AddAllowlistItems adds the "allowlist_items" edges to the AllowListItem entity.
func (_u *AllowListUpdate) AddAllowlistItems(v ...*AllowListItem) *AllowListUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowlistItemIDs(ids...)
}

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

// ClearAllowlistItems clears all "allowlist_items" edges to the AllowListItem entity.
func (_u *AllowListUpdate) ClearAllowlistItems() *AllowListUpdate {
	_u.mutation.ClearAllowlistItems()
	return _u
}

// RemoveAllowlistItemIDs removes the "allowlist_items" edge to AllowListItem entities by IDs.
func (_u *AllowListUpdate) RemoveAllowlistItemIDs(ids ...int) *AllowListUpdate {
	_u.mutation.RemoveAllowlistItemIDs(ids...)
	return _u
}

// RemoveAllowlistItems removes "allowlist_items" edges to AllowListItem entities.
func (_u *AllowListUpdate) RemoveAllowlistItems(v ...*AllowListItem) *AllowListUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowlistItemIDs(ids...)
}

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

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

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

func (_u *AllowListUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(allowlist.Table, allowlist.Columns, sqlgraph.NewFieldSpec(allowlist.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(allowlist.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(allowlist.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.FromConsole(); ok {
		_spec.SetField(allowlist.FieldFromConsole, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(allowlist.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(allowlist.FieldDescription, field.TypeString)
	}
	if _u.mutation.AllowlistIDCleared() {
		_spec.ClearField(allowlist.FieldAllowlistID, field.TypeString)
	}
	if _u.mutation.AllowlistItemsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowlistItemsIDs(); len(nodes) > 0 && !_u.mutation.AllowlistItemsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.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.AllowlistItemsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.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{allowlist.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AllowListUpdateOne is the builder for updating a single AllowList entity.
type AllowListUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AllowListMutation
}

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

// SetName sets the "name" field.
func (_u *AllowListUpdateOne) SetName(v string) *AllowListUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *AllowListUpdateOne) SetNillableName(v *string) *AllowListUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetFromConsole sets the "from_console" field.
func (_u *AllowListUpdateOne) SetFromConsole(v bool) *AllowListUpdateOne {
	_u.mutation.SetFromConsole(v)
	return _u
}

// SetNillableFromConsole sets the "from_console" field if the given value is not nil.
func (_u *AllowListUpdateOne) SetNillableFromConsole(v *bool) *AllowListUpdateOne {
	if v != nil {
		_u.SetFromConsole(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *AllowListUpdateOne) SetDescription(v string) *AllowListUpdateOne {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *AllowListUpdateOne) SetNillableDescription(v *string) *AllowListUpdateOne {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *AllowListUpdateOne) ClearDescription() *AllowListUpdateOne {
	_u.mutation.ClearDescription()
	return _u
}

// AddAllowlistItemIDs adds the "allowlist_items" edge to the AllowListItem entity by IDs.
func (_u *AllowListUpdateOne) AddAllowlistItemIDs(ids ...int) *AllowListUpdateOne {
	_u.mutation.AddAllowlistItemIDs(ids...)
	return _u
}

// AddAllowlistItems adds the "allowlist_items" edges to the AllowListItem entity.
func (_u *AllowListUpdateOne) AddAllowlistItems(v ...*AllowListItem) *AllowListUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowlistItemIDs(ids...)
}

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

// ClearAllowlistItems clears all "allowlist_items" edges to the AllowListItem entity.
func (_u *AllowListUpdateOne) ClearAllowlistItems() *AllowListUpdateOne {
	_u.mutation.ClearAllowlistItems()
	return _u
}

// RemoveAllowlistItemIDs removes the "allowlist_items" edge to AllowListItem entities by IDs.
func (_u *AllowListUpdateOne) RemoveAllowlistItemIDs(ids ...int) *AllowListUpdateOne {
	_u.mutation.RemoveAllowlistItemIDs(ids...)
	return _u
}

// RemoveAllowlistItems removes "allowlist_items" edges to AllowListItem entities.
func (_u *AllowListUpdateOne) RemoveAllowlistItems(v ...*AllowListItem) *AllowListUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowlistItemIDs(ids...)
}

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

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

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

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

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

func (_u *AllowListUpdateOne) sqlSave(ctx context.Context) (_node *AllowList, err error) {
	_spec := sqlgraph.NewUpdateSpec(allowlist.Table, allowlist.Columns, sqlgraph.NewFieldSpec(allowlist.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AllowList.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, allowlist.FieldID)
		for _, f := range fields {
			if !allowlist.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != allowlist.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(allowlist.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(allowlist.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.FromConsole(); ok {
		_spec.SetField(allowlist.FieldFromConsole, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(allowlist.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(allowlist.FieldDescription, field.TypeString)
	}
	if _u.mutation.AllowlistIDCleared() {
		_spec.ClearField(allowlist.FieldAllowlistID, field.TypeString)
	}
	if _u.mutation.AllowlistItemsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowlistItemsIDs(); len(nodes) > 0 && !_u.mutation.AllowlistItemsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.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.AllowlistItemsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   allowlist.AllowlistItemsTable,
			Columns: allowlist.AllowlistItemsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(allowlistitem.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &AllowList{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{allowlist.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
