// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/job"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// JobCreate is the builder for creating a Job entity.
type JobCreate struct {
	config
	mutation *JobMutation
	hooks    []Hook
}

// SetCreatedAt sets the "created_at" field.
func (jc *JobCreate) SetCreatedAt(t time.Time) *JobCreate {
	jc.mutation.SetCreatedAt(t)
	return jc
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (jc *JobCreate) SetNillableCreatedAt(t *time.Time) *JobCreate {
	if t != nil {
		jc.SetCreatedAt(*t)
	}
	return jc
}

// SetCreatedBy sets the "created_by" field.
func (jc *JobCreate) SetCreatedBy(u uint32) *JobCreate {
	jc.mutation.SetCreatedBy(u)
	return jc
}

// SetPayload sets the "payload" field.
func (jc *JobCreate) SetPayload(b []byte) *JobCreate {
	jc.mutation.SetPayload(b)
	return jc
}

// SetType sets the "type" field.
func (jc *JobCreate) SetType(s string) *JobCreate {
	jc.mutation.SetType(s)
	return jc
}

// SetNillableType sets the "type" field if the given value is not nil.
func (jc *JobCreate) SetNillableType(s *string) *JobCreate {
	if s != nil {
		jc.SetType(*s)
	}
	return jc
}

// SetUpdatedAt sets the "updated_at" field.
func (jc *JobCreate) SetUpdatedAt(t time.Time) *JobCreate {
	jc.mutation.SetUpdatedAt(t)
	return jc
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (jc *JobCreate) SetNillableUpdatedAt(t *time.Time) *JobCreate {
	if t != nil {
		jc.SetUpdatedAt(*t)
	}
	return jc
}

// SetWorker sets the "worker" field.
func (jc *JobCreate) SetWorker(u uint32) *JobCreate {
	jc.mutation.SetWorker(u)
	return jc
}

// SetDeletedAt sets the "deleted_at" field.
func (jc *JobCreate) SetDeletedAt(t time.Time) *JobCreate {
	jc.mutation.SetDeletedAt(t)
	return jc
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (jc *JobCreate) SetNillableDeletedAt(t *time.Time) *JobCreate {
	if t != nil {
		jc.SetDeletedAt(*t)
	}
	return jc
}

// SetDeletedBy sets the "deleted_by" field.
func (jc *JobCreate) SetDeletedBy(u uint32) *JobCreate {
	jc.mutation.SetDeletedBy(u)
	return jc
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (jc *JobCreate) SetNillableDeletedBy(u *uint32) *JobCreate {
	if u != nil {
		jc.SetDeletedBy(*u)
	}
	return jc
}

// SetEntryID sets the "entry_id" field.
func (jc *JobCreate) SetEntryID(s string) *JobCreate {
	jc.mutation.SetEntryID(s)
	return jc
}

// SetNillableEntryID sets the "entry_id" field if the given value is not nil.
func (jc *JobCreate) SetNillableEntryID(s *string) *JobCreate {
	if s != nil {
		jc.SetEntryID(*s)
	}
	return jc
}

// SetConfig sets the "config" field.
func (jc *JobCreate) SetConfig(b []byte) *JobCreate {
	jc.mutation.SetConfig(b)
	return jc
}

// SetTaskID sets the "task_id" field.
func (jc *JobCreate) SetTaskID(i int64) *JobCreate {
	jc.mutation.SetTaskID(i)
	return jc
}

// SetActive sets the "active" field.
func (jc *JobCreate) SetActive(b bool) *JobCreate {
	jc.mutation.SetActive(b)
	return jc
}

// SetNillableActive sets the "active" field if the given value is not nil.
func (jc *JobCreate) SetNillableActive(b *bool) *JobCreate {
	if b != nil {
		jc.SetActive(*b)
	}
	return jc
}

// SetID sets the "id" field.
func (jc *JobCreate) SetID(i int64) *JobCreate {
	jc.mutation.SetID(i)
	return jc
}

// Mutation returns the JobMutation object of the builder.
func (jc *JobCreate) Mutation() *JobMutation {
	return jc.mutation
}

// Save creates the Job in the database.
func (jc *JobCreate) Save(ctx context.Context) (*Job, error) {
	jc.defaults()
	return withHooks[*Job, JobMutation](ctx, jc.sqlSave, jc.mutation, jc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (jc *JobCreate) defaults() {
	if _, ok := jc.mutation.CreatedAt(); !ok {
		v := job.DefaultCreatedAt()
		jc.mutation.SetCreatedAt(v)
	}
	if _, ok := jc.mutation.Active(); !ok {
		v := job.DefaultActive
		jc.mutation.SetActive(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (jc *JobCreate) check() error {
	if _, ok := jc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Job.created_at"`)}
	}
	if _, ok := jc.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "Job.created_by"`)}
	}
	if _, ok := jc.mutation.Worker(); !ok {
		return &ValidationError{Name: "worker", err: errors.New(`ent: missing required field "Job.worker"`)}
	}
	if _, ok := jc.mutation.TaskID(); !ok {
		return &ValidationError{Name: "task_id", err: errors.New(`ent: missing required field "Job.task_id"`)}
	}
	if _, ok := jc.mutation.Active(); !ok {
		return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Job.active"`)}
	}
	return nil
}

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

func (jc *JobCreate) createSpec() (*Job, *sqlgraph.CreateSpec) {
	var (
		_node = &Job{config: jc.config}
		_spec = sqlgraph.NewCreateSpec(job.Table, sqlgraph.NewFieldSpec(job.FieldID, field.TypeInt64))
	)
	if id, ok := jc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := jc.mutation.CreatedAt(); ok {
		_spec.SetField(job.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := jc.mutation.CreatedBy(); ok {
		_spec.SetField(job.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := jc.mutation.Payload(); ok {
		_spec.SetField(job.FieldPayload, field.TypeBytes, value)
		_node.Payload = value
	}
	if value, ok := jc.mutation.GetType(); ok {
		_spec.SetField(job.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := jc.mutation.UpdatedAt(); ok {
		_spec.SetField(job.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := jc.mutation.Worker(); ok {
		_spec.SetField(job.FieldWorker, field.TypeUint32, value)
		_node.Worker = value
	}
	if value, ok := jc.mutation.DeletedAt(); ok {
		_spec.SetField(job.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := jc.mutation.DeletedBy(); ok {
		_spec.SetField(job.FieldDeletedBy, field.TypeUint32, value)
		_node.DeletedBy = value
	}
	if value, ok := jc.mutation.EntryID(); ok {
		_spec.SetField(job.FieldEntryID, field.TypeString, value)
		_node.EntryID = value
	}
	if value, ok := jc.mutation.Config(); ok {
		_spec.SetField(job.FieldConfig, field.TypeBytes, value)
		_node.Config = value
	}
	if value, ok := jc.mutation.TaskID(); ok {
		_spec.SetField(job.FieldTaskID, field.TypeInt64, value)
		_node.TaskID = value
	}
	if value, ok := jc.mutation.Active(); ok {
		_spec.SetField(job.FieldActive, field.TypeBool, value)
		_node.Active = value
	}
	return _node, _spec
}

// JobCreateBulk is the builder for creating many Job entities in bulk.
type JobCreateBulk struct {
	config
	builders []*JobCreate
}

// Save creates the Job entities in the database.
func (jcb *JobCreateBulk) Save(ctx context.Context) ([]*Job, error) {
	specs := make([]*sqlgraph.CreateSpec, len(jcb.builders))
	nodes := make([]*Job, len(jcb.builders))
	mutators := make([]Mutator, len(jcb.builders))
	for i := range jcb.builders {
		func(i int, root context.Context) {
			builder := jcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*JobMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				nodes[i], specs[i] = builder.createSpec()
				var err error
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, jcb.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, jcb.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 = int64(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, jcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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