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

// TimingMetricsCreate is the builder for creating a TimingMetrics entity.
type TimingMetricsCreate struct {
	config
	mutation *TimingMetricsMutation
	hooks    []Hook
}

// SetCPUTimeInMs sets the "cpu_time_in_ms" field.
func (tmc *TimingMetricsCreate) SetCPUTimeInMs(i int64) *TimingMetricsCreate {
	tmc.mutation.SetCPUTimeInMs(i)
	return tmc
}

// SetNillableCPUTimeInMs sets the "cpu_time_in_ms" field if the given value is not nil.
func (tmc *TimingMetricsCreate) SetNillableCPUTimeInMs(i *int64) *TimingMetricsCreate {
	if i != nil {
		tmc.SetCPUTimeInMs(*i)
	}
	return tmc
}

// SetWallTimeInMs sets the "wall_time_in_ms" field.
func (tmc *TimingMetricsCreate) SetWallTimeInMs(i int64) *TimingMetricsCreate {
	tmc.mutation.SetWallTimeInMs(i)
	return tmc
}

// SetNillableWallTimeInMs sets the "wall_time_in_ms" field if the given value is not nil.
func (tmc *TimingMetricsCreate) SetNillableWallTimeInMs(i *int64) *TimingMetricsCreate {
	if i != nil {
		tmc.SetWallTimeInMs(*i)
	}
	return tmc
}

// SetAnalysisPhaseTimeInMs sets the "analysis_phase_time_in_ms" field.
func (tmc *TimingMetricsCreate) SetAnalysisPhaseTimeInMs(i int64) *TimingMetricsCreate {
	tmc.mutation.SetAnalysisPhaseTimeInMs(i)
	return tmc
}

// SetNillableAnalysisPhaseTimeInMs sets the "analysis_phase_time_in_ms" field if the given value is not nil.
func (tmc *TimingMetricsCreate) SetNillableAnalysisPhaseTimeInMs(i *int64) *TimingMetricsCreate {
	if i != nil {
		tmc.SetAnalysisPhaseTimeInMs(*i)
	}
	return tmc
}

// SetExecutionPhaseTimeInMs sets the "execution_phase_time_in_ms" field.
func (tmc *TimingMetricsCreate) SetExecutionPhaseTimeInMs(i int64) *TimingMetricsCreate {
	tmc.mutation.SetExecutionPhaseTimeInMs(i)
	return tmc
}

// SetNillableExecutionPhaseTimeInMs sets the "execution_phase_time_in_ms" field if the given value is not nil.
func (tmc *TimingMetricsCreate) SetNillableExecutionPhaseTimeInMs(i *int64) *TimingMetricsCreate {
	if i != nil {
		tmc.SetExecutionPhaseTimeInMs(*i)
	}
	return tmc
}

// SetActionsExecutionStartInMs sets the "actions_execution_start_in_ms" field.
func (tmc *TimingMetricsCreate) SetActionsExecutionStartInMs(i int64) *TimingMetricsCreate {
	tmc.mutation.SetActionsExecutionStartInMs(i)
	return tmc
}

// SetNillableActionsExecutionStartInMs sets the "actions_execution_start_in_ms" field if the given value is not nil.
func (tmc *TimingMetricsCreate) SetNillableActionsExecutionStartInMs(i *int64) *TimingMetricsCreate {
	if i != nil {
		tmc.SetActionsExecutionStartInMs(*i)
	}
	return tmc
}

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

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

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

// Mutation returns the TimingMetricsMutation object of the builder.
func (tmc *TimingMetricsCreate) Mutation() *TimingMetricsMutation {
	return tmc.mutation
}

// Save creates the TimingMetrics in the database.
func (tmc *TimingMetricsCreate) Save(ctx context.Context) (*TimingMetrics, error) {
	return withHooks(ctx, tmc.sqlSave, tmc.mutation, tmc.hooks)
}

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

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

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

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

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

func (tmc *TimingMetricsCreate) createSpec() (*TimingMetrics, *sqlgraph.CreateSpec) {
	var (
		_node = &TimingMetrics{config: tmc.config}
		_spec = sqlgraph.NewCreateSpec(timingmetrics.Table, sqlgraph.NewFieldSpec(timingmetrics.FieldID, field.TypeInt))
	)
	if value, ok := tmc.mutation.CPUTimeInMs(); ok {
		_spec.SetField(timingmetrics.FieldCPUTimeInMs, field.TypeInt64, value)
		_node.CPUTimeInMs = value
	}
	if value, ok := tmc.mutation.WallTimeInMs(); ok {
		_spec.SetField(timingmetrics.FieldWallTimeInMs, field.TypeInt64, value)
		_node.WallTimeInMs = value
	}
	if value, ok := tmc.mutation.AnalysisPhaseTimeInMs(); ok {
		_spec.SetField(timingmetrics.FieldAnalysisPhaseTimeInMs, field.TypeInt64, value)
		_node.AnalysisPhaseTimeInMs = value
	}
	if value, ok := tmc.mutation.ExecutionPhaseTimeInMs(); ok {
		_spec.SetField(timingmetrics.FieldExecutionPhaseTimeInMs, field.TypeInt64, value)
		_node.ExecutionPhaseTimeInMs = value
	}
	if value, ok := tmc.mutation.ActionsExecutionStartInMs(); ok {
		_spec.SetField(timingmetrics.FieldActionsExecutionStartInMs, field.TypeInt64, value)
		_node.ActionsExecutionStartInMs = value
	}
	if nodes := tmc.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   timingmetrics.MetricsTable,
			Columns: []string{timingmetrics.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_timing_metrics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// TimingMetricsCreateBulk is the builder for creating many TimingMetrics entities in bulk.
type TimingMetricsCreateBulk struct {
	config
	err      error
	builders []*TimingMetricsCreate
}

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

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

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

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