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

// SystemNetworkStatsCreate is the builder for creating a SystemNetworkStats entity.
type SystemNetworkStatsCreate struct {
	config
	mutation *SystemNetworkStatsMutation
	hooks    []Hook
}

// SetBytesSent sets the "bytes_sent" field.
func (snsc *SystemNetworkStatsCreate) SetBytesSent(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetBytesSent(u)
	return snsc
}

// SetNillableBytesSent sets the "bytes_sent" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillableBytesSent(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetBytesSent(*u)
	}
	return snsc
}

// SetBytesRecv sets the "bytes_recv" field.
func (snsc *SystemNetworkStatsCreate) SetBytesRecv(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetBytesRecv(u)
	return snsc
}

// SetNillableBytesRecv sets the "bytes_recv" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillableBytesRecv(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetBytesRecv(*u)
	}
	return snsc
}

// SetPacketsSent sets the "packets_sent" field.
func (snsc *SystemNetworkStatsCreate) SetPacketsSent(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPacketsSent(u)
	return snsc
}

// SetNillablePacketsSent sets the "packets_sent" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePacketsSent(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPacketsSent(*u)
	}
	return snsc
}

// SetPacketsRecv sets the "packets_recv" field.
func (snsc *SystemNetworkStatsCreate) SetPacketsRecv(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPacketsRecv(u)
	return snsc
}

// SetNillablePacketsRecv sets the "packets_recv" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePacketsRecv(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPacketsRecv(*u)
	}
	return snsc
}

// SetPeakBytesSentPerSec sets the "peak_bytes_sent_per_sec" field.
func (snsc *SystemNetworkStatsCreate) SetPeakBytesSentPerSec(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPeakBytesSentPerSec(u)
	return snsc
}

// SetNillablePeakBytesSentPerSec sets the "peak_bytes_sent_per_sec" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePeakBytesSentPerSec(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPeakBytesSentPerSec(*u)
	}
	return snsc
}

// SetPeakBytesRecvPerSec sets the "peak_bytes_recv_per_sec" field.
func (snsc *SystemNetworkStatsCreate) SetPeakBytesRecvPerSec(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPeakBytesRecvPerSec(u)
	return snsc
}

// SetNillablePeakBytesRecvPerSec sets the "peak_bytes_recv_per_sec" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePeakBytesRecvPerSec(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPeakBytesRecvPerSec(*u)
	}
	return snsc
}

// SetPeakPacketsSentPerSec sets the "peak_packets_sent_per_sec" field.
func (snsc *SystemNetworkStatsCreate) SetPeakPacketsSentPerSec(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPeakPacketsSentPerSec(u)
	return snsc
}

// SetNillablePeakPacketsSentPerSec sets the "peak_packets_sent_per_sec" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePeakPacketsSentPerSec(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPeakPacketsSentPerSec(*u)
	}
	return snsc
}

// SetPeakPacketsRecvPerSec sets the "peak_packets_recv_per_sec" field.
func (snsc *SystemNetworkStatsCreate) SetPeakPacketsRecvPerSec(u uint64) *SystemNetworkStatsCreate {
	snsc.mutation.SetPeakPacketsRecvPerSec(u)
	return snsc
}

// SetNillablePeakPacketsRecvPerSec sets the "peak_packets_recv_per_sec" field if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillablePeakPacketsRecvPerSec(u *uint64) *SystemNetworkStatsCreate {
	if u != nil {
		snsc.SetPeakPacketsRecvPerSec(*u)
	}
	return snsc
}

// SetNetworkMetricsID sets the "network_metrics" edge to the NetworkMetrics entity by ID.
func (snsc *SystemNetworkStatsCreate) SetNetworkMetricsID(id int) *SystemNetworkStatsCreate {
	snsc.mutation.SetNetworkMetricsID(id)
	return snsc
}

// SetNillableNetworkMetricsID sets the "network_metrics" edge to the NetworkMetrics entity by ID if the given value is not nil.
func (snsc *SystemNetworkStatsCreate) SetNillableNetworkMetricsID(id *int) *SystemNetworkStatsCreate {
	if id != nil {
		snsc = snsc.SetNetworkMetricsID(*id)
	}
	return snsc
}

// SetNetworkMetrics sets the "network_metrics" edge to the NetworkMetrics entity.
func (snsc *SystemNetworkStatsCreate) SetNetworkMetrics(n *NetworkMetrics) *SystemNetworkStatsCreate {
	return snsc.SetNetworkMetricsID(n.ID)
}

// Mutation returns the SystemNetworkStatsMutation object of the builder.
func (snsc *SystemNetworkStatsCreate) Mutation() *SystemNetworkStatsMutation {
	return snsc.mutation
}

// Save creates the SystemNetworkStats in the database.
func (snsc *SystemNetworkStatsCreate) Save(ctx context.Context) (*SystemNetworkStats, error) {
	return withHooks(ctx, snsc.sqlSave, snsc.mutation, snsc.hooks)
}

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

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

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

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

func (snsc *SystemNetworkStatsCreate) sqlSave(ctx context.Context) (*SystemNetworkStats, error) {
	if err := snsc.check(); err != nil {
		return nil, err
	}
	_node, _spec := snsc.createSpec()
	if err := sqlgraph.CreateNode(ctx, snsc.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)
	snsc.mutation.id = &_node.ID
	snsc.mutation.done = true
	return _node, nil
}

func (snsc *SystemNetworkStatsCreate) createSpec() (*SystemNetworkStats, *sqlgraph.CreateSpec) {
	var (
		_node = &SystemNetworkStats{config: snsc.config}
		_spec = sqlgraph.NewCreateSpec(systemnetworkstats.Table, sqlgraph.NewFieldSpec(systemnetworkstats.FieldID, field.TypeInt))
	)
	if value, ok := snsc.mutation.BytesSent(); ok {
		_spec.SetField(systemnetworkstats.FieldBytesSent, field.TypeUint64, value)
		_node.BytesSent = value
	}
	if value, ok := snsc.mutation.BytesRecv(); ok {
		_spec.SetField(systemnetworkstats.FieldBytesRecv, field.TypeUint64, value)
		_node.BytesRecv = value
	}
	if value, ok := snsc.mutation.PacketsSent(); ok {
		_spec.SetField(systemnetworkstats.FieldPacketsSent, field.TypeUint64, value)
		_node.PacketsSent = value
	}
	if value, ok := snsc.mutation.PacketsRecv(); ok {
		_spec.SetField(systemnetworkstats.FieldPacketsRecv, field.TypeUint64, value)
		_node.PacketsRecv = value
	}
	if value, ok := snsc.mutation.PeakBytesSentPerSec(); ok {
		_spec.SetField(systemnetworkstats.FieldPeakBytesSentPerSec, field.TypeUint64, value)
		_node.PeakBytesSentPerSec = value
	}
	if value, ok := snsc.mutation.PeakBytesRecvPerSec(); ok {
		_spec.SetField(systemnetworkstats.FieldPeakBytesRecvPerSec, field.TypeUint64, value)
		_node.PeakBytesRecvPerSec = value
	}
	if value, ok := snsc.mutation.PeakPacketsSentPerSec(); ok {
		_spec.SetField(systemnetworkstats.FieldPeakPacketsSentPerSec, field.TypeUint64, value)
		_node.PeakPacketsSentPerSec = value
	}
	if value, ok := snsc.mutation.PeakPacketsRecvPerSec(); ok {
		_spec.SetField(systemnetworkstats.FieldPeakPacketsRecvPerSec, field.TypeUint64, value)
		_node.PeakPacketsRecvPerSec = value
	}
	if nodes := snsc.mutation.NetworkMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   systemnetworkstats.NetworkMetricsTable,
			Columns: []string{systemnetworkstats.NetworkMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(networkmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.network_metrics_system_network_stats = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// SystemNetworkStatsCreateBulk is the builder for creating many SystemNetworkStats entities in bulk.
type SystemNetworkStatsCreateBulk struct {
	config
	err      error
	builders []*SystemNetworkStatsCreate
}

// Save creates the SystemNetworkStats entities in the database.
func (snscb *SystemNetworkStatsCreateBulk) Save(ctx context.Context) ([]*SystemNetworkStats, error) {
	if snscb.err != nil {
		return nil, snscb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(snscb.builders))
	nodes := make([]*SystemNetworkStats, len(snscb.builders))
	mutators := make([]Mutator, len(snscb.builders))
	for i := range snscb.builders {
		func(i int, root context.Context) {
			builder := snscb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*SystemNetworkStatsMutation)
				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, snscb.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, snscb.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, snscb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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