// Code generated by ent, DO NOT EDIT.

package ent

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

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

// TestPlanCreate is the builder for creating a TestPlan entity.
type TestPlanCreate struct {
	config
	mutation *TestPlanMutation
	hooks    []Hook
}

// SetName sets the "name" field.
func (tpc *TestPlanCreate) SetName(s string) *TestPlanCreate {
	tpc.mutation.SetName(s)
	return tpc
}

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

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

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

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

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

// SetUpdatedBy sets the "updated_by" field.
func (tpc *TestPlanCreate) SetUpdatedBy(u uint32) *TestPlanCreate {
	tpc.mutation.SetUpdatedBy(u)
	return tpc
}

// SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableUpdatedBy(u *uint32) *TestPlanCreate {
	if u != nil {
		tpc.SetUpdatedBy(*u)
	}
	return tpc
}

// SetDescription sets the "description" field.
func (tpc *TestPlanCreate) SetDescription(s string) *TestPlanCreate {
	tpc.mutation.SetDescription(s)
	return tpc
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableDescription(s *string) *TestPlanCreate {
	if s != nil {
		tpc.SetDescription(*s)
	}
	return tpc
}

// SetStartTime sets the "start_time" field.
func (tpc *TestPlanCreate) SetStartTime(t time.Time) *TestPlanCreate {
	tpc.mutation.SetStartTime(t)
	return tpc
}

// SetNillableStartTime sets the "start_time" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableStartTime(t *time.Time) *TestPlanCreate {
	if t != nil {
		tpc.SetStartTime(*t)
	}
	return tpc
}

// SetDeadline sets the "deadline" field.
func (tpc *TestPlanCreate) SetDeadline(t time.Time) *TestPlanCreate {
	tpc.mutation.SetDeadline(t)
	return tpc
}

// SetNillableDeadline sets the "deadline" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableDeadline(t *time.Time) *TestPlanCreate {
	if t != nil {
		tpc.SetDeadline(*t)
	}
	return tpc
}

// SetStatusUpdatedAt sets the "status_updated_at" field.
func (tpc *TestPlanCreate) SetStatusUpdatedAt(t time.Time) *TestPlanCreate {
	tpc.mutation.SetStatusUpdatedAt(t)
	return tpc
}

// SetNillableStatusUpdatedAt sets the "status_updated_at" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableStatusUpdatedAt(t *time.Time) *TestPlanCreate {
	if t != nil {
		tpc.SetStatusUpdatedAt(*t)
	}
	return tpc
}

// SetStatus sets the "status" field.
func (tpc *TestPlanCreate) SetStatus(i int8) *TestPlanCreate {
	tpc.mutation.SetStatus(i)
	return tpc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tpc *TestPlanCreate) SetNillableStatus(i *int8) *TestPlanCreate {
	if i != nil {
		tpc.SetStatus(*i)
	}
	return tpc
}

// SetTasks sets the "tasks" field.
func (tpc *TestPlanCreate) SetTasks(i []int32) *TestPlanCreate {
	tpc.mutation.SetTasks(i)
	return tpc
}

// SetProjectID sets the "project_id" field.
func (tpc *TestPlanCreate) SetProjectID(i int32) *TestPlanCreate {
	tpc.mutation.SetProjectID(i)
	return tpc
}

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

// Mutation returns the TestPlanMutation object of the builder.
func (tpc *TestPlanCreate) Mutation() *TestPlanMutation {
	return tpc.mutation
}

// Save creates the TestPlan in the database.
func (tpc *TestPlanCreate) Save(ctx context.Context) (*TestPlan, error) {
	tpc.defaults()
	return withHooks[*TestPlan, TestPlanMutation](ctx, tpc.sqlSave, tpc.mutation, tpc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tpc *TestPlanCreate) defaults() {
	if _, ok := tpc.mutation.CreatedAt(); !ok {
		v := testplan.DefaultCreatedAt()
		tpc.mutation.SetCreatedAt(v)
	}
	if _, ok := tpc.mutation.Status(); !ok {
		v := testplan.DefaultStatus
		tpc.mutation.SetStatus(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (tpc *TestPlanCreate) check() error {
	if _, ok := tpc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "TestPlan.name"`)}
	}
	if v, ok := tpc.mutation.Name(); ok {
		if err := testplan.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "TestPlan.name": %w`, err)}
		}
	}
	if _, ok := tpc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "TestPlan.created_at"`)}
	}
	if _, ok := tpc.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "TestPlan.created_by"`)}
	}
	if _, ok := tpc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "TestPlan.status"`)}
	}
	if _, ok := tpc.mutation.ProjectID(); !ok {
		return &ValidationError{Name: "project_id", err: errors.New(`ent: missing required field "TestPlan.project_id"`)}
	}
	if v, ok := tpc.mutation.ProjectID(); ok {
		if err := testplan.ProjectIDValidator(v); err != nil {
			return &ValidationError{Name: "project_id", err: fmt.Errorf(`ent: validator failed for field "TestPlan.project_id": %w`, err)}
		}
	}
	return nil
}

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

func (tpc *TestPlanCreate) createSpec() (*TestPlan, *sqlgraph.CreateSpec) {
	var (
		_node = &TestPlan{config: tpc.config}
		_spec = sqlgraph.NewCreateSpec(testplan.Table, sqlgraph.NewFieldSpec(testplan.FieldID, field.TypeInt64))
	)
	if id, ok := tpc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := tpc.mutation.Name(); ok {
		_spec.SetField(testplan.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := tpc.mutation.CreatedAt(); ok {
		_spec.SetField(testplan.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := tpc.mutation.CreatedBy(); ok {
		_spec.SetField(testplan.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := tpc.mutation.UpdatedAt(); ok {
		_spec.SetField(testplan.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := tpc.mutation.UpdatedBy(); ok {
		_spec.SetField(testplan.FieldUpdatedBy, field.TypeUint32, value)
		_node.UpdatedBy = value
	}
	if value, ok := tpc.mutation.Description(); ok {
		_spec.SetField(testplan.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := tpc.mutation.StartTime(); ok {
		_spec.SetField(testplan.FieldStartTime, field.TypeTime, value)
		_node.StartTime = value
	}
	if value, ok := tpc.mutation.Deadline(); ok {
		_spec.SetField(testplan.FieldDeadline, field.TypeTime, value)
		_node.Deadline = value
	}
	if value, ok := tpc.mutation.StatusUpdatedAt(); ok {
		_spec.SetField(testplan.FieldStatusUpdatedAt, field.TypeTime, value)
		_node.StatusUpdatedAt = value
	}
	if value, ok := tpc.mutation.Status(); ok {
		_spec.SetField(testplan.FieldStatus, field.TypeInt8, value)
		_node.Status = value
	}
	if value, ok := tpc.mutation.Tasks(); ok {
		_spec.SetField(testplan.FieldTasks, field.TypeJSON, value)
		_node.Tasks = value
	}
	if value, ok := tpc.mutation.ProjectID(); ok {
		_spec.SetField(testplan.FieldProjectID, field.TypeInt32, value)
		_node.ProjectID = value
	}
	return _node, _spec
}

// TestPlanCreateBulk is the builder for creating many TestPlan entities in bulk.
type TestPlanCreateBulk struct {
	config
	builders []*TestPlanCreate
}

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

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

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

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