// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/common/keys"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/signingkeyshare"
)

// SigningKeyshareCreate is the builder for creating a SigningKeyshare entity.
type SigningKeyshareCreate struct {
	config
	mutation *SigningKeyshareMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreateTime sets the "create_time" field.
func (skc *SigningKeyshareCreate) SetCreateTime(t time.Time) *SigningKeyshareCreate {
	skc.mutation.SetCreateTime(t)
	return skc
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (skc *SigningKeyshareCreate) SetNillableCreateTime(t *time.Time) *SigningKeyshareCreate {
	if t != nil {
		skc.SetCreateTime(*t)
	}
	return skc
}

// SetUpdateTime sets the "update_time" field.
func (skc *SigningKeyshareCreate) SetUpdateTime(t time.Time) *SigningKeyshareCreate {
	skc.mutation.SetUpdateTime(t)
	return skc
}

// SetNillableUpdateTime sets the "update_time" field if the given value is not nil.
func (skc *SigningKeyshareCreate) SetNillableUpdateTime(t *time.Time) *SigningKeyshareCreate {
	if t != nil {
		skc.SetUpdateTime(*t)
	}
	return skc
}

// SetStatus sets the "status" field.
func (skc *SigningKeyshareCreate) SetStatus(sks schematype.SigningKeyshareStatus) *SigningKeyshareCreate {
	skc.mutation.SetStatus(sks)
	return skc
}

// SetSecretShare sets the "secret_share" field.
func (skc *SigningKeyshareCreate) SetSecretShare(k keys.Private) *SigningKeyshareCreate {
	skc.mutation.SetSecretShare(k)
	return skc
}

// SetPublicShares sets the "public_shares" field.
func (skc *SigningKeyshareCreate) SetPublicShares(m map[string]keys.Public) *SigningKeyshareCreate {
	skc.mutation.SetPublicShares(m)
	return skc
}

// SetPublicKey sets the "public_key" field.
func (skc *SigningKeyshareCreate) SetPublicKey(k keys.Public) *SigningKeyshareCreate {
	skc.mutation.SetPublicKey(k)
	return skc
}

// SetMinSigners sets the "min_signers" field.
func (skc *SigningKeyshareCreate) SetMinSigners(i int32) *SigningKeyshareCreate {
	skc.mutation.SetMinSigners(i)
	return skc
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (skc *SigningKeyshareCreate) SetCoordinatorIndex(u uint64) *SigningKeyshareCreate {
	skc.mutation.SetCoordinatorIndex(u)
	return skc
}

// SetID sets the "id" field.
func (skc *SigningKeyshareCreate) SetID(u uuid.UUID) *SigningKeyshareCreate {
	skc.mutation.SetID(u)
	return skc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (skc *SigningKeyshareCreate) SetNillableID(u *uuid.UUID) *SigningKeyshareCreate {
	if u != nil {
		skc.SetID(*u)
	}
	return skc
}

// Mutation returns the SigningKeyshareMutation object of the builder.
func (skc *SigningKeyshareCreate) Mutation() *SigningKeyshareMutation {
	return skc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (skc *SigningKeyshareCreate) defaults() {
	if _, ok := skc.mutation.CreateTime(); !ok {
		v := signingkeyshare.DefaultCreateTime()
		skc.mutation.SetCreateTime(v)
	}
	if _, ok := skc.mutation.UpdateTime(); !ok {
		v := signingkeyshare.DefaultUpdateTime()
		skc.mutation.SetUpdateTime(v)
	}
	if _, ok := skc.mutation.ID(); !ok {
		v := signingkeyshare.DefaultID()
		skc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (skc *SigningKeyshareCreate) check() error {
	if _, ok := skc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "SigningKeyshare.create_time"`)}
	}
	if _, ok := skc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "SigningKeyshare.update_time"`)}
	}
	if _, ok := skc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "SigningKeyshare.status"`)}
	}
	if v, ok := skc.mutation.Status(); ok {
		if err := signingkeyshare.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "SigningKeyshare.status": %w`, err)}
		}
	}
	if _, ok := skc.mutation.SecretShare(); !ok {
		return &ValidationError{Name: "secret_share", err: errors.New(`ent: missing required field "SigningKeyshare.secret_share"`)}
	}
	if _, ok := skc.mutation.PublicShares(); !ok {
		return &ValidationError{Name: "public_shares", err: errors.New(`ent: missing required field "SigningKeyshare.public_shares"`)}
	}
	if _, ok := skc.mutation.PublicKey(); !ok {
		return &ValidationError{Name: "public_key", err: errors.New(`ent: missing required field "SigningKeyshare.public_key"`)}
	}
	if _, ok := skc.mutation.MinSigners(); !ok {
		return &ValidationError{Name: "min_signers", err: errors.New(`ent: missing required field "SigningKeyshare.min_signers"`)}
	}
	if _, ok := skc.mutation.CoordinatorIndex(); !ok {
		return &ValidationError{Name: "coordinator_index", err: errors.New(`ent: missing required field "SigningKeyshare.coordinator_index"`)}
	}
	return nil
}

func (skc *SigningKeyshareCreate) sqlSave(ctx context.Context) (*SigningKeyshare, error) {
	if err := skc.check(); err != nil {
		return nil, err
	}
	_node, _spec := skc.createSpec()
	if err := sqlgraph.CreateNode(ctx, skc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	skc.mutation.id = &_node.ID
	skc.mutation.done = true
	return _node, nil
}

func (skc *SigningKeyshareCreate) createSpec() (*SigningKeyshare, *sqlgraph.CreateSpec) {
	var (
		_node = &SigningKeyshare{config: skc.config}
		_spec = sqlgraph.NewCreateSpec(signingkeyshare.Table, sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = skc.conflict
	if id, ok := skc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := skc.mutation.CreateTime(); ok {
		_spec.SetField(signingkeyshare.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := skc.mutation.UpdateTime(); ok {
		_spec.SetField(signingkeyshare.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := skc.mutation.Status(); ok {
		_spec.SetField(signingkeyshare.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := skc.mutation.SecretShare(); ok {
		_spec.SetField(signingkeyshare.FieldSecretShare, field.TypeBytes, value)
		_node.SecretShare = value
	}
	if value, ok := skc.mutation.PublicShares(); ok {
		_spec.SetField(signingkeyshare.FieldPublicShares, field.TypeJSON, value)
		_node.PublicShares = value
	}
	if value, ok := skc.mutation.PublicKey(); ok {
		_spec.SetField(signingkeyshare.FieldPublicKey, field.TypeBytes, value)
		_node.PublicKey = value
	}
	if value, ok := skc.mutation.MinSigners(); ok {
		_spec.SetField(signingkeyshare.FieldMinSigners, field.TypeInt32, value)
		_node.MinSigners = value
	}
	if value, ok := skc.mutation.CoordinatorIndex(); ok {
		_spec.SetField(signingkeyshare.FieldCoordinatorIndex, field.TypeUint64, value)
		_node.CoordinatorIndex = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.SigningKeyshare.Create().
//		SetCreateTime(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.SigningKeyshareUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (skc *SigningKeyshareCreate) OnConflict(opts ...sql.ConflictOption) *SigningKeyshareUpsertOne {
	skc.conflict = opts
	return &SigningKeyshareUpsertOne{
		create: skc,
	}
}

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

type (
	// SigningKeyshareUpsertOne is the builder for "upsert"-ing
	//  one SigningKeyshare node.
	SigningKeyshareUpsertOne struct {
		create *SigningKeyshareCreate
	}

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

// SetUpdateTime sets the "update_time" field.
func (u *SigningKeyshareUpsert) SetUpdateTime(v time.Time) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldUpdateTime, v)
	return u
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdateUpdateTime() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldUpdateTime)
	return u
}

// SetStatus sets the "status" field.
func (u *SigningKeyshareUpsert) SetStatus(v schematype.SigningKeyshareStatus) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldStatus, v)
	return u
}

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

// SetSecretShare sets the "secret_share" field.
func (u *SigningKeyshareUpsert) SetSecretShare(v keys.Private) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldSecretShare, v)
	return u
}

// UpdateSecretShare sets the "secret_share" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdateSecretShare() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldSecretShare)
	return u
}

// SetPublicShares sets the "public_shares" field.
func (u *SigningKeyshareUpsert) SetPublicShares(v map[string]keys.Public) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldPublicShares, v)
	return u
}

// UpdatePublicShares sets the "public_shares" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdatePublicShares() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldPublicShares)
	return u
}

// SetPublicKey sets the "public_key" field.
func (u *SigningKeyshareUpsert) SetPublicKey(v keys.Public) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldPublicKey, v)
	return u
}

// UpdatePublicKey sets the "public_key" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdatePublicKey() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldPublicKey)
	return u
}

// SetMinSigners sets the "min_signers" field.
func (u *SigningKeyshareUpsert) SetMinSigners(v int32) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldMinSigners, v)
	return u
}

// UpdateMinSigners sets the "min_signers" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdateMinSigners() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldMinSigners)
	return u
}

// AddMinSigners adds v to the "min_signers" field.
func (u *SigningKeyshareUpsert) AddMinSigners(v int32) *SigningKeyshareUpsert {
	u.Add(signingkeyshare.FieldMinSigners, v)
	return u
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (u *SigningKeyshareUpsert) SetCoordinatorIndex(v uint64) *SigningKeyshareUpsert {
	u.Set(signingkeyshare.FieldCoordinatorIndex, v)
	return u
}

// UpdateCoordinatorIndex sets the "coordinator_index" field to the value that was provided on create.
func (u *SigningKeyshareUpsert) UpdateCoordinatorIndex() *SigningKeyshareUpsert {
	u.SetExcluded(signingkeyshare.FieldCoordinatorIndex)
	return u
}

// AddCoordinatorIndex adds v to the "coordinator_index" field.
func (u *SigningKeyshareUpsert) AddCoordinatorIndex(v uint64) *SigningKeyshareUpsert {
	u.Add(signingkeyshare.FieldCoordinatorIndex, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.SigningKeyshare.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(signingkeyshare.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *SigningKeyshareUpsertOne) UpdateNewValues() *SigningKeyshareUpsertOne {
	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.ID(); exists {
			s.SetIgnore(signingkeyshare.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(signingkeyshare.FieldCreateTime)
		}
	}))
	return u
}

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

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

// SetUpdateTime sets the "update_time" field.
func (u *SigningKeyshareUpsertOne) SetUpdateTime(v time.Time) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdateUpdateTime() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetStatus sets the "status" field.
func (u *SigningKeyshareUpsertOne) SetStatus(v schematype.SigningKeyshareStatus) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetStatus(v)
	})
}

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

// SetSecretShare sets the "secret_share" field.
func (u *SigningKeyshareUpsertOne) SetSecretShare(v keys.Private) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetSecretShare(v)
	})
}

// UpdateSecretShare sets the "secret_share" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdateSecretShare() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateSecretShare()
	})
}

// SetPublicShares sets the "public_shares" field.
func (u *SigningKeyshareUpsertOne) SetPublicShares(v map[string]keys.Public) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetPublicShares(v)
	})
}

// UpdatePublicShares sets the "public_shares" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdatePublicShares() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdatePublicShares()
	})
}

// SetPublicKey sets the "public_key" field.
func (u *SigningKeyshareUpsertOne) SetPublicKey(v keys.Public) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetPublicKey(v)
	})
}

// UpdatePublicKey sets the "public_key" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdatePublicKey() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdatePublicKey()
	})
}

// SetMinSigners sets the "min_signers" field.
func (u *SigningKeyshareUpsertOne) SetMinSigners(v int32) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetMinSigners(v)
	})
}

// AddMinSigners adds v to the "min_signers" field.
func (u *SigningKeyshareUpsertOne) AddMinSigners(v int32) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.AddMinSigners(v)
	})
}

// UpdateMinSigners sets the "min_signers" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdateMinSigners() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateMinSigners()
	})
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (u *SigningKeyshareUpsertOne) SetCoordinatorIndex(v uint64) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetCoordinatorIndex(v)
	})
}

// AddCoordinatorIndex adds v to the "coordinator_index" field.
func (u *SigningKeyshareUpsertOne) AddCoordinatorIndex(v uint64) *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.AddCoordinatorIndex(v)
	})
}

// UpdateCoordinatorIndex sets the "coordinator_index" field to the value that was provided on create.
func (u *SigningKeyshareUpsertOne) UpdateCoordinatorIndex() *SigningKeyshareUpsertOne {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateCoordinatorIndex()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *SigningKeyshareUpsertOne) 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 *SigningKeyshareUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: SigningKeyshareUpsertOne.ID is not supported by MySQL driver. Use SigningKeyshareUpsertOne.Exec instead")
	}
	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 *SigningKeyshareUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.SigningKeyshare.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.SigningKeyshareUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (skcb *SigningKeyshareCreateBulk) OnConflict(opts ...sql.ConflictOption) *SigningKeyshareUpsertBulk {
	skcb.conflict = opts
	return &SigningKeyshareUpsertBulk{
		create: skcb,
	}
}

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

// SigningKeyshareUpsertBulk is the builder for "upsert"-ing
// a bulk of SigningKeyshare nodes.
type SigningKeyshareUpsertBulk struct {
	create *SigningKeyshareCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.SigningKeyshare.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(signingkeyshare.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *SigningKeyshareUpsertBulk) UpdateNewValues() *SigningKeyshareUpsertBulk {
	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.ID(); exists {
				s.SetIgnore(signingkeyshare.FieldID)
			}
			if _, exists := b.mutation.CreateTime(); exists {
				s.SetIgnore(signingkeyshare.FieldCreateTime)
			}
		}
	}))
	return u
}

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

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

// SetUpdateTime sets the "update_time" field.
func (u *SigningKeyshareUpsertBulk) SetUpdateTime(v time.Time) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdateUpdateTime() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetStatus sets the "status" field.
func (u *SigningKeyshareUpsertBulk) SetStatus(v schematype.SigningKeyshareStatus) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetStatus(v)
	})
}

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

// SetSecretShare sets the "secret_share" field.
func (u *SigningKeyshareUpsertBulk) SetSecretShare(v keys.Private) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetSecretShare(v)
	})
}

// UpdateSecretShare sets the "secret_share" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdateSecretShare() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateSecretShare()
	})
}

// SetPublicShares sets the "public_shares" field.
func (u *SigningKeyshareUpsertBulk) SetPublicShares(v map[string]keys.Public) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetPublicShares(v)
	})
}

// UpdatePublicShares sets the "public_shares" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdatePublicShares() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdatePublicShares()
	})
}

// SetPublicKey sets the "public_key" field.
func (u *SigningKeyshareUpsertBulk) SetPublicKey(v keys.Public) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetPublicKey(v)
	})
}

// UpdatePublicKey sets the "public_key" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdatePublicKey() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdatePublicKey()
	})
}

// SetMinSigners sets the "min_signers" field.
func (u *SigningKeyshareUpsertBulk) SetMinSigners(v int32) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetMinSigners(v)
	})
}

// AddMinSigners adds v to the "min_signers" field.
func (u *SigningKeyshareUpsertBulk) AddMinSigners(v int32) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.AddMinSigners(v)
	})
}

// UpdateMinSigners sets the "min_signers" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdateMinSigners() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateMinSigners()
	})
}

// SetCoordinatorIndex sets the "coordinator_index" field.
func (u *SigningKeyshareUpsertBulk) SetCoordinatorIndex(v uint64) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.SetCoordinatorIndex(v)
	})
}

// AddCoordinatorIndex adds v to the "coordinator_index" field.
func (u *SigningKeyshareUpsertBulk) AddCoordinatorIndex(v uint64) *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.AddCoordinatorIndex(v)
	})
}

// UpdateCoordinatorIndex sets the "coordinator_index" field to the value that was provided on create.
func (u *SigningKeyshareUpsertBulk) UpdateCoordinatorIndex() *SigningKeyshareUpsertBulk {
	return u.Update(func(s *SigningKeyshareUpsert) {
		s.UpdateCoordinatorIndex()
	})
}

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

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