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

// NamedSetOfFilesCreate is the builder for creating a NamedSetOfFiles entity.
type NamedSetOfFilesCreate struct {
	config
	mutation *NamedSetOfFilesMutation
	hooks    []Hook
}

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

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

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

// AddFileIDs adds the "files" edge to the TestFile entity by IDs.
func (nsofc *NamedSetOfFilesCreate) AddFileIDs(ids ...int) *NamedSetOfFilesCreate {
	nsofc.mutation.AddFileIDs(ids...)
	return nsofc
}

// AddFiles adds the "files" edges to the TestFile entity.
func (nsofc *NamedSetOfFilesCreate) AddFiles(t ...*TestFile) *NamedSetOfFilesCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return nsofc.AddFileIDs(ids...)
}

// SetFileSetsID sets the "file_sets" edge to the NamedSetOfFiles entity by ID.
func (nsofc *NamedSetOfFilesCreate) SetFileSetsID(id int) *NamedSetOfFilesCreate {
	nsofc.mutation.SetFileSetsID(id)
	return nsofc
}

// SetNillableFileSetsID sets the "file_sets" edge to the NamedSetOfFiles entity by ID if the given value is not nil.
func (nsofc *NamedSetOfFilesCreate) SetNillableFileSetsID(id *int) *NamedSetOfFilesCreate {
	if id != nil {
		nsofc = nsofc.SetFileSetsID(*id)
	}
	return nsofc
}

// SetFileSets sets the "file_sets" edge to the NamedSetOfFiles entity.
func (nsofc *NamedSetOfFilesCreate) SetFileSets(n *NamedSetOfFiles) *NamedSetOfFilesCreate {
	return nsofc.SetFileSetsID(n.ID)
}

// Mutation returns the NamedSetOfFilesMutation object of the builder.
func (nsofc *NamedSetOfFilesCreate) Mutation() *NamedSetOfFilesMutation {
	return nsofc.mutation
}

// Save creates the NamedSetOfFiles in the database.
func (nsofc *NamedSetOfFilesCreate) Save(ctx context.Context) (*NamedSetOfFiles, error) {
	return withHooks(ctx, nsofc.sqlSave, nsofc.mutation, nsofc.hooks)
}

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

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

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

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

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

func (nsofc *NamedSetOfFilesCreate) createSpec() (*NamedSetOfFiles, *sqlgraph.CreateSpec) {
	var (
		_node = &NamedSetOfFiles{config: nsofc.config}
		_spec = sqlgraph.NewCreateSpec(namedsetoffiles.Table, sqlgraph.NewFieldSpec(namedsetoffiles.FieldID, field.TypeInt))
	)
	if nodes := nsofc.mutation.OutputGroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   namedsetoffiles.OutputGroupTable,
			Columns: []string{namedsetoffiles.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)
		}
		_node.output_group_file_sets = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := nsofc.mutation.FilesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   namedsetoffiles.FilesTable,
			Columns: []string{namedsetoffiles.FilesColumn},
			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 := nsofc.mutation.FileSetsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   namedsetoffiles.FileSetsTable,
			Columns: []string{namedsetoffiles.FileSetsColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(namedsetoffiles.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.named_set_of_files_file_sets = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// NamedSetOfFilesCreateBulk is the builder for creating many NamedSetOfFiles entities in bulk.
type NamedSetOfFilesCreateBulk struct {
	config
	err      error
	builders []*NamedSetOfFilesCreate
}

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

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

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

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