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

// TargetCompleteCreate is the builder for creating a TargetComplete entity.
type TargetCompleteCreate struct {
	config
	mutation *TargetCompleteMutation
	hooks    []Hook
}

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

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

// SetTag sets the "tag" field.
func (tcc *TargetCompleteCreate) SetTag(s []string) *TargetCompleteCreate {
	tcc.mutation.SetTag(s)
	return tcc
}

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

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

// SetEndTimeInMs sets the "end_time_in_ms" field.
func (tcc *TargetCompleteCreate) SetEndTimeInMs(i int64) *TargetCompleteCreate {
	tcc.mutation.SetEndTimeInMs(i)
	return tcc
}

// SetNillableEndTimeInMs sets the "end_time_in_ms" field if the given value is not nil.
func (tcc *TargetCompleteCreate) SetNillableEndTimeInMs(i *int64) *TargetCompleteCreate {
	if i != nil {
		tcc.SetEndTimeInMs(*i)
	}
	return tcc
}

// SetTestTimeoutSeconds sets the "test_timeout_seconds" field.
func (tcc *TargetCompleteCreate) SetTestTimeoutSeconds(i int64) *TargetCompleteCreate {
	tcc.mutation.SetTestTimeoutSeconds(i)
	return tcc
}

// SetNillableTestTimeoutSeconds sets the "test_timeout_seconds" field if the given value is not nil.
func (tcc *TargetCompleteCreate) SetNillableTestTimeoutSeconds(i *int64) *TargetCompleteCreate {
	if i != nil {
		tcc.SetTestTimeoutSeconds(*i)
	}
	return tcc
}

// SetTestTimeout sets the "test_timeout" field.
func (tcc *TargetCompleteCreate) SetTestTimeout(i int64) *TargetCompleteCreate {
	tcc.mutation.SetTestTimeout(i)
	return tcc
}

// SetNillableTestTimeout sets the "test_timeout" field if the given value is not nil.
func (tcc *TargetCompleteCreate) SetNillableTestTimeout(i *int64) *TargetCompleteCreate {
	if i != nil {
		tcc.SetTestTimeout(*i)
	}
	return tcc
}

// SetTestSize sets the "test_size" field.
func (tcc *TargetCompleteCreate) SetTestSize(ts targetcomplete.TestSize) *TargetCompleteCreate {
	tcc.mutation.SetTestSize(ts)
	return tcc
}

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

// SetTargetPairID sets the "target_pair" edge to the TargetPair entity by ID.
func (tcc *TargetCompleteCreate) SetTargetPairID(id int) *TargetCompleteCreate {
	tcc.mutation.SetTargetPairID(id)
	return tcc
}

// SetNillableTargetPairID sets the "target_pair" edge to the TargetPair entity by ID if the given value is not nil.
func (tcc *TargetCompleteCreate) SetNillableTargetPairID(id *int) *TargetCompleteCreate {
	if id != nil {
		tcc = tcc.SetTargetPairID(*id)
	}
	return tcc
}

// SetTargetPair sets the "target_pair" edge to the TargetPair entity.
func (tcc *TargetCompleteCreate) SetTargetPair(t *TargetPair) *TargetCompleteCreate {
	return tcc.SetTargetPairID(t.ID)
}

// AddImportantOutputIDs adds the "important_output" edge to the TestFile entity by IDs.
func (tcc *TargetCompleteCreate) AddImportantOutputIDs(ids ...int) *TargetCompleteCreate {
	tcc.mutation.AddImportantOutputIDs(ids...)
	return tcc
}

// AddImportantOutput adds the "important_output" edges to the TestFile entity.
func (tcc *TargetCompleteCreate) AddImportantOutput(t ...*TestFile) *TargetCompleteCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcc.AddImportantOutputIDs(ids...)
}

// AddDirectoryOutputIDs adds the "directory_output" edge to the TestFile entity by IDs.
func (tcc *TargetCompleteCreate) AddDirectoryOutputIDs(ids ...int) *TargetCompleteCreate {
	tcc.mutation.AddDirectoryOutputIDs(ids...)
	return tcc
}

// AddDirectoryOutput adds the "directory_output" edges to the TestFile entity.
func (tcc *TargetCompleteCreate) AddDirectoryOutput(t ...*TestFile) *TargetCompleteCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcc.AddDirectoryOutputIDs(ids...)
}

// SetOutputGroupID sets the "output_group" edge to the OutputGroup entity by ID.
func (tcc *TargetCompleteCreate) SetOutputGroupID(id int) *TargetCompleteCreate {
	tcc.mutation.SetOutputGroupID(id)
	return tcc
}

// SetNillableOutputGroupID sets the "output_group" edge to the OutputGroup entity by ID if the given value is not nil.
func (tcc *TargetCompleteCreate) SetNillableOutputGroupID(id *int) *TargetCompleteCreate {
	if id != nil {
		tcc = tcc.SetOutputGroupID(*id)
	}
	return tcc
}

// SetOutputGroup sets the "output_group" edge to the OutputGroup entity.
func (tcc *TargetCompleteCreate) SetOutputGroup(o *OutputGroup) *TargetCompleteCreate {
	return tcc.SetOutputGroupID(o.ID)
}

// Mutation returns the TargetCompleteMutation object of the builder.
func (tcc *TargetCompleteCreate) Mutation() *TargetCompleteMutation {
	return tcc.mutation
}

// Save creates the TargetComplete in the database.
func (tcc *TargetCompleteCreate) Save(ctx context.Context) (*TargetComplete, error) {
	return withHooks(ctx, tcc.sqlSave, tcc.mutation, tcc.hooks)
}

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

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

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

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

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

func (tcc *TargetCompleteCreate) createSpec() (*TargetComplete, *sqlgraph.CreateSpec) {
	var (
		_node = &TargetComplete{config: tcc.config}
		_spec = sqlgraph.NewCreateSpec(targetcomplete.Table, sqlgraph.NewFieldSpec(targetcomplete.FieldID, field.TypeInt))
	)
	if value, ok := tcc.mutation.Success(); ok {
		_spec.SetField(targetcomplete.FieldSuccess, field.TypeBool, value)
		_node.Success = value
	}
	if value, ok := tcc.mutation.Tag(); ok {
		_spec.SetField(targetcomplete.FieldTag, field.TypeJSON, value)
		_node.Tag = value
	}
	if value, ok := tcc.mutation.TargetKind(); ok {
		_spec.SetField(targetcomplete.FieldTargetKind, field.TypeString, value)
		_node.TargetKind = value
	}
	if value, ok := tcc.mutation.EndTimeInMs(); ok {
		_spec.SetField(targetcomplete.FieldEndTimeInMs, field.TypeInt64, value)
		_node.EndTimeInMs = value
	}
	if value, ok := tcc.mutation.TestTimeoutSeconds(); ok {
		_spec.SetField(targetcomplete.FieldTestTimeoutSeconds, field.TypeInt64, value)
		_node.TestTimeoutSeconds = value
	}
	if value, ok := tcc.mutation.TestTimeout(); ok {
		_spec.SetField(targetcomplete.FieldTestTimeout, field.TypeInt64, value)
		_node.TestTimeout = value
	}
	if value, ok := tcc.mutation.TestSize(); ok {
		_spec.SetField(targetcomplete.FieldTestSize, field.TypeEnum, value)
		_node.TestSize = value
	}
	if nodes := tcc.mutation.TargetPairIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   targetcomplete.TargetPairTable,
			Columns: []string{targetcomplete.TargetPairColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(targetpair.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.target_pair_completion = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.ImportantOutputIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   targetcomplete.ImportantOutputTable,
			Columns: []string{targetcomplete.ImportantOutputColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testfile.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.DirectoryOutputIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   targetcomplete.DirectoryOutputTable,
			Columns: []string{targetcomplete.DirectoryOutputColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testfile.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tcc.mutation.OutputGroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   targetcomplete.OutputGroupTable,
			Columns: []string{targetcomplete.OutputGroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(outputgroup.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// TargetCompleteCreateBulk is the builder for creating many TargetComplete entities in bulk.
type TargetCompleteCreateBulk struct {
	config
	err      error
	builders []*TargetCompleteCreate
}

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

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

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

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