// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/TSELab/astra/internal/store/ent/step"
)

// StepCreate is the builder for creating a Step entity.
type StepCreate struct {
	config
	mutation *StepMutation
	hooks    []Hook
}

// SetAstraID sets the "astra_id" field.
func (_c *StepCreate) SetAstraID(v string) *StepCreate {
	_c.mutation.SetAstraID(v)
	return _c
}

// SetCommand sets the "command" field.
func (_c *StepCreate) SetCommand(v string) *StepCreate {
	_c.mutation.SetCommand(v)
	return _c
}

// SetNillableCommand sets the "command" field if the given value is not nil.
func (_c *StepCreate) SetNillableCommand(v *string) *StepCreate {
	if v != nil {
		_c.SetCommand(*v)
	}
	return _c
}

// SetEnvironment sets the "environment" field.
func (_c *StepCreate) SetEnvironment(v map[string]string) *StepCreate {
	_c.mutation.SetEnvironment(v)
	return _c
}

// SetArch sets the "Arch" field.
func (_c *StepCreate) SetArch(v string) *StepCreate {
	_c.mutation.SetArch(v)
	return _c
}

// SetNillableArch sets the "Arch" field if the given value is not nil.
func (_c *StepCreate) SetNillableArch(v *string) *StepCreate {
	if v != nil {
		_c.SetArch(*v)
	}
	return _c
}

// SetTimestamp sets the "timestamp" field.
func (_c *StepCreate) SetTimestamp(v string) *StepCreate {
	_c.mutation.SetTimestamp(v)
	return _c
}

// SetMetadata sets the "metadata" field.
func (_c *StepCreate) SetMetadata(v map[string]string) *StepCreate {
	_c.mutation.SetMetadata(v)
	return _c
}

// SetCompleteness sets the "completeness" field.
func (_c *StepCreate) SetCompleteness(v step.Completeness) *StepCreate {
	_c.mutation.SetCompleteness(v)
	return _c
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_c *StepCreate) SetNillableCompleteness(v *step.Completeness) *StepCreate {
	if v != nil {
		_c.SetCompleteness(*v)
	}
	return _c
}

// SetCreatedAt sets the "created_at" field.
func (_c *StepCreate) SetCreatedAt(v time.Time) *StepCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// Mutation returns the StepMutation object of the builder.
func (_c *StepCreate) Mutation() *StepMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *StepCreate) defaults() {
	if _, ok := _c.mutation.Command(); !ok {
		v := step.DefaultCommand
		_c.mutation.SetCommand(v)
	}
	if _, ok := _c.mutation.Arch(); !ok {
		v := step.DefaultArch
		_c.mutation.SetArch(v)
	}
	if _, ok := _c.mutation.Completeness(); !ok {
		v := step.DefaultCompleteness
		_c.mutation.SetCompleteness(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := step.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *StepCreate) check() error {
	if _, ok := _c.mutation.AstraID(); !ok {
		return &ValidationError{Name: "astra_id", err: errors.New(`ent: missing required field "Step.astra_id"`)}
	}
	if v, ok := _c.mutation.AstraID(); ok {
		if err := step.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Step.astra_id": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Command(); !ok {
		return &ValidationError{Name: "command", err: errors.New(`ent: missing required field "Step.command"`)}
	}
	if _, ok := _c.mutation.Environment(); !ok {
		return &ValidationError{Name: "environment", err: errors.New(`ent: missing required field "Step.environment"`)}
	}
	if _, ok := _c.mutation.Arch(); !ok {
		return &ValidationError{Name: "Arch", err: errors.New(`ent: missing required field "Step.Arch"`)}
	}
	if _, ok := _c.mutation.Timestamp(); !ok {
		return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "Step.timestamp"`)}
	}
	if _, ok := _c.mutation.Completeness(); !ok {
		return &ValidationError{Name: "completeness", err: errors.New(`ent: missing required field "Step.completeness"`)}
	}
	if v, ok := _c.mutation.Completeness(); ok {
		if err := step.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Step.completeness": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Step.created_at"`)}
	}
	return nil
}

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

func (_c *StepCreate) createSpec() (*Step, *sqlgraph.CreateSpec) {
	var (
		_node = &Step{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(step.Table, sqlgraph.NewFieldSpec(step.FieldID, field.TypeInt))
	)
	if value, ok := _c.mutation.AstraID(); ok {
		_spec.SetField(step.FieldAstraID, field.TypeString, value)
		_node.AstraID = value
	}
	if value, ok := _c.mutation.Command(); ok {
		_spec.SetField(step.FieldCommand, field.TypeString, value)
		_node.Command = value
	}
	if value, ok := _c.mutation.Environment(); ok {
		_spec.SetField(step.FieldEnvironment, field.TypeJSON, value)
		_node.Environment = value
	}
	if value, ok := _c.mutation.Arch(); ok {
		_spec.SetField(step.FieldArch, field.TypeString, value)
		_node.Arch = value
	}
	if value, ok := _c.mutation.Timestamp(); ok {
		_spec.SetField(step.FieldTimestamp, field.TypeString, value)
		_node.Timestamp = value
	}
	if value, ok := _c.mutation.Metadata(); ok {
		_spec.SetField(step.FieldMetadata, field.TypeJSON, value)
		_node.Metadata = value
	}
	if value, ok := _c.mutation.Completeness(); ok {
		_spec.SetField(step.FieldCompleteness, field.TypeEnum, value)
		_node.Completeness = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(step.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	return _node, _spec
}

// StepCreateBulk is the builder for creating many Step entities in bulk.
type StepCreateBulk struct {
	config
	err      error
	builders []*StepCreate
}

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

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

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

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