// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"code.icod.de/dalu/gomanager/ent/logentry"
	"code.icod.de/dalu/gomanager/ent/project"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// ProjectCreate is the builder for creating a Project entity.
type ProjectCreate struct {
	config
	mutation *ProjectMutation
	hooks    []Hook
}

// SetCreateTime sets the "create_time" field.
func (pc *ProjectCreate) SetCreateTime(t time.Time) *ProjectCreate {
	pc.mutation.SetCreateTime(t)
	return pc
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (pc *ProjectCreate) SetNillableCreateTime(t *time.Time) *ProjectCreate {
	if t != nil {
		pc.SetCreateTime(*t)
	}
	return pc
}

// SetUser sets the "user" field.
func (pc *ProjectCreate) SetUser(s string) *ProjectCreate {
	pc.mutation.SetUser(s)
	return pc
}

// SetGroup sets the "group" field.
func (pc *ProjectCreate) SetGroup(s string) *ProjectCreate {
	pc.mutation.SetGroup(s)
	return pc
}

// SetRootPath sets the "root_path" field.
func (pc *ProjectCreate) SetRootPath(s string) *ProjectCreate {
	pc.mutation.SetRootPath(s)
	return pc
}

// SetServiceName sets the "service_name" field.
func (pc *ProjectCreate) SetServiceName(s string) *ProjectCreate {
	pc.mutation.SetServiceName(s)
	return pc
}

// SetBinaryPath sets the "binary_path" field.
func (pc *ProjectCreate) SetBinaryPath(s string) *ProjectCreate {
	pc.mutation.SetBinaryPath(s)
	return pc
}

// SetMoveToTarget sets the "move_to_target" field.
func (pc *ProjectCreate) SetMoveToTarget(b bool) *ProjectCreate {
	pc.mutation.SetMoveToTarget(b)
	return pc
}

// SetNillableMoveToTarget sets the "move_to_target" field if the given value is not nil.
func (pc *ProjectCreate) SetNillableMoveToTarget(b *bool) *ProjectCreate {
	if b != nil {
		pc.SetMoveToTarget(*b)
	}
	return pc
}

// SetBinaryTargetPath sets the "binary_target_path" field.
func (pc *ProjectCreate) SetBinaryTargetPath(s string) *ProjectCreate {
	pc.mutation.SetBinaryTargetPath(s)
	return pc
}

// SetNillableBinaryTargetPath sets the "binary_target_path" field if the given value is not nil.
func (pc *ProjectCreate) SetNillableBinaryTargetPath(s *string) *ProjectCreate {
	if s != nil {
		pc.SetBinaryTargetPath(*s)
	}
	return pc
}

// SetID sets the "id" field.
func (pc *ProjectCreate) SetID(i int) *ProjectCreate {
	pc.mutation.SetID(i)
	return pc
}

// AddLogentryIDs adds the "logentries" edge to the Logentry entity by IDs.
func (pc *ProjectCreate) AddLogentryIDs(ids ...int) *ProjectCreate {
	pc.mutation.AddLogentryIDs(ids...)
	return pc
}

// AddLogentries adds the "logentries" edges to the Logentry entity.
func (pc *ProjectCreate) AddLogentries(l ...*Logentry) *ProjectCreate {
	ids := make([]int, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return pc.AddLogentryIDs(ids...)
}

// Mutation returns the ProjectMutation object of the builder.
func (pc *ProjectCreate) Mutation() *ProjectMutation {
	return pc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (pc *ProjectCreate) defaults() {
	if _, ok := pc.mutation.CreateTime(); !ok {
		v := project.DefaultCreateTime()
		pc.mutation.SetCreateTime(v)
	}
	if _, ok := pc.mutation.MoveToTarget(); !ok {
		v := project.DefaultMoveToTarget
		pc.mutation.SetMoveToTarget(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (pc *ProjectCreate) check() error {
	if _, ok := pc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "Project.create_time"`)}
	}
	if _, ok := pc.mutation.User(); !ok {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required field "Project.user"`)}
	}
	if _, ok := pc.mutation.Group(); !ok {
		return &ValidationError{Name: "group", err: errors.New(`ent: missing required field "Project.group"`)}
	}
	if _, ok := pc.mutation.RootPath(); !ok {
		return &ValidationError{Name: "root_path", err: errors.New(`ent: missing required field "Project.root_path"`)}
	}
	if _, ok := pc.mutation.ServiceName(); !ok {
		return &ValidationError{Name: "service_name", err: errors.New(`ent: missing required field "Project.service_name"`)}
	}
	if _, ok := pc.mutation.BinaryPath(); !ok {
		return &ValidationError{Name: "binary_path", err: errors.New(`ent: missing required field "Project.binary_path"`)}
	}
	if _, ok := pc.mutation.MoveToTarget(); !ok {
		return &ValidationError{Name: "move_to_target", err: errors.New(`ent: missing required field "Project.move_to_target"`)}
	}
	return nil
}

func (pc *ProjectCreate) sqlSave(ctx context.Context) (*Project, error) {
	if err := pc.check(); err != nil {
		return nil, err
	}
	_node, _spec := pc.createSpec()
	if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = int(id)
	}
	pc.mutation.id = &_node.ID
	pc.mutation.done = true
	return _node, nil
}

func (pc *ProjectCreate) createSpec() (*Project, *sqlgraph.CreateSpec) {
	var (
		_node = &Project{config: pc.config}
		_spec = sqlgraph.NewCreateSpec(project.Table, sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt))
	)
	if id, ok := pc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := pc.mutation.CreateTime(); ok {
		_spec.SetField(project.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := pc.mutation.User(); ok {
		_spec.SetField(project.FieldUser, field.TypeString, value)
		_node.User = value
	}
	if value, ok := pc.mutation.Group(); ok {
		_spec.SetField(project.FieldGroup, field.TypeString, value)
		_node.Group = value
	}
	if value, ok := pc.mutation.RootPath(); ok {
		_spec.SetField(project.FieldRootPath, field.TypeString, value)
		_node.RootPath = value
	}
	if value, ok := pc.mutation.ServiceName(); ok {
		_spec.SetField(project.FieldServiceName, field.TypeString, value)
		_node.ServiceName = value
	}
	if value, ok := pc.mutation.BinaryPath(); ok {
		_spec.SetField(project.FieldBinaryPath, field.TypeString, value)
		_node.BinaryPath = value
	}
	if value, ok := pc.mutation.MoveToTarget(); ok {
		_spec.SetField(project.FieldMoveToTarget, field.TypeBool, value)
		_node.MoveToTarget = value
	}
	if value, ok := pc.mutation.BinaryTargetPath(); ok {
		_spec.SetField(project.FieldBinaryTargetPath, field.TypeString, value)
		_node.BinaryTargetPath = value
	}
	if nodes := pc.mutation.LogentriesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// ProjectCreateBulk is the builder for creating many Project entities in bulk.
type ProjectCreateBulk struct {
	config
	err      error
	builders []*ProjectCreate
}

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

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

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

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