// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/cloudreve/Cloudreve/v4/ent/task"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/gofrs/uuid"
)

// TaskCreate is the builder for creating a Task entity.
type TaskCreate struct {
	config
	mutation *TaskMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

// SetStatus sets the "status" field.
func (tc *TaskCreate) SetStatus(t task.Status) *TaskCreate {
	tc.mutation.SetStatus(t)
	return tc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tc *TaskCreate) SetNillableStatus(t *task.Status) *TaskCreate {
	if t != nil {
		tc.SetStatus(*t)
	}
	return tc
}

// SetPublicState sets the "public_state" field.
func (tc *TaskCreate) SetPublicState(tps *types.TaskPublicState) *TaskCreate {
	tc.mutation.SetPublicState(tps)
	return tc
}

// SetPrivateState sets the "private_state" field.
func (tc *TaskCreate) SetPrivateState(s string) *TaskCreate {
	tc.mutation.SetPrivateState(s)
	return tc
}

// SetNillablePrivateState sets the "private_state" field if the given value is not nil.
func (tc *TaskCreate) SetNillablePrivateState(s *string) *TaskCreate {
	if s != nil {
		tc.SetPrivateState(*s)
	}
	return tc
}

// SetCorrelationID sets the "correlation_id" field.
func (tc *TaskCreate) SetCorrelationID(u uuid.UUID) *TaskCreate {
	tc.mutation.SetCorrelationID(u)
	return tc
}

// SetNillableCorrelationID sets the "correlation_id" field if the given value is not nil.
func (tc *TaskCreate) SetNillableCorrelationID(u *uuid.UUID) *TaskCreate {
	if u != nil {
		tc.SetCorrelationID(*u)
	}
	return tc
}

// SetUserTasks sets the "user_tasks" field.
func (tc *TaskCreate) SetUserTasks(i int) *TaskCreate {
	tc.mutation.SetUserTasks(i)
	return tc
}

// SetNillableUserTasks sets the "user_tasks" field if the given value is not nil.
func (tc *TaskCreate) SetNillableUserTasks(i *int) *TaskCreate {
	if i != nil {
		tc.SetUserTasks(*i)
	}
	return tc
}

// SetUserID sets the "user" edge to the User entity by ID.
func (tc *TaskCreate) SetUserID(id int) *TaskCreate {
	tc.mutation.SetUserID(id)
	return tc
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (tc *TaskCreate) SetNillableUserID(id *int) *TaskCreate {
	if id != nil {
		tc = tc.SetUserID(*id)
	}
	return tc
}

// SetUser sets the "user" edge to the User entity.
func (tc *TaskCreate) SetUser(u *User) *TaskCreate {
	return tc.SetUserID(u.ID)
}

// Mutation returns the TaskMutation object of the builder.
func (tc *TaskCreate) Mutation() *TaskMutation {
	return tc.mutation
}

// Save creates the Task in the database.
func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) {
	if err := tc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, tc.sqlSave, tc.mutation, tc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tc *TaskCreate) defaults() error {
	if _, ok := tc.mutation.CreatedAt(); !ok {
		if task.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized task.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := task.DefaultCreatedAt()
		tc.mutation.SetCreatedAt(v)
	}
	if _, ok := tc.mutation.UpdatedAt(); !ok {
		if task.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized task.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := task.DefaultUpdatedAt()
		tc.mutation.SetUpdatedAt(v)
	}
	if _, ok := tc.mutation.Status(); !ok {
		v := task.DefaultStatus
		tc.mutation.SetStatus(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tc *TaskCreate) check() error {
	if _, ok := tc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Task.created_at"`)}
	}
	if _, ok := tc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Task.updated_at"`)}
	}
	if _, ok := tc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Task.type"`)}
	}
	if _, ok := tc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Task.status"`)}
	}
	if v, ok := tc.mutation.Status(); ok {
		if err := task.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Task.status": %w`, err)}
		}
	}
	if _, ok := tc.mutation.PublicState(); !ok {
		return &ValidationError{Name: "public_state", err: errors.New(`ent: missing required field "Task.public_state"`)}
	}
	return nil
}

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

func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) {
	var (
		_node = &Task{config: tc.config}
		_spec = sqlgraph.NewCreateSpec(task.Table, sqlgraph.NewFieldSpec(task.FieldID, field.TypeInt))
	)

	if id, ok := tc.mutation.ID(); ok {
		_node.ID = id
		id64 := int64(id)
		_spec.ID.Value = id64
	}

	_spec.OnConflict = tc.conflict
	if value, ok := tc.mutation.CreatedAt(); ok {
		_spec.SetField(task.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := tc.mutation.UpdatedAt(); ok {
		_spec.SetField(task.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := tc.mutation.DeletedAt(); ok {
		_spec.SetField(task.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := tc.mutation.GetType(); ok {
		_spec.SetField(task.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := tc.mutation.Status(); ok {
		_spec.SetField(task.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := tc.mutation.PublicState(); ok {
		_spec.SetField(task.FieldPublicState, field.TypeJSON, value)
		_node.PublicState = value
	}
	if value, ok := tc.mutation.PrivateState(); ok {
		_spec.SetField(task.FieldPrivateState, field.TypeString, value)
		_node.PrivateState = value
	}
	if value, ok := tc.mutation.CorrelationID(); ok {
		_spec.SetField(task.FieldCorrelationID, field.TypeUUID, value)
		_node.CorrelationID = value
	}
	if nodes := tc.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   task.UserTable,
			Columns: []string{task.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserTasks = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Task.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.TaskUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (tc *TaskCreate) OnConflict(opts ...sql.ConflictOption) *TaskUpsertOne {
	tc.conflict = opts
	return &TaskUpsertOne{
		create: tc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.Task.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (tc *TaskCreate) OnConflictColumns(columns ...string) *TaskUpsertOne {
	tc.conflict = append(tc.conflict, sql.ConflictColumns(columns...))
	return &TaskUpsertOne{
		create: tc,
	}
}

type (
	// TaskUpsertOne is the builder for "upsert"-ing
	//  one Task node.
	TaskUpsertOne struct {
		create *TaskCreate
	}

	// TaskUpsert is the "OnConflict" setter.
	TaskUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *TaskUpsert) SetUpdatedAt(v time.Time) *TaskUpsert {
	u.Set(task.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TaskUpsert) UpdateUpdatedAt() *TaskUpsert {
	u.SetExcluded(task.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TaskUpsert) SetDeletedAt(v time.Time) *TaskUpsert {
	u.Set(task.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TaskUpsert) UpdateDeletedAt() *TaskUpsert {
	u.SetExcluded(task.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TaskUpsert) ClearDeletedAt() *TaskUpsert {
	u.SetNull(task.FieldDeletedAt)
	return u
}

// SetType sets the "type" field.
func (u *TaskUpsert) SetType(v string) *TaskUpsert {
	u.Set(task.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *TaskUpsert) UpdateType() *TaskUpsert {
	u.SetExcluded(task.FieldType)
	return u
}

// SetStatus sets the "status" field.
func (u *TaskUpsert) SetStatus(v task.Status) *TaskUpsert {
	u.Set(task.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TaskUpsert) UpdateStatus() *TaskUpsert {
	u.SetExcluded(task.FieldStatus)
	return u
}

// SetPublicState sets the "public_state" field.
func (u *TaskUpsert) SetPublicState(v *types.TaskPublicState) *TaskUpsert {
	u.Set(task.FieldPublicState, v)
	return u
}

// UpdatePublicState sets the "public_state" field to the value that was provided on create.
func (u *TaskUpsert) UpdatePublicState() *TaskUpsert {
	u.SetExcluded(task.FieldPublicState)
	return u
}

// SetPrivateState sets the "private_state" field.
func (u *TaskUpsert) SetPrivateState(v string) *TaskUpsert {
	u.Set(task.FieldPrivateState, v)
	return u
}

// UpdatePrivateState sets the "private_state" field to the value that was provided on create.
func (u *TaskUpsert) UpdatePrivateState() *TaskUpsert {
	u.SetExcluded(task.FieldPrivateState)
	return u
}

// ClearPrivateState clears the value of the "private_state" field.
func (u *TaskUpsert) ClearPrivateState() *TaskUpsert {
	u.SetNull(task.FieldPrivateState)
	return u
}

// SetUserTasks sets the "user_tasks" field.
func (u *TaskUpsert) SetUserTasks(v int) *TaskUpsert {
	u.Set(task.FieldUserTasks, v)
	return u
}

// UpdateUserTasks sets the "user_tasks" field to the value that was provided on create.
func (u *TaskUpsert) UpdateUserTasks() *TaskUpsert {
	u.SetExcluded(task.FieldUserTasks)
	return u
}

// ClearUserTasks clears the value of the "user_tasks" field.
func (u *TaskUpsert) ClearUserTasks() *TaskUpsert {
	u.SetNull(task.FieldUserTasks)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Task.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *TaskUpsertOne) UpdateNewValues() *TaskUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(task.FieldCreatedAt)
		}
		if _, exists := u.create.mutation.CorrelationID(); exists {
			s.SetIgnore(task.FieldCorrelationID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Task.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *TaskUpsertOne) Ignore() *TaskUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TaskUpsertOne) DoNothing() *TaskUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the TaskCreate.OnConflict
// documentation for more info.
func (u *TaskUpsertOne) Update(set func(*TaskUpsert)) *TaskUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&TaskUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *TaskUpsertOne) SetUpdatedAt(v time.Time) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateUpdatedAt() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TaskUpsertOne) SetDeletedAt(v time.Time) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateDeletedAt() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TaskUpsertOne) ClearDeletedAt() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.ClearDeletedAt()
	})
}

// SetType sets the "type" field.
func (u *TaskUpsertOne) SetType(v string) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateType() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateType()
	})
}

// SetStatus sets the "status" field.
func (u *TaskUpsertOne) SetStatus(v task.Status) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateStatus() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateStatus()
	})
}

// SetPublicState sets the "public_state" field.
func (u *TaskUpsertOne) SetPublicState(v *types.TaskPublicState) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetPublicState(v)
	})
}

// UpdatePublicState sets the "public_state" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdatePublicState() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdatePublicState()
	})
}

// SetPrivateState sets the "private_state" field.
func (u *TaskUpsertOne) SetPrivateState(v string) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetPrivateState(v)
	})
}

// UpdatePrivateState sets the "private_state" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdatePrivateState() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdatePrivateState()
	})
}

// ClearPrivateState clears the value of the "private_state" field.
func (u *TaskUpsertOne) ClearPrivateState() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.ClearPrivateState()
	})
}

// SetUserTasks sets the "user_tasks" field.
func (u *TaskUpsertOne) SetUserTasks(v int) *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.SetUserTasks(v)
	})
}

// UpdateUserTasks sets the "user_tasks" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateUserTasks() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateUserTasks()
	})
}

// ClearUserTasks clears the value of the "user_tasks" field.
func (u *TaskUpsertOne) ClearUserTasks() *TaskUpsertOne {
	return u.Update(func(s *TaskUpsert) {
		s.ClearUserTasks()
	})
}

// Exec executes the query.
func (u *TaskUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TaskCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *TaskUpsertOne) ID(ctx context.Context) (id int, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *TaskUpsertOne) IDX(ctx context.Context) int {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

func (m *TaskCreate) SetRawID(t int) *TaskCreate {
	m.mutation.SetRawID(t)
	return m
}

// TaskCreateBulk is the builder for creating many Task entities in bulk.
type TaskCreateBulk struct {
	config
	err      error
	builders []*TaskCreate
	conflict []sql.ConflictOption
}

// Save creates the Task entities in the database.
func (tcb *TaskCreateBulk) Save(ctx context.Context) ([]*Task, error) {
	if tcb.err != nil {
		return nil, tcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
	nodes := make([]*Task, len(tcb.builders))
	mutators := make([]Mutator, len(tcb.builders))
	for i := range tcb.builders {
		func(i int, root context.Context) {
			builder := tcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*TaskMutation)
				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, tcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = tcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, tcb.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, tcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Task.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.TaskUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (tcb *TaskCreateBulk) OnConflict(opts ...sql.ConflictOption) *TaskUpsertBulk {
	tcb.conflict = opts
	return &TaskUpsertBulk{
		create: tcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.Task.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (tcb *TaskCreateBulk) OnConflictColumns(columns ...string) *TaskUpsertBulk {
	tcb.conflict = append(tcb.conflict, sql.ConflictColumns(columns...))
	return &TaskUpsertBulk{
		create: tcb,
	}
}

// TaskUpsertBulk is the builder for "upsert"-ing
// a bulk of Task nodes.
type TaskUpsertBulk struct {
	create *TaskCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Task.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *TaskUpsertBulk) UpdateNewValues() *TaskUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(task.FieldCreatedAt)
			}
			if _, exists := b.mutation.CorrelationID(); exists {
				s.SetIgnore(task.FieldCorrelationID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Task.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *TaskUpsertBulk) Ignore() *TaskUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TaskUpsertBulk) DoNothing() *TaskUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the TaskCreateBulk.OnConflict
// documentation for more info.
func (u *TaskUpsertBulk) Update(set func(*TaskUpsert)) *TaskUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&TaskUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *TaskUpsertBulk) SetUpdatedAt(v time.Time) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateUpdatedAt() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TaskUpsertBulk) SetDeletedAt(v time.Time) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateDeletedAt() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TaskUpsertBulk) ClearDeletedAt() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.ClearDeletedAt()
	})
}

// SetType sets the "type" field.
func (u *TaskUpsertBulk) SetType(v string) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateType() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateType()
	})
}

// SetStatus sets the "status" field.
func (u *TaskUpsertBulk) SetStatus(v task.Status) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateStatus() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateStatus()
	})
}

// SetPublicState sets the "public_state" field.
func (u *TaskUpsertBulk) SetPublicState(v *types.TaskPublicState) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetPublicState(v)
	})
}

// UpdatePublicState sets the "public_state" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdatePublicState() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdatePublicState()
	})
}

// SetPrivateState sets the "private_state" field.
func (u *TaskUpsertBulk) SetPrivateState(v string) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetPrivateState(v)
	})
}

// UpdatePrivateState sets the "private_state" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdatePrivateState() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdatePrivateState()
	})
}

// ClearPrivateState clears the value of the "private_state" field.
func (u *TaskUpsertBulk) ClearPrivateState() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.ClearPrivateState()
	})
}

// SetUserTasks sets the "user_tasks" field.
func (u *TaskUpsertBulk) SetUserTasks(v int) *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.SetUserTasks(v)
	})
}

// UpdateUserTasks sets the "user_tasks" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateUserTasks() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.UpdateUserTasks()
	})
}

// ClearUserTasks clears the value of the "user_tasks" field.
func (u *TaskUpsertBulk) ClearUserTasks() *TaskUpsertBulk {
	return u.Update(func(s *TaskUpsert) {
		s.ClearUserTasks()
	})
}

// Exec executes the query.
func (u *TaskUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the TaskCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TaskCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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