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

// ArtifactMetricsCreate is the builder for creating a ArtifactMetrics entity.
type ArtifactMetricsCreate struct {
	config
	mutation *ArtifactMetricsMutation
	hooks    []Hook
}

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

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

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

// SetSourceArtifactsReadID sets the "source_artifacts_read" edge to the FilesMetric entity by ID.
func (amc *ArtifactMetricsCreate) SetSourceArtifactsReadID(id int) *ArtifactMetricsCreate {
	amc.mutation.SetSourceArtifactsReadID(id)
	return amc
}

// SetNillableSourceArtifactsReadID sets the "source_artifacts_read" edge to the FilesMetric entity by ID if the given value is not nil.
func (amc *ArtifactMetricsCreate) SetNillableSourceArtifactsReadID(id *int) *ArtifactMetricsCreate {
	if id != nil {
		amc = amc.SetSourceArtifactsReadID(*id)
	}
	return amc
}

// SetSourceArtifactsRead sets the "source_artifacts_read" edge to the FilesMetric entity.
func (amc *ArtifactMetricsCreate) SetSourceArtifactsRead(f *FilesMetric) *ArtifactMetricsCreate {
	return amc.SetSourceArtifactsReadID(f.ID)
}

// SetOutputArtifactsSeenID sets the "output_artifacts_seen" edge to the FilesMetric entity by ID.
func (amc *ArtifactMetricsCreate) SetOutputArtifactsSeenID(id int) *ArtifactMetricsCreate {
	amc.mutation.SetOutputArtifactsSeenID(id)
	return amc
}

// SetNillableOutputArtifactsSeenID sets the "output_artifacts_seen" edge to the FilesMetric entity by ID if the given value is not nil.
func (amc *ArtifactMetricsCreate) SetNillableOutputArtifactsSeenID(id *int) *ArtifactMetricsCreate {
	if id != nil {
		amc = amc.SetOutputArtifactsSeenID(*id)
	}
	return amc
}

// SetOutputArtifactsSeen sets the "output_artifacts_seen" edge to the FilesMetric entity.
func (amc *ArtifactMetricsCreate) SetOutputArtifactsSeen(f *FilesMetric) *ArtifactMetricsCreate {
	return amc.SetOutputArtifactsSeenID(f.ID)
}

// SetOutputArtifactsFromActionCacheID sets the "output_artifacts_from_action_cache" edge to the FilesMetric entity by ID.
func (amc *ArtifactMetricsCreate) SetOutputArtifactsFromActionCacheID(id int) *ArtifactMetricsCreate {
	amc.mutation.SetOutputArtifactsFromActionCacheID(id)
	return amc
}

// SetNillableOutputArtifactsFromActionCacheID sets the "output_artifacts_from_action_cache" edge to the FilesMetric entity by ID if the given value is not nil.
func (amc *ArtifactMetricsCreate) SetNillableOutputArtifactsFromActionCacheID(id *int) *ArtifactMetricsCreate {
	if id != nil {
		amc = amc.SetOutputArtifactsFromActionCacheID(*id)
	}
	return amc
}

// SetOutputArtifactsFromActionCache sets the "output_artifacts_from_action_cache" edge to the FilesMetric entity.
func (amc *ArtifactMetricsCreate) SetOutputArtifactsFromActionCache(f *FilesMetric) *ArtifactMetricsCreate {
	return amc.SetOutputArtifactsFromActionCacheID(f.ID)
}

// SetTopLevelArtifactsID sets the "top_level_artifacts" edge to the FilesMetric entity by ID.
func (amc *ArtifactMetricsCreate) SetTopLevelArtifactsID(id int) *ArtifactMetricsCreate {
	amc.mutation.SetTopLevelArtifactsID(id)
	return amc
}

// SetNillableTopLevelArtifactsID sets the "top_level_artifacts" edge to the FilesMetric entity by ID if the given value is not nil.
func (amc *ArtifactMetricsCreate) SetNillableTopLevelArtifactsID(id *int) *ArtifactMetricsCreate {
	if id != nil {
		amc = amc.SetTopLevelArtifactsID(*id)
	}
	return amc
}

// SetTopLevelArtifacts sets the "top_level_artifacts" edge to the FilesMetric entity.
func (amc *ArtifactMetricsCreate) SetTopLevelArtifacts(f *FilesMetric) *ArtifactMetricsCreate {
	return amc.SetTopLevelArtifactsID(f.ID)
}

// Mutation returns the ArtifactMetricsMutation object of the builder.
func (amc *ArtifactMetricsCreate) Mutation() *ArtifactMetricsMutation {
	return amc.mutation
}

// Save creates the ArtifactMetrics in the database.
func (amc *ArtifactMetricsCreate) Save(ctx context.Context) (*ArtifactMetrics, error) {
	return withHooks(ctx, amc.sqlSave, amc.mutation, amc.hooks)
}

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

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

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

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

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

func (amc *ArtifactMetricsCreate) createSpec() (*ArtifactMetrics, *sqlgraph.CreateSpec) {
	var (
		_node = &ArtifactMetrics{config: amc.config}
		_spec = sqlgraph.NewCreateSpec(artifactmetrics.Table, sqlgraph.NewFieldSpec(artifactmetrics.FieldID, field.TypeInt))
	)
	if nodes := amc.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   artifactmetrics.MetricsTable,
			Columns: []string{artifactmetrics.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_artifact_metrics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := amc.mutation.SourceArtifactsReadIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   artifactmetrics.SourceArtifactsReadTable,
			Columns: []string{artifactmetrics.SourceArtifactsReadColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.artifact_metrics_source_artifacts_read = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := amc.mutation.OutputArtifactsSeenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   artifactmetrics.OutputArtifactsSeenTable,
			Columns: []string{artifactmetrics.OutputArtifactsSeenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.artifact_metrics_output_artifacts_seen = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := amc.mutation.OutputArtifactsFromActionCacheIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   artifactmetrics.OutputArtifactsFromActionCacheTable,
			Columns: []string{artifactmetrics.OutputArtifactsFromActionCacheColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.artifact_metrics_output_artifacts_from_action_cache = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := amc.mutation.TopLevelArtifactsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   artifactmetrics.TopLevelArtifactsTable,
			Columns: []string{artifactmetrics.TopLevelArtifactsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// ArtifactMetricsCreateBulk is the builder for creating many ArtifactMetrics entities in bulk.
type ArtifactMetricsCreateBulk struct {
	config
	err      error
	builders []*ArtifactMetricsCreate
}

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

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

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

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