// 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/metrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/networkmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/systemnetworkstats"
)

// NetworkMetricsUpdate is the builder for updating NetworkMetrics entities.
type NetworkMetricsUpdate struct {
	config
	hooks    []Hook
	mutation *NetworkMetricsMutation
}

// Where appends a list predicates to the NetworkMetricsUpdate builder.
func (nmu *NetworkMetricsUpdate) Where(ps ...predicate.NetworkMetrics) *NetworkMetricsUpdate {
	nmu.mutation.Where(ps...)
	return nmu
}

// SetMetricsID sets the "metrics" edge to the Metrics entity by ID.
func (nmu *NetworkMetricsUpdate) SetMetricsID(id int) *NetworkMetricsUpdate {
	nmu.mutation.SetMetricsID(id)
	return nmu
}

// SetNillableMetricsID sets the "metrics" edge to the Metrics entity by ID if the given value is not nil.
func (nmu *NetworkMetricsUpdate) SetNillableMetricsID(id *int) *NetworkMetricsUpdate {
	if id != nil {
		nmu = nmu.SetMetricsID(*id)
	}
	return nmu
}

// SetMetrics sets the "metrics" edge to the Metrics entity.
func (nmu *NetworkMetricsUpdate) SetMetrics(m *Metrics) *NetworkMetricsUpdate {
	return nmu.SetMetricsID(m.ID)
}

// SetSystemNetworkStatsID sets the "system_network_stats" edge to the SystemNetworkStats entity by ID.
func (nmu *NetworkMetricsUpdate) SetSystemNetworkStatsID(id int) *NetworkMetricsUpdate {
	nmu.mutation.SetSystemNetworkStatsID(id)
	return nmu
}

// SetNillableSystemNetworkStatsID sets the "system_network_stats" edge to the SystemNetworkStats entity by ID if the given value is not nil.
func (nmu *NetworkMetricsUpdate) SetNillableSystemNetworkStatsID(id *int) *NetworkMetricsUpdate {
	if id != nil {
		nmu = nmu.SetSystemNetworkStatsID(*id)
	}
	return nmu
}

// SetSystemNetworkStats sets the "system_network_stats" edge to the SystemNetworkStats entity.
func (nmu *NetworkMetricsUpdate) SetSystemNetworkStats(s *SystemNetworkStats) *NetworkMetricsUpdate {
	return nmu.SetSystemNetworkStatsID(s.ID)
}

// Mutation returns the NetworkMetricsMutation object of the builder.
func (nmu *NetworkMetricsUpdate) Mutation() *NetworkMetricsMutation {
	return nmu.mutation
}

// ClearMetrics clears the "metrics" edge to the Metrics entity.
func (nmu *NetworkMetricsUpdate) ClearMetrics() *NetworkMetricsUpdate {
	nmu.mutation.ClearMetrics()
	return nmu
}

// ClearSystemNetworkStats clears the "system_network_stats" edge to the SystemNetworkStats entity.
func (nmu *NetworkMetricsUpdate) ClearSystemNetworkStats() *NetworkMetricsUpdate {
	nmu.mutation.ClearSystemNetworkStats()
	return nmu
}

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

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

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

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

func (nmu *NetworkMetricsUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(networkmetrics.Table, networkmetrics.Columns, sqlgraph.NewFieldSpec(networkmetrics.FieldID, field.TypeInt))
	if ps := nmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if nmu.mutation.MetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   networkmetrics.MetricsTable,
			Columns: []string{networkmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nmu.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   networkmetrics.MetricsTable,
			Columns: []string{networkmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if nmu.mutation.SystemNetworkStatsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   networkmetrics.SystemNetworkStatsTable,
			Columns: []string{networkmetrics.SystemNetworkStatsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(systemnetworkstats.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nmu.mutation.SystemNetworkStatsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   networkmetrics.SystemNetworkStatsTable,
			Columns: []string{networkmetrics.SystemNetworkStatsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(systemnetworkstats.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, nmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{networkmetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	nmu.mutation.done = true
	return n, nil
}

// NetworkMetricsUpdateOne is the builder for updating a single NetworkMetrics entity.
type NetworkMetricsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *NetworkMetricsMutation
}

// SetMetricsID sets the "metrics" edge to the Metrics entity by ID.
func (nmuo *NetworkMetricsUpdateOne) SetMetricsID(id int) *NetworkMetricsUpdateOne {
	nmuo.mutation.SetMetricsID(id)
	return nmuo
}

// SetNillableMetricsID sets the "metrics" edge to the Metrics entity by ID if the given value is not nil.
func (nmuo *NetworkMetricsUpdateOne) SetNillableMetricsID(id *int) *NetworkMetricsUpdateOne {
	if id != nil {
		nmuo = nmuo.SetMetricsID(*id)
	}
	return nmuo
}

// SetMetrics sets the "metrics" edge to the Metrics entity.
func (nmuo *NetworkMetricsUpdateOne) SetMetrics(m *Metrics) *NetworkMetricsUpdateOne {
	return nmuo.SetMetricsID(m.ID)
}

// SetSystemNetworkStatsID sets the "system_network_stats" edge to the SystemNetworkStats entity by ID.
func (nmuo *NetworkMetricsUpdateOne) SetSystemNetworkStatsID(id int) *NetworkMetricsUpdateOne {
	nmuo.mutation.SetSystemNetworkStatsID(id)
	return nmuo
}

// SetNillableSystemNetworkStatsID sets the "system_network_stats" edge to the SystemNetworkStats entity by ID if the given value is not nil.
func (nmuo *NetworkMetricsUpdateOne) SetNillableSystemNetworkStatsID(id *int) *NetworkMetricsUpdateOne {
	if id != nil {
		nmuo = nmuo.SetSystemNetworkStatsID(*id)
	}
	return nmuo
}

// SetSystemNetworkStats sets the "system_network_stats" edge to the SystemNetworkStats entity.
func (nmuo *NetworkMetricsUpdateOne) SetSystemNetworkStats(s *SystemNetworkStats) *NetworkMetricsUpdateOne {
	return nmuo.SetSystemNetworkStatsID(s.ID)
}

// Mutation returns the NetworkMetricsMutation object of the builder.
func (nmuo *NetworkMetricsUpdateOne) Mutation() *NetworkMetricsMutation {
	return nmuo.mutation
}

// ClearMetrics clears the "metrics" edge to the Metrics entity.
func (nmuo *NetworkMetricsUpdateOne) ClearMetrics() *NetworkMetricsUpdateOne {
	nmuo.mutation.ClearMetrics()
	return nmuo
}

// ClearSystemNetworkStats clears the "system_network_stats" edge to the SystemNetworkStats entity.
func (nmuo *NetworkMetricsUpdateOne) ClearSystemNetworkStats() *NetworkMetricsUpdateOne {
	nmuo.mutation.ClearSystemNetworkStats()
	return nmuo
}

// Where appends a list predicates to the NetworkMetricsUpdate builder.
func (nmuo *NetworkMetricsUpdateOne) Where(ps ...predicate.NetworkMetrics) *NetworkMetricsUpdateOne {
	nmuo.mutation.Where(ps...)
	return nmuo
}

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

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

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

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

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

func (nmuo *NetworkMetricsUpdateOne) sqlSave(ctx context.Context) (_node *NetworkMetrics, err error) {
	_spec := sqlgraph.NewUpdateSpec(networkmetrics.Table, networkmetrics.Columns, sqlgraph.NewFieldSpec(networkmetrics.FieldID, field.TypeInt))
	id, ok := nmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "NetworkMetrics.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := nmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, networkmetrics.FieldID)
		for _, f := range fields {
			if !networkmetrics.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != networkmetrics.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := nmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if nmuo.mutation.MetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   networkmetrics.MetricsTable,
			Columns: []string{networkmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nmuo.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   networkmetrics.MetricsTable,
			Columns: []string{networkmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if nmuo.mutation.SystemNetworkStatsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   networkmetrics.SystemNetworkStatsTable,
			Columns: []string{networkmetrics.SystemNetworkStatsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(systemnetworkstats.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nmuo.mutation.SystemNetworkStatsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   networkmetrics.SystemNetworkStatsTable,
			Columns: []string{networkmetrics.SystemNetworkStatsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(systemnetworkstats.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &NetworkMetrics{config: nmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, nmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{networkmetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	nmuo.mutation.done = true
	return _node, nil
}
