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

// TestFileCreate is the builder for creating a TestFile entity.
type TestFileCreate struct {
	config
	mutation *TestFileMutation
	hooks    []Hook
}

// SetDigest sets the "digest" field.
func (tfc *TestFileCreate) SetDigest(s string) *TestFileCreate {
	tfc.mutation.SetDigest(s)
	return tfc
}

// SetNillableDigest sets the "digest" field if the given value is not nil.
func (tfc *TestFileCreate) SetNillableDigest(s *string) *TestFileCreate {
	if s != nil {
		tfc.SetDigest(*s)
	}
	return tfc
}

// SetFile sets the "file" field.
func (tfc *TestFileCreate) SetFile(s string) *TestFileCreate {
	tfc.mutation.SetFile(s)
	return tfc
}

// SetNillableFile sets the "file" field if the given value is not nil.
func (tfc *TestFileCreate) SetNillableFile(s *string) *TestFileCreate {
	if s != nil {
		tfc.SetFile(*s)
	}
	return tfc
}

// SetLength sets the "length" field.
func (tfc *TestFileCreate) SetLength(i int64) *TestFileCreate {
	tfc.mutation.SetLength(i)
	return tfc
}

// SetNillableLength sets the "length" field if the given value is not nil.
func (tfc *TestFileCreate) SetNillableLength(i *int64) *TestFileCreate {
	if i != nil {
		tfc.SetLength(*i)
	}
	return tfc
}

// SetName sets the "name" field.
func (tfc *TestFileCreate) SetName(s string) *TestFileCreate {
	tfc.mutation.SetName(s)
	return tfc
}

// SetNillableName sets the "name" field if the given value is not nil.
func (tfc *TestFileCreate) SetNillableName(s *string) *TestFileCreate {
	if s != nil {
		tfc.SetName(*s)
	}
	return tfc
}

// SetPrefix sets the "prefix" field.
func (tfc *TestFileCreate) SetPrefix(s []string) *TestFileCreate {
	tfc.mutation.SetPrefix(s)
	return tfc
}

// SetTestResultID sets the "test_result" edge to the TestResultBES entity by ID.
func (tfc *TestFileCreate) SetTestResultID(id int) *TestFileCreate {
	tfc.mutation.SetTestResultID(id)
	return tfc
}

// SetNillableTestResultID sets the "test_result" edge to the TestResultBES entity by ID if the given value is not nil.
func (tfc *TestFileCreate) SetNillableTestResultID(id *int) *TestFileCreate {
	if id != nil {
		tfc = tfc.SetTestResultID(*id)
	}
	return tfc
}

// SetTestResult sets the "test_result" edge to the TestResultBES entity.
func (tfc *TestFileCreate) SetTestResult(t *TestResultBES) *TestFileCreate {
	return tfc.SetTestResultID(t.ID)
}

// Mutation returns the TestFileMutation object of the builder.
func (tfc *TestFileCreate) Mutation() *TestFileMutation {
	return tfc.mutation
}

// Save creates the TestFile in the database.
func (tfc *TestFileCreate) Save(ctx context.Context) (*TestFile, error) {
	return withHooks(ctx, tfc.sqlSave, tfc.mutation, tfc.hooks)
}

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

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

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

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

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

func (tfc *TestFileCreate) createSpec() (*TestFile, *sqlgraph.CreateSpec) {
	var (
		_node = &TestFile{config: tfc.config}
		_spec = sqlgraph.NewCreateSpec(testfile.Table, sqlgraph.NewFieldSpec(testfile.FieldID, field.TypeInt))
	)
	if value, ok := tfc.mutation.Digest(); ok {
		_spec.SetField(testfile.FieldDigest, field.TypeString, value)
		_node.Digest = value
	}
	if value, ok := tfc.mutation.File(); ok {
		_spec.SetField(testfile.FieldFile, field.TypeString, value)
		_node.File = value
	}
	if value, ok := tfc.mutation.Length(); ok {
		_spec.SetField(testfile.FieldLength, field.TypeInt64, value)
		_node.Length = value
	}
	if value, ok := tfc.mutation.Name(); ok {
		_spec.SetField(testfile.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := tfc.mutation.Prefix(); ok {
		_spec.SetField(testfile.FieldPrefix, field.TypeJSON, value)
		_node.Prefix = value
	}
	if nodes := tfc.mutation.TestResultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testfile.TestResultTable,
			Columns: []string{testfile.TestResultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.test_result_bes_test_action_output = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// TestFileCreateBulk is the builder for creating many TestFile entities in bulk.
type TestFileCreateBulk struct {
	config
	err      error
	builders []*TestFileCreate
}

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

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

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

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