// 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/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetcomplete"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetconfigured"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetpair"
)

// TargetPairCreate is the builder for creating a TargetPair entity.
type TargetPairCreate struct {
	config
	mutation *TargetPairMutation
	hooks    []Hook
}

// SetLabel sets the "label" field.
func (tpc *TargetPairCreate) SetLabel(s string) *TargetPairCreate {
	tpc.mutation.SetLabel(s)
	return tpc
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableLabel(s *string) *TargetPairCreate {
	if s != nil {
		tpc.SetLabel(*s)
	}
	return tpc
}

// SetDurationInMs sets the "duration_in_ms" field.
func (tpc *TargetPairCreate) SetDurationInMs(i int64) *TargetPairCreate {
	tpc.mutation.SetDurationInMs(i)
	return tpc
}

// SetNillableDurationInMs sets the "duration_in_ms" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableDurationInMs(i *int64) *TargetPairCreate {
	if i != nil {
		tpc.SetDurationInMs(*i)
	}
	return tpc
}

// SetSuccess sets the "success" field.
func (tpc *TargetPairCreate) SetSuccess(b bool) *TargetPairCreate {
	tpc.mutation.SetSuccess(b)
	return tpc
}

// SetNillableSuccess sets the "success" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableSuccess(b *bool) *TargetPairCreate {
	if b != nil {
		tpc.SetSuccess(*b)
	}
	return tpc
}

// SetTargetKind sets the "target_kind" field.
func (tpc *TargetPairCreate) SetTargetKind(s string) *TargetPairCreate {
	tpc.mutation.SetTargetKind(s)
	return tpc
}

// SetNillableTargetKind sets the "target_kind" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableTargetKind(s *string) *TargetPairCreate {
	if s != nil {
		tpc.SetTargetKind(*s)
	}
	return tpc
}

// SetTestSize sets the "test_size" field.
func (tpc *TargetPairCreate) SetTestSize(ts targetpair.TestSize) *TargetPairCreate {
	tpc.mutation.SetTestSize(ts)
	return tpc
}

// SetNillableTestSize sets the "test_size" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableTestSize(ts *targetpair.TestSize) *TargetPairCreate {
	if ts != nil {
		tpc.SetTestSize(*ts)
	}
	return tpc
}

// SetAbortReason sets the "abort_reason" field.
func (tpc *TargetPairCreate) SetAbortReason(tr targetpair.AbortReason) *TargetPairCreate {
	tpc.mutation.SetAbortReason(tr)
	return tpc
}

// SetNillableAbortReason sets the "abort_reason" field if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableAbortReason(tr *targetpair.AbortReason) *TargetPairCreate {
	if tr != nil {
		tpc.SetAbortReason(*tr)
	}
	return tpc
}

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

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

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

// SetConfigurationID sets the "configuration" edge to the TargetConfigured entity by ID.
func (tpc *TargetPairCreate) SetConfigurationID(id int) *TargetPairCreate {
	tpc.mutation.SetConfigurationID(id)
	return tpc
}

// SetNillableConfigurationID sets the "configuration" edge to the TargetConfigured entity by ID if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableConfigurationID(id *int) *TargetPairCreate {
	if id != nil {
		tpc = tpc.SetConfigurationID(*id)
	}
	return tpc
}

// SetConfiguration sets the "configuration" edge to the TargetConfigured entity.
func (tpc *TargetPairCreate) SetConfiguration(t *TargetConfigured) *TargetPairCreate {
	return tpc.SetConfigurationID(t.ID)
}

// SetCompletionID sets the "completion" edge to the TargetComplete entity by ID.
func (tpc *TargetPairCreate) SetCompletionID(id int) *TargetPairCreate {
	tpc.mutation.SetCompletionID(id)
	return tpc
}

// SetNillableCompletionID sets the "completion" edge to the TargetComplete entity by ID if the given value is not nil.
func (tpc *TargetPairCreate) SetNillableCompletionID(id *int) *TargetPairCreate {
	if id != nil {
		tpc = tpc.SetCompletionID(*id)
	}
	return tpc
}

// SetCompletion sets the "completion" edge to the TargetComplete entity.
func (tpc *TargetPairCreate) SetCompletion(t *TargetComplete) *TargetPairCreate {
	return tpc.SetCompletionID(t.ID)
}

// Mutation returns the TargetPairMutation object of the builder.
func (tpc *TargetPairCreate) Mutation() *TargetPairMutation {
	return tpc.mutation
}

// Save creates the TargetPair in the database.
func (tpc *TargetPairCreate) Save(ctx context.Context) (*TargetPair, error) {
	tpc.defaults()
	return withHooks(ctx, tpc.sqlSave, tpc.mutation, tpc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tpc *TargetPairCreate) defaults() {
	if _, ok := tpc.mutation.Success(); !ok {
		v := targetpair.DefaultSuccess
		tpc.mutation.SetSuccess(v)
	}
	if _, ok := tpc.mutation.TestSize(); !ok {
		v := targetpair.DefaultTestSize
		tpc.mutation.SetTestSize(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (tpc *TargetPairCreate) check() error {
	if v, ok := tpc.mutation.TestSize(); ok {
		if err := targetpair.TestSizeValidator(v); err != nil {
			return &ValidationError{Name: "test_size", err: fmt.Errorf(`ent: validator failed for field "TargetPair.test_size": %w`, err)}
		}
	}
	if v, ok := tpc.mutation.AbortReason(); ok {
		if err := targetpair.AbortReasonValidator(v); err != nil {
			return &ValidationError{Name: "abort_reason", err: fmt.Errorf(`ent: validator failed for field "TargetPair.abort_reason": %w`, err)}
		}
	}
	return nil
}

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

func (tpc *TargetPairCreate) createSpec() (*TargetPair, *sqlgraph.CreateSpec) {
	var (
		_node = &TargetPair{config: tpc.config}
		_spec = sqlgraph.NewCreateSpec(targetpair.Table, sqlgraph.NewFieldSpec(targetpair.FieldID, field.TypeInt))
	)
	if value, ok := tpc.mutation.Label(); ok {
		_spec.SetField(targetpair.FieldLabel, field.TypeString, value)
		_node.Label = value
	}
	if value, ok := tpc.mutation.DurationInMs(); ok {
		_spec.SetField(targetpair.FieldDurationInMs, field.TypeInt64, value)
		_node.DurationInMs = value
	}
	if value, ok := tpc.mutation.Success(); ok {
		_spec.SetField(targetpair.FieldSuccess, field.TypeBool, value)
		_node.Success = value
	}
	if value, ok := tpc.mutation.TargetKind(); ok {
		_spec.SetField(targetpair.FieldTargetKind, field.TypeString, value)
		_node.TargetKind = value
	}
	if value, ok := tpc.mutation.TestSize(); ok {
		_spec.SetField(targetpair.FieldTestSize, field.TypeEnum, value)
		_node.TestSize = value
	}
	if value, ok := tpc.mutation.AbortReason(); ok {
		_spec.SetField(targetpair.FieldAbortReason, field.TypeEnum, value)
		_node.AbortReason = value
	}
	if nodes := tpc.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   targetpair.BazelInvocationTable,
			Columns: []string{targetpair.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_targets = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tpc.mutation.ConfigurationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   targetpair.ConfigurationTable,
			Columns: []string{targetpair.ConfigurationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(targetconfigured.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tpc.mutation.CompletionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   targetpair.CompletionTable,
			Columns: []string{targetpair.CompletionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(targetcomplete.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// TargetPairCreateBulk is the builder for creating many TargetPair entities in bulk.
type TargetPairCreateBulk struct {
	config
	err      error
	builders []*TargetPairCreate
}

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

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

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

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