// 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/actionsummary"
	"github.com/buildbarn/bb-portal/ent/gen/ent/artifactmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/buildgraphmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/cumulativemetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/dynamicexecutionmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/memorymetrics"
	"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/packagemetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/timingmetrics"
)

// MetricsCreate is the builder for creating a Metrics entity.
type MetricsCreate struct {
	config
	mutation *MetricsMutation
	hooks    []Hook
}

// SetBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID.
func (mc *MetricsCreate) SetBazelInvocationID(id int) *MetricsCreate {
	mc.mutation.SetBazelInvocationID(id)
	return mc
}

// SetNillableBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableBazelInvocationID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetBazelInvocationID(*id)
	}
	return mc
}

// SetBazelInvocation sets the "bazel_invocation" edge to the BazelInvocation entity.
func (mc *MetricsCreate) SetBazelInvocation(b *BazelInvocation) *MetricsCreate {
	return mc.SetBazelInvocationID(b.ID)
}

// SetActionSummaryID sets the "action_summary" edge to the ActionSummary entity by ID.
func (mc *MetricsCreate) SetActionSummaryID(id int) *MetricsCreate {
	mc.mutation.SetActionSummaryID(id)
	return mc
}

// SetNillableActionSummaryID sets the "action_summary" edge to the ActionSummary entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableActionSummaryID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetActionSummaryID(*id)
	}
	return mc
}

// SetActionSummary sets the "action_summary" edge to the ActionSummary entity.
func (mc *MetricsCreate) SetActionSummary(a *ActionSummary) *MetricsCreate {
	return mc.SetActionSummaryID(a.ID)
}

// SetMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID.
func (mc *MetricsCreate) SetMemoryMetricsID(id int) *MetricsCreate {
	mc.mutation.SetMemoryMetricsID(id)
	return mc
}

// SetNillableMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableMemoryMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetMemoryMetricsID(*id)
	}
	return mc
}

// SetMemoryMetrics sets the "memory_metrics" edge to the MemoryMetrics entity.
func (mc *MetricsCreate) SetMemoryMetrics(m *MemoryMetrics) *MetricsCreate {
	return mc.SetMemoryMetricsID(m.ID)
}

// SetTargetMetricsID sets the "target_metrics" edge to the TargetMetrics entity by ID.
func (mc *MetricsCreate) SetTargetMetricsID(id int) *MetricsCreate {
	mc.mutation.SetTargetMetricsID(id)
	return mc
}

// SetNillableTargetMetricsID sets the "target_metrics" edge to the TargetMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableTargetMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetTargetMetricsID(*id)
	}
	return mc
}

// SetTargetMetrics sets the "target_metrics" edge to the TargetMetrics entity.
func (mc *MetricsCreate) SetTargetMetrics(t *TargetMetrics) *MetricsCreate {
	return mc.SetTargetMetricsID(t.ID)
}

// SetPackageMetricsID sets the "package_metrics" edge to the PackageMetrics entity by ID.
func (mc *MetricsCreate) SetPackageMetricsID(id int) *MetricsCreate {
	mc.mutation.SetPackageMetricsID(id)
	return mc
}

// SetNillablePackageMetricsID sets the "package_metrics" edge to the PackageMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillablePackageMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetPackageMetricsID(*id)
	}
	return mc
}

// SetPackageMetrics sets the "package_metrics" edge to the PackageMetrics entity.
func (mc *MetricsCreate) SetPackageMetrics(p *PackageMetrics) *MetricsCreate {
	return mc.SetPackageMetricsID(p.ID)
}

// SetTimingMetricsID sets the "timing_metrics" edge to the TimingMetrics entity by ID.
func (mc *MetricsCreate) SetTimingMetricsID(id int) *MetricsCreate {
	mc.mutation.SetTimingMetricsID(id)
	return mc
}

// SetNillableTimingMetricsID sets the "timing_metrics" edge to the TimingMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableTimingMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetTimingMetricsID(*id)
	}
	return mc
}

// SetTimingMetrics sets the "timing_metrics" edge to the TimingMetrics entity.
func (mc *MetricsCreate) SetTimingMetrics(t *TimingMetrics) *MetricsCreate {
	return mc.SetTimingMetricsID(t.ID)
}

// SetCumulativeMetricsID sets the "cumulative_metrics" edge to the CumulativeMetrics entity by ID.
func (mc *MetricsCreate) SetCumulativeMetricsID(id int) *MetricsCreate {
	mc.mutation.SetCumulativeMetricsID(id)
	return mc
}

// SetNillableCumulativeMetricsID sets the "cumulative_metrics" edge to the CumulativeMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableCumulativeMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetCumulativeMetricsID(*id)
	}
	return mc
}

// SetCumulativeMetrics sets the "cumulative_metrics" edge to the CumulativeMetrics entity.
func (mc *MetricsCreate) SetCumulativeMetrics(c *CumulativeMetrics) *MetricsCreate {
	return mc.SetCumulativeMetricsID(c.ID)
}

// SetArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID.
func (mc *MetricsCreate) SetArtifactMetricsID(id int) *MetricsCreate {
	mc.mutation.SetArtifactMetricsID(id)
	return mc
}

// SetNillableArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableArtifactMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetArtifactMetricsID(*id)
	}
	return mc
}

// SetArtifactMetrics sets the "artifact_metrics" edge to the ArtifactMetrics entity.
func (mc *MetricsCreate) SetArtifactMetrics(a *ArtifactMetrics) *MetricsCreate {
	return mc.SetArtifactMetricsID(a.ID)
}

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

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

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

// SetDynamicExecutionMetricsID sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity by ID.
func (mc *MetricsCreate) SetDynamicExecutionMetricsID(id int) *MetricsCreate {
	mc.mutation.SetDynamicExecutionMetricsID(id)
	return mc
}

// SetNillableDynamicExecutionMetricsID sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableDynamicExecutionMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetDynamicExecutionMetricsID(*id)
	}
	return mc
}

// SetDynamicExecutionMetrics sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity.
func (mc *MetricsCreate) SetDynamicExecutionMetrics(d *DynamicExecutionMetrics) *MetricsCreate {
	return mc.SetDynamicExecutionMetricsID(d.ID)
}

// SetBuildGraphMetricsID sets the "build_graph_metrics" edge to the BuildGraphMetrics entity by ID.
func (mc *MetricsCreate) SetBuildGraphMetricsID(id int) *MetricsCreate {
	mc.mutation.SetBuildGraphMetricsID(id)
	return mc
}

// SetNillableBuildGraphMetricsID sets the "build_graph_metrics" edge to the BuildGraphMetrics entity by ID if the given value is not nil.
func (mc *MetricsCreate) SetNillableBuildGraphMetricsID(id *int) *MetricsCreate {
	if id != nil {
		mc = mc.SetBuildGraphMetricsID(*id)
	}
	return mc
}

// SetBuildGraphMetrics sets the "build_graph_metrics" edge to the BuildGraphMetrics entity.
func (mc *MetricsCreate) SetBuildGraphMetrics(b *BuildGraphMetrics) *MetricsCreate {
	return mc.SetBuildGraphMetricsID(b.ID)
}

// Mutation returns the MetricsMutation object of the builder.
func (mc *MetricsCreate) Mutation() *MetricsMutation {
	return mc.mutation
}

// Save creates the Metrics in the database.
func (mc *MetricsCreate) Save(ctx context.Context) (*Metrics, error) {
	return withHooks(ctx, mc.sqlSave, mc.mutation, mc.hooks)
}

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

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

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

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

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

func (mc *MetricsCreate) createSpec() (*Metrics, *sqlgraph.CreateSpec) {
	var (
		_node = &Metrics{config: mc.config}
		_spec = sqlgraph.NewCreateSpec(metrics.Table, sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt))
	)
	if nodes := mc.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   metrics.BazelInvocationTable,
			Columns: []string{metrics.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.bazel_invocation_metrics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.ActionSummaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.ActionSummaryTable,
			Columns: []string{metrics.ActionSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actionsummary.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.MemoryMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.MemoryMetricsTable,
			Columns: []string{metrics.MemoryMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(memorymetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.TargetMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.TargetMetricsTable,
			Columns: []string{metrics.TargetMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(targetmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.PackageMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.PackageMetricsTable,
			Columns: []string{metrics.PackageMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(packagemetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.TimingMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.TimingMetricsTable,
			Columns: []string{metrics.TimingMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.CumulativeMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.CumulativeMetricsTable,
			Columns: []string{metrics.CumulativeMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(cumulativemetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.ArtifactMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.ArtifactMetricsTable,
			Columns: []string{metrics.ArtifactMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(artifactmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.NetworkMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.NetworkMetricsTable,
			Columns: []string{metrics.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)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.DynamicExecutionMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.DynamicExecutionMetricsTable,
			Columns: []string{metrics.DynamicExecutionMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dynamicexecutionmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mc.mutation.BuildGraphMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   metrics.BuildGraphMetricsTable,
			Columns: []string{metrics.BuildGraphMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(buildgraphmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// MetricsCreateBulk is the builder for creating many Metrics entities in bulk.
type MetricsCreateBulk struct {
	config
	err      error
	builders []*MetricsCreate
}

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

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

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

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