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

// BuildGraphMetricsCreate is the builder for creating a BuildGraphMetrics entity.
type BuildGraphMetricsCreate struct {
	config
	mutation *BuildGraphMetricsMutation
	hooks    []Hook
}

// SetActionLookupValueCount sets the "action_lookup_value_count" field.
func (bgmc *BuildGraphMetricsCreate) SetActionLookupValueCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetActionLookupValueCount(i)
	return bgmc
}

// SetNillableActionLookupValueCount sets the "action_lookup_value_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableActionLookupValueCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetActionLookupValueCount(*i)
	}
	return bgmc
}

// SetActionLookupValueCountNotIncludingAspects sets the "action_lookup_value_count_not_including_aspects" field.
func (bgmc *BuildGraphMetricsCreate) SetActionLookupValueCountNotIncludingAspects(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetActionLookupValueCountNotIncludingAspects(i)
	return bgmc
}

// SetNillableActionLookupValueCountNotIncludingAspects sets the "action_lookup_value_count_not_including_aspects" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableActionLookupValueCountNotIncludingAspects(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetActionLookupValueCountNotIncludingAspects(*i)
	}
	return bgmc
}

// SetActionCount sets the "action_count" field.
func (bgmc *BuildGraphMetricsCreate) SetActionCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetActionCount(i)
	return bgmc
}

// SetNillableActionCount sets the "action_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableActionCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetActionCount(*i)
	}
	return bgmc
}

// SetActionCountNotIncludingAspects sets the "action_count_not_including_aspects" field.
func (bgmc *BuildGraphMetricsCreate) SetActionCountNotIncludingAspects(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetActionCountNotIncludingAspects(i)
	return bgmc
}

// SetNillableActionCountNotIncludingAspects sets the "action_count_not_including_aspects" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableActionCountNotIncludingAspects(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetActionCountNotIncludingAspects(*i)
	}
	return bgmc
}

// SetInputFileConfiguredTargetCount sets the "input_file_configured_target_count" field.
func (bgmc *BuildGraphMetricsCreate) SetInputFileConfiguredTargetCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetInputFileConfiguredTargetCount(i)
	return bgmc
}

// SetNillableInputFileConfiguredTargetCount sets the "input_file_configured_target_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableInputFileConfiguredTargetCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetInputFileConfiguredTargetCount(*i)
	}
	return bgmc
}

// SetOutputFileConfiguredTargetCount sets the "output_file_configured_target_count" field.
func (bgmc *BuildGraphMetricsCreate) SetOutputFileConfiguredTargetCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetOutputFileConfiguredTargetCount(i)
	return bgmc
}

// SetNillableOutputFileConfiguredTargetCount sets the "output_file_configured_target_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableOutputFileConfiguredTargetCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetOutputFileConfiguredTargetCount(*i)
	}
	return bgmc
}

// SetOtherConfiguredTargetCount sets the "other_configured_target_count" field.
func (bgmc *BuildGraphMetricsCreate) SetOtherConfiguredTargetCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetOtherConfiguredTargetCount(i)
	return bgmc
}

// SetNillableOtherConfiguredTargetCount sets the "other_configured_target_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableOtherConfiguredTargetCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetOtherConfiguredTargetCount(*i)
	}
	return bgmc
}

// SetOutputArtifactCount sets the "output_artifact_count" field.
func (bgmc *BuildGraphMetricsCreate) SetOutputArtifactCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetOutputArtifactCount(i)
	return bgmc
}

// SetNillableOutputArtifactCount sets the "output_artifact_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableOutputArtifactCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetOutputArtifactCount(*i)
	}
	return bgmc
}

// SetPostInvocationSkyframeNodeCount sets the "post_invocation_skyframe_node_count" field.
func (bgmc *BuildGraphMetricsCreate) SetPostInvocationSkyframeNodeCount(i int32) *BuildGraphMetricsCreate {
	bgmc.mutation.SetPostInvocationSkyframeNodeCount(i)
	return bgmc
}

// SetNillablePostInvocationSkyframeNodeCount sets the "post_invocation_skyframe_node_count" field if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillablePostInvocationSkyframeNodeCount(i *int32) *BuildGraphMetricsCreate {
	if i != nil {
		bgmc.SetPostInvocationSkyframeNodeCount(*i)
	}
	return bgmc
}

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

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

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

// SetDirtiedValuesID sets the "dirtied_values" edge to the EvaluationStat entity by ID.
func (bgmc *BuildGraphMetricsCreate) SetDirtiedValuesID(id int) *BuildGraphMetricsCreate {
	bgmc.mutation.SetDirtiedValuesID(id)
	return bgmc
}

// SetNillableDirtiedValuesID sets the "dirtied_values" edge to the EvaluationStat entity by ID if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableDirtiedValuesID(id *int) *BuildGraphMetricsCreate {
	if id != nil {
		bgmc = bgmc.SetDirtiedValuesID(*id)
	}
	return bgmc
}

// SetDirtiedValues sets the "dirtied_values" edge to the EvaluationStat entity.
func (bgmc *BuildGraphMetricsCreate) SetDirtiedValues(e *EvaluationStat) *BuildGraphMetricsCreate {
	return bgmc.SetDirtiedValuesID(e.ID)
}

// SetChangedValuesID sets the "changed_values" edge to the EvaluationStat entity by ID.
func (bgmc *BuildGraphMetricsCreate) SetChangedValuesID(id int) *BuildGraphMetricsCreate {
	bgmc.mutation.SetChangedValuesID(id)
	return bgmc
}

// SetNillableChangedValuesID sets the "changed_values" edge to the EvaluationStat entity by ID if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableChangedValuesID(id *int) *BuildGraphMetricsCreate {
	if id != nil {
		bgmc = bgmc.SetChangedValuesID(*id)
	}
	return bgmc
}

// SetChangedValues sets the "changed_values" edge to the EvaluationStat entity.
func (bgmc *BuildGraphMetricsCreate) SetChangedValues(e *EvaluationStat) *BuildGraphMetricsCreate {
	return bgmc.SetChangedValuesID(e.ID)
}

// SetBuiltValuesID sets the "built_values" edge to the EvaluationStat entity by ID.
func (bgmc *BuildGraphMetricsCreate) SetBuiltValuesID(id int) *BuildGraphMetricsCreate {
	bgmc.mutation.SetBuiltValuesID(id)
	return bgmc
}

// SetNillableBuiltValuesID sets the "built_values" edge to the EvaluationStat entity by ID if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableBuiltValuesID(id *int) *BuildGraphMetricsCreate {
	if id != nil {
		bgmc = bgmc.SetBuiltValuesID(*id)
	}
	return bgmc
}

// SetBuiltValues sets the "built_values" edge to the EvaluationStat entity.
func (bgmc *BuildGraphMetricsCreate) SetBuiltValues(e *EvaluationStat) *BuildGraphMetricsCreate {
	return bgmc.SetBuiltValuesID(e.ID)
}

// SetCleanedValuesID sets the "cleaned_values" edge to the EvaluationStat entity by ID.
func (bgmc *BuildGraphMetricsCreate) SetCleanedValuesID(id int) *BuildGraphMetricsCreate {
	bgmc.mutation.SetCleanedValuesID(id)
	return bgmc
}

// SetNillableCleanedValuesID sets the "cleaned_values" edge to the EvaluationStat entity by ID if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableCleanedValuesID(id *int) *BuildGraphMetricsCreate {
	if id != nil {
		bgmc = bgmc.SetCleanedValuesID(*id)
	}
	return bgmc
}

// SetCleanedValues sets the "cleaned_values" edge to the EvaluationStat entity.
func (bgmc *BuildGraphMetricsCreate) SetCleanedValues(e *EvaluationStat) *BuildGraphMetricsCreate {
	return bgmc.SetCleanedValuesID(e.ID)
}

// SetEvaluatedValuesID sets the "evaluated_values" edge to the EvaluationStat entity by ID.
func (bgmc *BuildGraphMetricsCreate) SetEvaluatedValuesID(id int) *BuildGraphMetricsCreate {
	bgmc.mutation.SetEvaluatedValuesID(id)
	return bgmc
}

// SetNillableEvaluatedValuesID sets the "evaluated_values" edge to the EvaluationStat entity by ID if the given value is not nil.
func (bgmc *BuildGraphMetricsCreate) SetNillableEvaluatedValuesID(id *int) *BuildGraphMetricsCreate {
	if id != nil {
		bgmc = bgmc.SetEvaluatedValuesID(*id)
	}
	return bgmc
}

// SetEvaluatedValues sets the "evaluated_values" edge to the EvaluationStat entity.
func (bgmc *BuildGraphMetricsCreate) SetEvaluatedValues(e *EvaluationStat) *BuildGraphMetricsCreate {
	return bgmc.SetEvaluatedValuesID(e.ID)
}

// Mutation returns the BuildGraphMetricsMutation object of the builder.
func (bgmc *BuildGraphMetricsCreate) Mutation() *BuildGraphMetricsMutation {
	return bgmc.mutation
}

// Save creates the BuildGraphMetrics in the database.
func (bgmc *BuildGraphMetricsCreate) Save(ctx context.Context) (*BuildGraphMetrics, error) {
	return withHooks(ctx, bgmc.sqlSave, bgmc.mutation, bgmc.hooks)
}

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

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

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

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

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

func (bgmc *BuildGraphMetricsCreate) createSpec() (*BuildGraphMetrics, *sqlgraph.CreateSpec) {
	var (
		_node = &BuildGraphMetrics{config: bgmc.config}
		_spec = sqlgraph.NewCreateSpec(buildgraphmetrics.Table, sqlgraph.NewFieldSpec(buildgraphmetrics.FieldID, field.TypeInt))
	)
	if value, ok := bgmc.mutation.ActionLookupValueCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldActionLookupValueCount, field.TypeInt32, value)
		_node.ActionLookupValueCount = value
	}
	if value, ok := bgmc.mutation.ActionLookupValueCountNotIncludingAspects(); ok {
		_spec.SetField(buildgraphmetrics.FieldActionLookupValueCountNotIncludingAspects, field.TypeInt32, value)
		_node.ActionLookupValueCountNotIncludingAspects = value
	}
	if value, ok := bgmc.mutation.ActionCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldActionCount, field.TypeInt32, value)
		_node.ActionCount = value
	}
	if value, ok := bgmc.mutation.ActionCountNotIncludingAspects(); ok {
		_spec.SetField(buildgraphmetrics.FieldActionCountNotIncludingAspects, field.TypeInt32, value)
		_node.ActionCountNotIncludingAspects = value
	}
	if value, ok := bgmc.mutation.InputFileConfiguredTargetCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldInputFileConfiguredTargetCount, field.TypeInt32, value)
		_node.InputFileConfiguredTargetCount = value
	}
	if value, ok := bgmc.mutation.OutputFileConfiguredTargetCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldOutputFileConfiguredTargetCount, field.TypeInt32, value)
		_node.OutputFileConfiguredTargetCount = value
	}
	if value, ok := bgmc.mutation.OtherConfiguredTargetCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldOtherConfiguredTargetCount, field.TypeInt32, value)
		_node.OtherConfiguredTargetCount = value
	}
	if value, ok := bgmc.mutation.OutputArtifactCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldOutputArtifactCount, field.TypeInt32, value)
		_node.OutputArtifactCount = value
	}
	if value, ok := bgmc.mutation.PostInvocationSkyframeNodeCount(); ok {
		_spec.SetField(buildgraphmetrics.FieldPostInvocationSkyframeNodeCount, field.TypeInt32, value)
		_node.PostInvocationSkyframeNodeCount = value
	}
	if nodes := bgmc.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   buildgraphmetrics.MetricsTable,
			Columns: []string{buildgraphmetrics.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_build_graph_metrics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bgmc.mutation.DirtiedValuesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   buildgraphmetrics.DirtiedValuesTable,
			Columns: []string{buildgraphmetrics.DirtiedValuesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(evaluationstat.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.build_graph_metrics_dirtied_values = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bgmc.mutation.ChangedValuesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   buildgraphmetrics.ChangedValuesTable,
			Columns: []string{buildgraphmetrics.ChangedValuesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(evaluationstat.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.build_graph_metrics_changed_values = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bgmc.mutation.BuiltValuesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   buildgraphmetrics.BuiltValuesTable,
			Columns: []string{buildgraphmetrics.BuiltValuesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(evaluationstat.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.build_graph_metrics_built_values = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bgmc.mutation.CleanedValuesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   buildgraphmetrics.CleanedValuesTable,
			Columns: []string{buildgraphmetrics.CleanedValuesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(evaluationstat.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.build_graph_metrics_cleaned_values = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bgmc.mutation.EvaluatedValuesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   buildgraphmetrics.EvaluatedValuesTable,
			Columns: []string{buildgraphmetrics.EvaluatedValuesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(evaluationstat.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// BuildGraphMetricsCreateBulk is the builder for creating many BuildGraphMetrics entities in bulk.
type BuildGraphMetricsCreateBulk struct {
	config
	err      error
	builders []*BuildGraphMetricsCreate
}

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

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

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

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