// 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/node"
	"github.com/cloudreve/Cloudreve/v4/ent/storagepolicy"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
)

// NodeCreate is the builder for creating a Node entity.
type NodeCreate struct {
	config
	mutation *NodeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// SetStatus sets the "status" field.
func (nc *NodeCreate) SetStatus(n node.Status) *NodeCreate {
	nc.mutation.SetStatus(n)
	return nc
}

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

// SetType sets the "type" field.
func (nc *NodeCreate) SetType(n node.Type) *NodeCreate {
	nc.mutation.SetType(n)
	return nc
}

// SetServer sets the "server" field.
func (nc *NodeCreate) SetServer(s string) *NodeCreate {
	nc.mutation.SetServer(s)
	return nc
}

// SetNillableServer sets the "server" field if the given value is not nil.
func (nc *NodeCreate) SetNillableServer(s *string) *NodeCreate {
	if s != nil {
		nc.SetServer(*s)
	}
	return nc
}

// SetSlaveKey sets the "slave_key" field.
func (nc *NodeCreate) SetSlaveKey(s string) *NodeCreate {
	nc.mutation.SetSlaveKey(s)
	return nc
}

// SetNillableSlaveKey sets the "slave_key" field if the given value is not nil.
func (nc *NodeCreate) SetNillableSlaveKey(s *string) *NodeCreate {
	if s != nil {
		nc.SetSlaveKey(*s)
	}
	return nc
}

// SetCapabilities sets the "capabilities" field.
func (nc *NodeCreate) SetCapabilities(bs *boolset.BooleanSet) *NodeCreate {
	nc.mutation.SetCapabilities(bs)
	return nc
}

// SetSettings sets the "settings" field.
func (nc *NodeCreate) SetSettings(ts *types.NodeSetting) *NodeCreate {
	nc.mutation.SetSettings(ts)
	return nc
}

// SetWeight sets the "weight" field.
func (nc *NodeCreate) SetWeight(i int) *NodeCreate {
	nc.mutation.SetWeight(i)
	return nc
}

// SetNillableWeight sets the "weight" field if the given value is not nil.
func (nc *NodeCreate) SetNillableWeight(i *int) *NodeCreate {
	if i != nil {
		nc.SetWeight(*i)
	}
	return nc
}

// AddStoragePolicyIDs adds the "storage_policy" edge to the StoragePolicy entity by IDs.
func (nc *NodeCreate) AddStoragePolicyIDs(ids ...int) *NodeCreate {
	nc.mutation.AddStoragePolicyIDs(ids...)
	return nc
}

// AddStoragePolicy adds the "storage_policy" edges to the StoragePolicy entity.
func (nc *NodeCreate) AddStoragePolicy(s ...*StoragePolicy) *NodeCreate {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return nc.AddStoragePolicyIDs(ids...)
}

// Mutation returns the NodeMutation object of the builder.
func (nc *NodeCreate) Mutation() *NodeMutation {
	return nc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (nc *NodeCreate) defaults() error {
	if _, ok := nc.mutation.CreatedAt(); !ok {
		if node.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized node.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := node.DefaultCreatedAt()
		nc.mutation.SetCreatedAt(v)
	}
	if _, ok := nc.mutation.UpdatedAt(); !ok {
		if node.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized node.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := node.DefaultUpdatedAt()
		nc.mutation.SetUpdatedAt(v)
	}
	if _, ok := nc.mutation.Settings(); !ok {
		v := node.DefaultSettings
		nc.mutation.SetSettings(v)
	}
	if _, ok := nc.mutation.Weight(); !ok {
		v := node.DefaultWeight
		nc.mutation.SetWeight(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (nc *NodeCreate) check() error {
	if _, ok := nc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Node.created_at"`)}
	}
	if _, ok := nc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Node.updated_at"`)}
	}
	if _, ok := nc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Node.status"`)}
	}
	if v, ok := nc.mutation.Status(); ok {
		if err := node.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Node.status": %w`, err)}
		}
	}
	if _, ok := nc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Node.name"`)}
	}
	if _, ok := nc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Node.type"`)}
	}
	if v, ok := nc.mutation.GetType(); ok {
		if err := node.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Node.type": %w`, err)}
		}
	}
	if _, ok := nc.mutation.Capabilities(); !ok {
		return &ValidationError{Name: "capabilities", err: errors.New(`ent: missing required field "Node.capabilities"`)}
	}
	if _, ok := nc.mutation.Weight(); !ok {
		return &ValidationError{Name: "weight", err: errors.New(`ent: missing required field "Node.weight"`)}
	}
	return nil
}

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

func (nc *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) {
	var (
		_node = &Node{config: nc.config}
		_spec = sqlgraph.NewCreateSpec(node.Table, sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt))
	)

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

	_spec.OnConflict = nc.conflict
	if value, ok := nc.mutation.CreatedAt(); ok {
		_spec.SetField(node.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := nc.mutation.UpdatedAt(); ok {
		_spec.SetField(node.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := nc.mutation.DeletedAt(); ok {
		_spec.SetField(node.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := nc.mutation.Status(); ok {
		_spec.SetField(node.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := nc.mutation.Name(); ok {
		_spec.SetField(node.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := nc.mutation.GetType(); ok {
		_spec.SetField(node.FieldType, field.TypeEnum, value)
		_node.Type = value
	}
	if value, ok := nc.mutation.Server(); ok {
		_spec.SetField(node.FieldServer, field.TypeString, value)
		_node.Server = value
	}
	if value, ok := nc.mutation.SlaveKey(); ok {
		_spec.SetField(node.FieldSlaveKey, field.TypeString, value)
		_node.SlaveKey = value
	}
	if value, ok := nc.mutation.Capabilities(); ok {
		_spec.SetField(node.FieldCapabilities, field.TypeBytes, value)
		_node.Capabilities = value
	}
	if value, ok := nc.mutation.Settings(); ok {
		_spec.SetField(node.FieldSettings, field.TypeJSON, value)
		_node.Settings = value
	}
	if value, ok := nc.mutation.Weight(); ok {
		_spec.SetField(node.FieldWeight, field.TypeInt, value)
		_node.Weight = value
	}
	if nodes := nc.mutation.StoragePolicyIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_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.Node.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.NodeUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (nc *NodeCreate) OnConflict(opts ...sql.ConflictOption) *NodeUpsertOne {
	nc.conflict = opts
	return &NodeUpsertOne{
		create: nc,
	}
}

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

type (
	// NodeUpsertOne is the builder for "upsert"-ing
	//  one Node node.
	NodeUpsertOne struct {
		create *NodeCreate
	}

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

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

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

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

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

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

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

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

// SetName sets the "name" field.
func (u *NodeUpsert) SetName(v string) *NodeUpsert {
	u.Set(node.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *NodeUpsert) UpdateName() *NodeUpsert {
	u.SetExcluded(node.FieldName)
	return u
}

// SetType sets the "type" field.
func (u *NodeUpsert) SetType(v node.Type) *NodeUpsert {
	u.Set(node.FieldType, v)
	return u
}

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

// SetServer sets the "server" field.
func (u *NodeUpsert) SetServer(v string) *NodeUpsert {
	u.Set(node.FieldServer, v)
	return u
}

// UpdateServer sets the "server" field to the value that was provided on create.
func (u *NodeUpsert) UpdateServer() *NodeUpsert {
	u.SetExcluded(node.FieldServer)
	return u
}

// ClearServer clears the value of the "server" field.
func (u *NodeUpsert) ClearServer() *NodeUpsert {
	u.SetNull(node.FieldServer)
	return u
}

// SetSlaveKey sets the "slave_key" field.
func (u *NodeUpsert) SetSlaveKey(v string) *NodeUpsert {
	u.Set(node.FieldSlaveKey, v)
	return u
}

// UpdateSlaveKey sets the "slave_key" field to the value that was provided on create.
func (u *NodeUpsert) UpdateSlaveKey() *NodeUpsert {
	u.SetExcluded(node.FieldSlaveKey)
	return u
}

// ClearSlaveKey clears the value of the "slave_key" field.
func (u *NodeUpsert) ClearSlaveKey() *NodeUpsert {
	u.SetNull(node.FieldSlaveKey)
	return u
}

// SetCapabilities sets the "capabilities" field.
func (u *NodeUpsert) SetCapabilities(v *boolset.BooleanSet) *NodeUpsert {
	u.Set(node.FieldCapabilities, v)
	return u
}

// UpdateCapabilities sets the "capabilities" field to the value that was provided on create.
func (u *NodeUpsert) UpdateCapabilities() *NodeUpsert {
	u.SetExcluded(node.FieldCapabilities)
	return u
}

// SetSettings sets the "settings" field.
func (u *NodeUpsert) SetSettings(v *types.NodeSetting) *NodeUpsert {
	u.Set(node.FieldSettings, v)
	return u
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *NodeUpsert) UpdateSettings() *NodeUpsert {
	u.SetExcluded(node.FieldSettings)
	return u
}

// ClearSettings clears the value of the "settings" field.
func (u *NodeUpsert) ClearSettings() *NodeUpsert {
	u.SetNull(node.FieldSettings)
	return u
}

// SetWeight sets the "weight" field.
func (u *NodeUpsert) SetWeight(v int) *NodeUpsert {
	u.Set(node.FieldWeight, v)
	return u
}

// UpdateWeight sets the "weight" field to the value that was provided on create.
func (u *NodeUpsert) UpdateWeight() *NodeUpsert {
	u.SetExcluded(node.FieldWeight)
	return u
}

// AddWeight adds v to the "weight" field.
func (u *NodeUpsert) AddWeight(v int) *NodeUpsert {
	u.Add(node.FieldWeight, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Node.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *NodeUpsertOne) UpdateNewValues() *NodeUpsertOne {
	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(node.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Node.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *NodeUpsertOne) Ignore() *NodeUpsertOne {
	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 *NodeUpsertOne) DoNothing() *NodeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

// SetStatus sets the "status" field.
func (u *NodeUpsertOne) SetStatus(v node.Status) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetStatus(v)
	})
}

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

// SetName sets the "name" field.
func (u *NodeUpsertOne) SetName(v string) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateName() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateName()
	})
}

// SetType sets the "type" field.
func (u *NodeUpsertOne) SetType(v node.Type) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetType(v)
	})
}

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

// SetServer sets the "server" field.
func (u *NodeUpsertOne) SetServer(v string) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetServer(v)
	})
}

// UpdateServer sets the "server" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateServer() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateServer()
	})
}

// ClearServer clears the value of the "server" field.
func (u *NodeUpsertOne) ClearServer() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.ClearServer()
	})
}

// SetSlaveKey sets the "slave_key" field.
func (u *NodeUpsertOne) SetSlaveKey(v string) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetSlaveKey(v)
	})
}

// UpdateSlaveKey sets the "slave_key" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateSlaveKey() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateSlaveKey()
	})
}

// ClearSlaveKey clears the value of the "slave_key" field.
func (u *NodeUpsertOne) ClearSlaveKey() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.ClearSlaveKey()
	})
}

// SetCapabilities sets the "capabilities" field.
func (u *NodeUpsertOne) SetCapabilities(v *boolset.BooleanSet) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetCapabilities(v)
	})
}

// UpdateCapabilities sets the "capabilities" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateCapabilities() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateCapabilities()
	})
}

// SetSettings sets the "settings" field.
func (u *NodeUpsertOne) SetSettings(v *types.NodeSetting) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetSettings(v)
	})
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateSettings() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateSettings()
	})
}

// ClearSettings clears the value of the "settings" field.
func (u *NodeUpsertOne) ClearSettings() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.ClearSettings()
	})
}

// SetWeight sets the "weight" field.
func (u *NodeUpsertOne) SetWeight(v int) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.SetWeight(v)
	})
}

// AddWeight adds v to the "weight" field.
func (u *NodeUpsertOne) AddWeight(v int) *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.AddWeight(v)
	})
}

// UpdateWeight sets the "weight" field to the value that was provided on create.
func (u *NodeUpsertOne) UpdateWeight() *NodeUpsertOne {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateWeight()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *NodeUpsertOne) 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 *NodeUpsertOne) 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 *NodeUpsertOne) IDX(ctx context.Context) int {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Node.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.NodeUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (ncb *NodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *NodeUpsertBulk {
	ncb.conflict = opts
	return &NodeUpsertBulk{
		create: ncb,
	}
}

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

// NodeUpsertBulk is the builder for "upsert"-ing
// a bulk of Node nodes.
type NodeUpsertBulk struct {
	create *NodeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Node.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *NodeUpsertBulk) UpdateNewValues() *NodeUpsertBulk {
	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(node.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Node.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *NodeUpsertBulk) Ignore() *NodeUpsertBulk {
	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 *NodeUpsertBulk) DoNothing() *NodeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

// SetStatus sets the "status" field.
func (u *NodeUpsertBulk) SetStatus(v node.Status) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetStatus(v)
	})
}

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

// SetName sets the "name" field.
func (u *NodeUpsertBulk) SetName(v string) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateName() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateName()
	})
}

// SetType sets the "type" field.
func (u *NodeUpsertBulk) SetType(v node.Type) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetType(v)
	})
}

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

// SetServer sets the "server" field.
func (u *NodeUpsertBulk) SetServer(v string) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetServer(v)
	})
}

// UpdateServer sets the "server" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateServer() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateServer()
	})
}

// ClearServer clears the value of the "server" field.
func (u *NodeUpsertBulk) ClearServer() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.ClearServer()
	})
}

// SetSlaveKey sets the "slave_key" field.
func (u *NodeUpsertBulk) SetSlaveKey(v string) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetSlaveKey(v)
	})
}

// UpdateSlaveKey sets the "slave_key" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateSlaveKey() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateSlaveKey()
	})
}

// ClearSlaveKey clears the value of the "slave_key" field.
func (u *NodeUpsertBulk) ClearSlaveKey() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.ClearSlaveKey()
	})
}

// SetCapabilities sets the "capabilities" field.
func (u *NodeUpsertBulk) SetCapabilities(v *boolset.BooleanSet) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetCapabilities(v)
	})
}

// UpdateCapabilities sets the "capabilities" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateCapabilities() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateCapabilities()
	})
}

// SetSettings sets the "settings" field.
func (u *NodeUpsertBulk) SetSettings(v *types.NodeSetting) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetSettings(v)
	})
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateSettings() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateSettings()
	})
}

// ClearSettings clears the value of the "settings" field.
func (u *NodeUpsertBulk) ClearSettings() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.ClearSettings()
	})
}

// SetWeight sets the "weight" field.
func (u *NodeUpsertBulk) SetWeight(v int) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.SetWeight(v)
	})
}

// AddWeight adds v to the "weight" field.
func (u *NodeUpsertBulk) AddWeight(v int) *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.AddWeight(v)
	})
}

// UpdateWeight sets the "weight" field to the value that was provided on create.
func (u *NodeUpsertBulk) UpdateWeight() *NodeUpsertBulk {
	return u.Update(func(s *NodeUpsert) {
		s.UpdateWeight()
	})
}

// Exec executes the query.
func (u *NodeUpsertBulk) 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 NodeCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for NodeCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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