// 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"
)

// LogentryCreate is the builder for creating a Logentry entity.
type LogentryCreate struct {
	config
	mutation *LogentryMutation
	hooks    []Hook
}

// SetDate sets the "date" field.
func (lc *LogentryCreate) SetDate(t time.Time) *LogentryCreate {
	lc.mutation.SetDate(t)
	return lc
}

// SetNillableDate sets the "date" field if the given value is not nil.
func (lc *LogentryCreate) SetNillableDate(t *time.Time) *LogentryCreate {
	if t != nil {
		lc.SetDate(*t)
	}
	return lc
}

// SetContent sets the "content" field.
func (lc *LogentryCreate) SetContent(s string) *LogentryCreate {
	lc.mutation.SetContent(s)
	return lc
}

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

// SetProjectID sets the "project" edge to the Project entity by ID.
func (lc *LogentryCreate) SetProjectID(id int) *LogentryCreate {
	lc.mutation.SetProjectID(id)
	return lc
}

// SetNillableProjectID sets the "project" edge to the Project entity by ID if the given value is not nil.
func (lc *LogentryCreate) SetNillableProjectID(id *int) *LogentryCreate {
	if id != nil {
		lc = lc.SetProjectID(*id)
	}
	return lc
}

// SetProject sets the "project" edge to the Project entity.
func (lc *LogentryCreate) SetProject(p *Project) *LogentryCreate {
	return lc.SetProjectID(p.ID)
}

// Mutation returns the LogentryMutation object of the builder.
func (lc *LogentryCreate) Mutation() *LogentryMutation {
	return lc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (lc *LogentryCreate) defaults() {
	if _, ok := lc.mutation.Date(); !ok {
		v := logentry.DefaultDate()
		lc.mutation.SetDate(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (lc *LogentryCreate) check() error {
	if _, ok := lc.mutation.Date(); !ok {
		return &ValidationError{Name: "date", err: errors.New(`ent: missing required field "Logentry.date"`)}
	}
	if _, ok := lc.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Logentry.content"`)}
	}
	return nil
}

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

func (lc *LogentryCreate) createSpec() (*Logentry, *sqlgraph.CreateSpec) {
	var (
		_node = &Logentry{config: lc.config}
		_spec = sqlgraph.NewCreateSpec(logentry.Table, sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt))
	)
	if id, ok := lc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := lc.mutation.Date(); ok {
		_spec.SetField(logentry.FieldDate, field.TypeTime, value)
		_node.Date = value
	}
	if value, ok := lc.mutation.Content(); ok {
		_spec.SetField(logentry.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if nodes := lc.mutation.ProjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.ProjectTable,
			Columns: []string{logentry.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.project_logentries = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// LogentryCreateBulk is the builder for creating many Logentry entities in bulk.
type LogentryCreateBulk struct {
	config
	err      error
	builders []*LogentryCreate
}

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

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

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

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