// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

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

// NetworkMetricsCreate is the builder for creating a NetworkMetrics entity.
type NetworkMetricsCreate struct {
	config
	mutation *NetworkMetricsMutation
	hooks    []Hook
}

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

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

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

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

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

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

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

// Save creates the NetworkMetrics in the database.
func (nmc *NetworkMetricsCreate) Save(ctx context.Context) (*NetworkMetrics, error) {
	return withHooks(ctx, nmc.sqlSave, nmc.mutation, nmc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (nmc *NetworkMetricsCreate) SaveX(ctx context.Context) *NetworkMetrics {
	v, err := nmc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// check runs all checks and user-defined validators on the builder.
func (nmc *NetworkMetricsCreate) check() error {
	return nil
}

func (nmc *NetworkMetricsCreate) sqlSave(ctx context.Context) (*NetworkMetrics, error) {
	if err := nmc.check(); err != nil {
		return nil, err
	}
	_node, _spec := nmc.createSpec()
	if err := sqlgraph.CreateNode(ctx, nmc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int(id)
	nmc.mutation.id = &_node.ID
	nmc.mutation.done = true
	return _node, nil
}

func (nmc *NetworkMetricsCreate) createSpec() (*NetworkMetrics, *sqlgraph.CreateSpec) {
	var (
		_node = &NetworkMetrics{config: nmc.config}
		_spec = sqlgraph.NewCreateSpec(networkmetrics.Table, sqlgraph.NewFieldSpec(networkmetrics.FieldID, field.TypeInt))
	)
	if nodes := nmc.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)
		}
		_node.metrics_network_metrics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := nmc.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 = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// NetworkMetricsCreateBulk is the builder for creating many NetworkMetrics entities in bulk.
type NetworkMetricsCreateBulk struct {
	config
	err      error
	builders []*NetworkMetricsCreate
}

// Save creates the NetworkMetrics entities in the database.
func (nmcb *NetworkMetricsCreateBulk) Save(ctx context.Context) ([]*NetworkMetrics, error) {
	if nmcb.err != nil {
		return nil, nmcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(nmcb.builders))
	nodes := make([]*NetworkMetrics, len(nmcb.builders))
	mutators := make([]Mutator, len(nmcb.builders))
	for i := range nmcb.builders {
		func(i int, root context.Context) {
			builder := nmcb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*NetworkMetricsMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, nmcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, nmcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, nmcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (nmcb *NetworkMetricsCreateBulk) SaveX(ctx context.Context) []*NetworkMetrics {
	v, err := nmcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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