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

// MetaUpdate is the builder for updating Meta entities.
type MetaUpdate struct {
	config
	hooks    []Hook
	mutation *MetaMutation
}

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

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

// SetAlertMetas sets the "alert_metas" field.
func (_u *MetaUpdate) SetAlertMetas(v int) *MetaUpdate {
	_u.mutation.SetAlertMetas(v)
	return _u
}

// SetNillableAlertMetas sets the "alert_metas" field if the given value is not nil.
func (_u *MetaUpdate) SetNillableAlertMetas(v *int) *MetaUpdate {
	if v != nil {
		_u.SetAlertMetas(*v)
	}
	return _u
}

// ClearAlertMetas clears the value of the "alert_metas" field.
func (_u *MetaUpdate) ClearAlertMetas() *MetaUpdate {
	_u.mutation.ClearAlertMetas()
	return _u
}

// SetOwnerID sets the "owner" edge to the Alert entity by ID.
func (_u *MetaUpdate) SetOwnerID(id int) *MetaUpdate {
	_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 *MetaUpdate) SetNillableOwnerID(id *int) *MetaUpdate {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

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

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

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

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

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

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

func (_u *MetaUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(meta.Table, meta.Columns, sqlgraph.NewFieldSpec(meta.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(meta.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   meta.OwnerTable,
			Columns: []string{meta.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:   meta.OwnerTable,
			Columns: []string{meta.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{meta.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// MetaUpdateOne is the builder for updating a single Meta entity.
type MetaUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MetaMutation
}

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

// SetAlertMetas sets the "alert_metas" field.
func (_u *MetaUpdateOne) SetAlertMetas(v int) *MetaUpdateOne {
	_u.mutation.SetAlertMetas(v)
	return _u
}

// SetNillableAlertMetas sets the "alert_metas" field if the given value is not nil.
func (_u *MetaUpdateOne) SetNillableAlertMetas(v *int) *MetaUpdateOne {
	if v != nil {
		_u.SetAlertMetas(*v)
	}
	return _u
}

// ClearAlertMetas clears the value of the "alert_metas" field.
func (_u *MetaUpdateOne) ClearAlertMetas() *MetaUpdateOne {
	_u.mutation.ClearAlertMetas()
	return _u
}

// SetOwnerID sets the "owner" edge to the Alert entity by ID.
func (_u *MetaUpdateOne) SetOwnerID(id int) *MetaUpdateOne {
	_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 *MetaUpdateOne) SetNillableOwnerID(id *int) *MetaUpdateOne {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

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

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

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

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

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

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

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

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

func (_u *MetaUpdateOne) sqlSave(ctx context.Context) (_node *Meta, err error) {
	_spec := sqlgraph.NewUpdateSpec(meta.Table, meta.Columns, sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Meta.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, meta.FieldID)
		for _, f := range fields {
			if !meta.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != meta.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(meta.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   meta.OwnerTable,
			Columns: []string{meta.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:   meta.OwnerTable,
			Columns: []string{meta.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 = &Meta{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{meta.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
