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

// TargetMetricsUpdate is the builder for updating TargetMetrics entities.
type TargetMetricsUpdate struct {
	config
	hooks    []Hook
	mutation *TargetMetricsMutation
}

// Where appends a list predicates to the TargetMetricsUpdate builder.
func (tmu *TargetMetricsUpdate) Where(ps ...predicate.TargetMetrics) *TargetMetricsUpdate {
	tmu.mutation.Where(ps...)
	return tmu
}

// SetTargetsLoaded sets the "targets_loaded" field.
func (tmu *TargetMetricsUpdate) SetTargetsLoaded(i int64) *TargetMetricsUpdate {
	tmu.mutation.ResetTargetsLoaded()
	tmu.mutation.SetTargetsLoaded(i)
	return tmu
}

// SetNillableTargetsLoaded sets the "targets_loaded" field if the given value is not nil.
func (tmu *TargetMetricsUpdate) SetNillableTargetsLoaded(i *int64) *TargetMetricsUpdate {
	if i != nil {
		tmu.SetTargetsLoaded(*i)
	}
	return tmu
}

// AddTargetsLoaded adds i to the "targets_loaded" field.
func (tmu *TargetMetricsUpdate) AddTargetsLoaded(i int64) *TargetMetricsUpdate {
	tmu.mutation.AddTargetsLoaded(i)
	return tmu
}

// ClearTargetsLoaded clears the value of the "targets_loaded" field.
func (tmu *TargetMetricsUpdate) ClearTargetsLoaded() *TargetMetricsUpdate {
	tmu.mutation.ClearTargetsLoaded()
	return tmu
}

// SetTargetsConfigured sets the "targets_configured" field.
func (tmu *TargetMetricsUpdate) SetTargetsConfigured(i int64) *TargetMetricsUpdate {
	tmu.mutation.ResetTargetsConfigured()
	tmu.mutation.SetTargetsConfigured(i)
	return tmu
}

// SetNillableTargetsConfigured sets the "targets_configured" field if the given value is not nil.
func (tmu *TargetMetricsUpdate) SetNillableTargetsConfigured(i *int64) *TargetMetricsUpdate {
	if i != nil {
		tmu.SetTargetsConfigured(*i)
	}
	return tmu
}

// AddTargetsConfigured adds i to the "targets_configured" field.
func (tmu *TargetMetricsUpdate) AddTargetsConfigured(i int64) *TargetMetricsUpdate {
	tmu.mutation.AddTargetsConfigured(i)
	return tmu
}

// ClearTargetsConfigured clears the value of the "targets_configured" field.
func (tmu *TargetMetricsUpdate) ClearTargetsConfigured() *TargetMetricsUpdate {
	tmu.mutation.ClearTargetsConfigured()
	return tmu
}

// SetTargetsConfiguredNotIncludingAspects sets the "targets_configured_not_including_aspects" field.
func (tmu *TargetMetricsUpdate) SetTargetsConfiguredNotIncludingAspects(i int64) *TargetMetricsUpdate {
	tmu.mutation.ResetTargetsConfiguredNotIncludingAspects()
	tmu.mutation.SetTargetsConfiguredNotIncludingAspects(i)
	return tmu
}

// SetNillableTargetsConfiguredNotIncludingAspects sets the "targets_configured_not_including_aspects" field if the given value is not nil.
func (tmu *TargetMetricsUpdate) SetNillableTargetsConfiguredNotIncludingAspects(i *int64) *TargetMetricsUpdate {
	if i != nil {
		tmu.SetTargetsConfiguredNotIncludingAspects(*i)
	}
	return tmu
}

// AddTargetsConfiguredNotIncludingAspects adds i to the "targets_configured_not_including_aspects" field.
func (tmu *TargetMetricsUpdate) AddTargetsConfiguredNotIncludingAspects(i int64) *TargetMetricsUpdate {
	tmu.mutation.AddTargetsConfiguredNotIncludingAspects(i)
	return tmu
}

// ClearTargetsConfiguredNotIncludingAspects clears the value of the "targets_configured_not_including_aspects" field.
func (tmu *TargetMetricsUpdate) ClearTargetsConfiguredNotIncludingAspects() *TargetMetricsUpdate {
	tmu.mutation.ClearTargetsConfiguredNotIncludingAspects()
	return tmu
}

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

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

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

// Mutation returns the TargetMetricsMutation object of the builder.
func (tmu *TargetMetricsUpdate) Mutation() *TargetMetricsMutation {
	return tmu.mutation
}

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

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

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

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

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

func (tmu *TargetMetricsUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(targetmetrics.Table, targetmetrics.Columns, sqlgraph.NewFieldSpec(targetmetrics.FieldID, field.TypeInt))
	if ps := tmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tmu.mutation.TargetsLoaded(); ok {
		_spec.SetField(targetmetrics.FieldTargetsLoaded, field.TypeInt64, value)
	}
	if value, ok := tmu.mutation.AddedTargetsLoaded(); ok {
		_spec.AddField(targetmetrics.FieldTargetsLoaded, field.TypeInt64, value)
	}
	if tmu.mutation.TargetsLoadedCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsLoaded, field.TypeInt64)
	}
	if value, ok := tmu.mutation.TargetsConfigured(); ok {
		_spec.SetField(targetmetrics.FieldTargetsConfigured, field.TypeInt64, value)
	}
	if value, ok := tmu.mutation.AddedTargetsConfigured(); ok {
		_spec.AddField(targetmetrics.FieldTargetsConfigured, field.TypeInt64, value)
	}
	if tmu.mutation.TargetsConfiguredCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsConfigured, field.TypeInt64)
	}
	if value, ok := tmu.mutation.TargetsConfiguredNotIncludingAspects(); ok {
		_spec.SetField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64, value)
	}
	if value, ok := tmu.mutation.AddedTargetsConfiguredNotIncludingAspects(); ok {
		_spec.AddField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64, value)
	}
	if tmu.mutation.TargetsConfiguredNotIncludingAspectsCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64)
	}
	if tmu.mutation.MetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   targetmetrics.MetricsTable,
			Columns: []string{targetmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tmu.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   targetmetrics.MetricsTable,
			Columns: []string{targetmetrics.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 n, err = sqlgraph.UpdateNodes(ctx, tmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{targetmetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tmu.mutation.done = true
	return n, nil
}

// TargetMetricsUpdateOne is the builder for updating a single TargetMetrics entity.
type TargetMetricsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TargetMetricsMutation
}

// SetTargetsLoaded sets the "targets_loaded" field.
func (tmuo *TargetMetricsUpdateOne) SetTargetsLoaded(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.ResetTargetsLoaded()
	tmuo.mutation.SetTargetsLoaded(i)
	return tmuo
}

// SetNillableTargetsLoaded sets the "targets_loaded" field if the given value is not nil.
func (tmuo *TargetMetricsUpdateOne) SetNillableTargetsLoaded(i *int64) *TargetMetricsUpdateOne {
	if i != nil {
		tmuo.SetTargetsLoaded(*i)
	}
	return tmuo
}

// AddTargetsLoaded adds i to the "targets_loaded" field.
func (tmuo *TargetMetricsUpdateOne) AddTargetsLoaded(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.AddTargetsLoaded(i)
	return tmuo
}

// ClearTargetsLoaded clears the value of the "targets_loaded" field.
func (tmuo *TargetMetricsUpdateOne) ClearTargetsLoaded() *TargetMetricsUpdateOne {
	tmuo.mutation.ClearTargetsLoaded()
	return tmuo
}

// SetTargetsConfigured sets the "targets_configured" field.
func (tmuo *TargetMetricsUpdateOne) SetTargetsConfigured(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.ResetTargetsConfigured()
	tmuo.mutation.SetTargetsConfigured(i)
	return tmuo
}

// SetNillableTargetsConfigured sets the "targets_configured" field if the given value is not nil.
func (tmuo *TargetMetricsUpdateOne) SetNillableTargetsConfigured(i *int64) *TargetMetricsUpdateOne {
	if i != nil {
		tmuo.SetTargetsConfigured(*i)
	}
	return tmuo
}

// AddTargetsConfigured adds i to the "targets_configured" field.
func (tmuo *TargetMetricsUpdateOne) AddTargetsConfigured(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.AddTargetsConfigured(i)
	return tmuo
}

// ClearTargetsConfigured clears the value of the "targets_configured" field.
func (tmuo *TargetMetricsUpdateOne) ClearTargetsConfigured() *TargetMetricsUpdateOne {
	tmuo.mutation.ClearTargetsConfigured()
	return tmuo
}

// SetTargetsConfiguredNotIncludingAspects sets the "targets_configured_not_including_aspects" field.
func (tmuo *TargetMetricsUpdateOne) SetTargetsConfiguredNotIncludingAspects(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.ResetTargetsConfiguredNotIncludingAspects()
	tmuo.mutation.SetTargetsConfiguredNotIncludingAspects(i)
	return tmuo
}

// SetNillableTargetsConfiguredNotIncludingAspects sets the "targets_configured_not_including_aspects" field if the given value is not nil.
func (tmuo *TargetMetricsUpdateOne) SetNillableTargetsConfiguredNotIncludingAspects(i *int64) *TargetMetricsUpdateOne {
	if i != nil {
		tmuo.SetTargetsConfiguredNotIncludingAspects(*i)
	}
	return tmuo
}

// AddTargetsConfiguredNotIncludingAspects adds i to the "targets_configured_not_including_aspects" field.
func (tmuo *TargetMetricsUpdateOne) AddTargetsConfiguredNotIncludingAspects(i int64) *TargetMetricsUpdateOne {
	tmuo.mutation.AddTargetsConfiguredNotIncludingAspects(i)
	return tmuo
}

// ClearTargetsConfiguredNotIncludingAspects clears the value of the "targets_configured_not_including_aspects" field.
func (tmuo *TargetMetricsUpdateOne) ClearTargetsConfiguredNotIncludingAspects() *TargetMetricsUpdateOne {
	tmuo.mutation.ClearTargetsConfiguredNotIncludingAspects()
	return tmuo
}

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

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

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

// Mutation returns the TargetMetricsMutation object of the builder.
func (tmuo *TargetMetricsUpdateOne) Mutation() *TargetMetricsMutation {
	return tmuo.mutation
}

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

// Where appends a list predicates to the TargetMetricsUpdate builder.
func (tmuo *TargetMetricsUpdateOne) Where(ps ...predicate.TargetMetrics) *TargetMetricsUpdateOne {
	tmuo.mutation.Where(ps...)
	return tmuo
}

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

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

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

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

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

func (tmuo *TargetMetricsUpdateOne) sqlSave(ctx context.Context) (_node *TargetMetrics, err error) {
	_spec := sqlgraph.NewUpdateSpec(targetmetrics.Table, targetmetrics.Columns, sqlgraph.NewFieldSpec(targetmetrics.FieldID, field.TypeInt))
	id, ok := tmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TargetMetrics.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, targetmetrics.FieldID)
		for _, f := range fields {
			if !targetmetrics.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != targetmetrics.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tmuo.mutation.TargetsLoaded(); ok {
		_spec.SetField(targetmetrics.FieldTargetsLoaded, field.TypeInt64, value)
	}
	if value, ok := tmuo.mutation.AddedTargetsLoaded(); ok {
		_spec.AddField(targetmetrics.FieldTargetsLoaded, field.TypeInt64, value)
	}
	if tmuo.mutation.TargetsLoadedCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsLoaded, field.TypeInt64)
	}
	if value, ok := tmuo.mutation.TargetsConfigured(); ok {
		_spec.SetField(targetmetrics.FieldTargetsConfigured, field.TypeInt64, value)
	}
	if value, ok := tmuo.mutation.AddedTargetsConfigured(); ok {
		_spec.AddField(targetmetrics.FieldTargetsConfigured, field.TypeInt64, value)
	}
	if tmuo.mutation.TargetsConfiguredCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsConfigured, field.TypeInt64)
	}
	if value, ok := tmuo.mutation.TargetsConfiguredNotIncludingAspects(); ok {
		_spec.SetField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64, value)
	}
	if value, ok := tmuo.mutation.AddedTargetsConfiguredNotIncludingAspects(); ok {
		_spec.AddField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64, value)
	}
	if tmuo.mutation.TargetsConfiguredNotIncludingAspectsCleared() {
		_spec.ClearField(targetmetrics.FieldTargetsConfiguredNotIncludingAspects, field.TypeInt64)
	}
	if tmuo.mutation.MetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   targetmetrics.MetricsTable,
			Columns: []string{targetmetrics.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tmuo.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   targetmetrics.MetricsTable,
			Columns: []string{targetmetrics.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)
	}
	_node = &TargetMetrics{config: tmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{targetmetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tmuo.mutation.done = true
	return _node, nil
}
