// 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/exectioninfo"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testcollection"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testfile"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testresultbes"
)

// TestResultBESCreate is the builder for creating a TestResultBES entity.
type TestResultBESCreate struct {
	config
	mutation *TestResultBESMutation
	hooks    []Hook
}

// SetTestStatus sets the "test_status" field.
func (trbc *TestResultBESCreate) SetTestStatus(ts testresultbes.TestStatus) *TestResultBESCreate {
	trbc.mutation.SetTestStatus(ts)
	return trbc
}

// SetNillableTestStatus sets the "test_status" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestStatus(ts *testresultbes.TestStatus) *TestResultBESCreate {
	if ts != nil {
		trbc.SetTestStatus(*ts)
	}
	return trbc
}

// SetStatusDetails sets the "status_details" field.
func (trbc *TestResultBESCreate) SetStatusDetails(s string) *TestResultBESCreate {
	trbc.mutation.SetStatusDetails(s)
	return trbc
}

// SetNillableStatusDetails sets the "status_details" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableStatusDetails(s *string) *TestResultBESCreate {
	if s != nil {
		trbc.SetStatusDetails(*s)
	}
	return trbc
}

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

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

// SetWarning sets the "warning" field.
func (trbc *TestResultBESCreate) SetWarning(s []string) *TestResultBESCreate {
	trbc.mutation.SetWarning(s)
	return trbc
}

// SetCachedLocally sets the "cached_locally" field.
func (trbc *TestResultBESCreate) SetCachedLocally(b bool) *TestResultBESCreate {
	trbc.mutation.SetCachedLocally(b)
	return trbc
}

// SetNillableCachedLocally sets the "cached_locally" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableCachedLocally(b *bool) *TestResultBESCreate {
	if b != nil {
		trbc.SetCachedLocally(*b)
	}
	return trbc
}

// SetTestAttemptStartMillisEpoch sets the "test_attempt_start_millis_epoch" field.
func (trbc *TestResultBESCreate) SetTestAttemptStartMillisEpoch(i int64) *TestResultBESCreate {
	trbc.mutation.SetTestAttemptStartMillisEpoch(i)
	return trbc
}

// SetNillableTestAttemptStartMillisEpoch sets the "test_attempt_start_millis_epoch" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestAttemptStartMillisEpoch(i *int64) *TestResultBESCreate {
	if i != nil {
		trbc.SetTestAttemptStartMillisEpoch(*i)
	}
	return trbc
}

// SetTestAttemptStart sets the "test_attempt_start" field.
func (trbc *TestResultBESCreate) SetTestAttemptStart(s string) *TestResultBESCreate {
	trbc.mutation.SetTestAttemptStart(s)
	return trbc
}

// SetNillableTestAttemptStart sets the "test_attempt_start" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestAttemptStart(s *string) *TestResultBESCreate {
	if s != nil {
		trbc.SetTestAttemptStart(*s)
	}
	return trbc
}

// SetTestAttemptDurationMillis sets the "test_attempt_duration_millis" field.
func (trbc *TestResultBESCreate) SetTestAttemptDurationMillis(i int64) *TestResultBESCreate {
	trbc.mutation.SetTestAttemptDurationMillis(i)
	return trbc
}

// SetNillableTestAttemptDurationMillis sets the "test_attempt_duration_millis" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestAttemptDurationMillis(i *int64) *TestResultBESCreate {
	if i != nil {
		trbc.SetTestAttemptDurationMillis(*i)
	}
	return trbc
}

// SetTestAttemptDuration sets the "test_attempt_duration" field.
func (trbc *TestResultBESCreate) SetTestAttemptDuration(i int64) *TestResultBESCreate {
	trbc.mutation.SetTestAttemptDuration(i)
	return trbc
}

// SetNillableTestAttemptDuration sets the "test_attempt_duration" field if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestAttemptDuration(i *int64) *TestResultBESCreate {
	if i != nil {
		trbc.SetTestAttemptDuration(*i)
	}
	return trbc
}

// SetTestCollectionID sets the "test_collection" edge to the TestCollection entity by ID.
func (trbc *TestResultBESCreate) SetTestCollectionID(id int) *TestResultBESCreate {
	trbc.mutation.SetTestCollectionID(id)
	return trbc
}

// SetNillableTestCollectionID sets the "test_collection" edge to the TestCollection entity by ID if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableTestCollectionID(id *int) *TestResultBESCreate {
	if id != nil {
		trbc = trbc.SetTestCollectionID(*id)
	}
	return trbc
}

// SetTestCollection sets the "test_collection" edge to the TestCollection entity.
func (trbc *TestResultBESCreate) SetTestCollection(t *TestCollection) *TestResultBESCreate {
	return trbc.SetTestCollectionID(t.ID)
}

// AddTestActionOutputIDs adds the "test_action_output" edge to the TestFile entity by IDs.
func (trbc *TestResultBESCreate) AddTestActionOutputIDs(ids ...int) *TestResultBESCreate {
	trbc.mutation.AddTestActionOutputIDs(ids...)
	return trbc
}

// AddTestActionOutput adds the "test_action_output" edges to the TestFile entity.
func (trbc *TestResultBESCreate) AddTestActionOutput(t ...*TestFile) *TestResultBESCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return trbc.AddTestActionOutputIDs(ids...)
}

// SetExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID.
func (trbc *TestResultBESCreate) SetExecutionInfoID(id int) *TestResultBESCreate {
	trbc.mutation.SetExecutionInfoID(id)
	return trbc
}

// SetNillableExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID if the given value is not nil.
func (trbc *TestResultBESCreate) SetNillableExecutionInfoID(id *int) *TestResultBESCreate {
	if id != nil {
		trbc = trbc.SetExecutionInfoID(*id)
	}
	return trbc
}

// SetExecutionInfo sets the "execution_info" edge to the ExectionInfo entity.
func (trbc *TestResultBESCreate) SetExecutionInfo(e *ExectionInfo) *TestResultBESCreate {
	return trbc.SetExecutionInfoID(e.ID)
}

// Mutation returns the TestResultBESMutation object of the builder.
func (trbc *TestResultBESCreate) Mutation() *TestResultBESMutation {
	return trbc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (trbc *TestResultBESCreate) defaults() {
	if _, ok := trbc.mutation.TestStatus(); !ok {
		v := testresultbes.DefaultTestStatus
		trbc.mutation.SetTestStatus(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (trbc *TestResultBESCreate) check() error {
	if v, ok := trbc.mutation.TestStatus(); ok {
		if err := testresultbes.TestStatusValidator(v); err != nil {
			return &ValidationError{Name: "test_status", err: fmt.Errorf(`ent: validator failed for field "TestResultBES.test_status": %w`, err)}
		}
	}
	return nil
}

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

func (trbc *TestResultBESCreate) createSpec() (*TestResultBES, *sqlgraph.CreateSpec) {
	var (
		_node = &TestResultBES{config: trbc.config}
		_spec = sqlgraph.NewCreateSpec(testresultbes.Table, sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt))
	)
	if value, ok := trbc.mutation.TestStatus(); ok {
		_spec.SetField(testresultbes.FieldTestStatus, field.TypeEnum, value)
		_node.TestStatus = value
	}
	if value, ok := trbc.mutation.StatusDetails(); ok {
		_spec.SetField(testresultbes.FieldStatusDetails, field.TypeString, value)
		_node.StatusDetails = value
	}
	if value, ok := trbc.mutation.Label(); ok {
		_spec.SetField(testresultbes.FieldLabel, field.TypeString, value)
		_node.Label = value
	}
	if value, ok := trbc.mutation.Warning(); ok {
		_spec.SetField(testresultbes.FieldWarning, field.TypeJSON, value)
		_node.Warning = value
	}
	if value, ok := trbc.mutation.CachedLocally(); ok {
		_spec.SetField(testresultbes.FieldCachedLocally, field.TypeBool, value)
		_node.CachedLocally = value
	}
	if value, ok := trbc.mutation.TestAttemptStartMillisEpoch(); ok {
		_spec.SetField(testresultbes.FieldTestAttemptStartMillisEpoch, field.TypeInt64, value)
		_node.TestAttemptStartMillisEpoch = value
	}
	if value, ok := trbc.mutation.TestAttemptStart(); ok {
		_spec.SetField(testresultbes.FieldTestAttemptStart, field.TypeString, value)
		_node.TestAttemptStart = value
	}
	if value, ok := trbc.mutation.TestAttemptDurationMillis(); ok {
		_spec.SetField(testresultbes.FieldTestAttemptDurationMillis, field.TypeInt64, value)
		_node.TestAttemptDurationMillis = value
	}
	if value, ok := trbc.mutation.TestAttemptDuration(); ok {
		_spec.SetField(testresultbes.FieldTestAttemptDuration, field.TypeInt64, value)
		_node.TestAttemptDuration = value
	}
	if nodes := trbc.mutation.TestCollectionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testresultbes.TestCollectionTable,
			Columns: []string{testresultbes.TestCollectionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testcollection.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.test_collection_test_results = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := trbc.mutation.TestActionOutputIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testresultbes.TestActionOutputTable,
			Columns: []string{testresultbes.TestActionOutputColumn},
			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 := trbc.mutation.ExecutionInfoIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   testresultbes.ExecutionInfoTable,
			Columns: []string{testresultbes.ExecutionInfoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(exectioninfo.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// TestResultBESCreateBulk is the builder for creating many TestResultBES entities in bulk.
type TestResultBESCreateBulk struct {
	config
	err      error
	builders []*TestResultBESCreate
}

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

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

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

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