// 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/preimagerequest"
	"github.com/lightsparkdev/spark/so/ent/preimageshare"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/transfer"
	"github.com/lightsparkdev/spark/so/ent/usersignedtransaction"
)

// PreimageRequestCreate is the builder for creating a PreimageRequest entity.
type PreimageRequestCreate struct {
	config
	mutation *PreimageRequestMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetPaymentHash sets the "payment_hash" field.
func (prc *PreimageRequestCreate) SetPaymentHash(b []byte) *PreimageRequestCreate {
	prc.mutation.SetPaymentHash(b)
	return prc
}

// SetStatus sets the "status" field.
func (prc *PreimageRequestCreate) SetStatus(srs schematype.PreimageRequestStatus) *PreimageRequestCreate {
	prc.mutation.SetStatus(srs)
	return prc
}

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (prc *PreimageRequestCreate) SetReceiverIdentityPubkey(k keys.Public) *PreimageRequestCreate {
	prc.mutation.SetReceiverIdentityPubkey(k)
	return prc
}

// SetNillableReceiverIdentityPubkey sets the "receiver_identity_pubkey" field if the given value is not nil.
func (prc *PreimageRequestCreate) SetNillableReceiverIdentityPubkey(k *keys.Public) *PreimageRequestCreate {
	if k != nil {
		prc.SetReceiverIdentityPubkey(*k)
	}
	return prc
}

// SetPreimage sets the "preimage" field.
func (prc *PreimageRequestCreate) SetPreimage(b []byte) *PreimageRequestCreate {
	prc.mutation.SetPreimage(b)
	return prc
}

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

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

// AddTransactionIDs adds the "transactions" edge to the UserSignedTransaction entity by IDs.
func (prc *PreimageRequestCreate) AddTransactionIDs(ids ...uuid.UUID) *PreimageRequestCreate {
	prc.mutation.AddTransactionIDs(ids...)
	return prc
}

// AddTransactions adds the "transactions" edges to the UserSignedTransaction entity.
func (prc *PreimageRequestCreate) AddTransactions(u ...*UserSignedTransaction) *PreimageRequestCreate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return prc.AddTransactionIDs(ids...)
}

// SetPreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID.
func (prc *PreimageRequestCreate) SetPreimageSharesID(id uuid.UUID) *PreimageRequestCreate {
	prc.mutation.SetPreimageSharesID(id)
	return prc
}

// SetNillablePreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID if the given value is not nil.
func (prc *PreimageRequestCreate) SetNillablePreimageSharesID(id *uuid.UUID) *PreimageRequestCreate {
	if id != nil {
		prc = prc.SetPreimageSharesID(*id)
	}
	return prc
}

// SetPreimageShares sets the "preimage_shares" edge to the PreimageShare entity.
func (prc *PreimageRequestCreate) SetPreimageShares(p *PreimageShare) *PreimageRequestCreate {
	return prc.SetPreimageSharesID(p.ID)
}

// SetTransfersID sets the "transfers" edge to the Transfer entity by ID.
func (prc *PreimageRequestCreate) SetTransfersID(id uuid.UUID) *PreimageRequestCreate {
	prc.mutation.SetTransfersID(id)
	return prc
}

// SetNillableTransfersID sets the "transfers" edge to the Transfer entity by ID if the given value is not nil.
func (prc *PreimageRequestCreate) SetNillableTransfersID(id *uuid.UUID) *PreimageRequestCreate {
	if id != nil {
		prc = prc.SetTransfersID(*id)
	}
	return prc
}

// SetTransfers sets the "transfers" edge to the Transfer entity.
func (prc *PreimageRequestCreate) SetTransfers(t *Transfer) *PreimageRequestCreate {
	return prc.SetTransfersID(t.ID)
}

// Mutation returns the PreimageRequestMutation object of the builder.
func (prc *PreimageRequestCreate) Mutation() *PreimageRequestMutation {
	return prc.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (prc *PreimageRequestCreate) check() error {
	if _, ok := prc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "PreimageRequest.create_time"`)}
	}
	if _, ok := prc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "PreimageRequest.update_time"`)}
	}
	if _, ok := prc.mutation.PaymentHash(); !ok {
		return &ValidationError{Name: "payment_hash", err: errors.New(`ent: missing required field "PreimageRequest.payment_hash"`)}
	}
	if v, ok := prc.mutation.PaymentHash(); ok {
		if err := preimagerequest.PaymentHashValidator(v); err != nil {
			return &ValidationError{Name: "payment_hash", err: fmt.Errorf(`ent: validator failed for field "PreimageRequest.payment_hash": %w`, err)}
		}
	}
	if _, ok := prc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "PreimageRequest.status"`)}
	}
	if v, ok := prc.mutation.Status(); ok {
		if err := preimagerequest.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PreimageRequest.status": %w`, err)}
		}
	}
	return nil
}

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

func (prc *PreimageRequestCreate) createSpec() (*PreimageRequest, *sqlgraph.CreateSpec) {
	var (
		_node = &PreimageRequest{config: prc.config}
		_spec = sqlgraph.NewCreateSpec(preimagerequest.Table, sqlgraph.NewFieldSpec(preimagerequest.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = prc.conflict
	if id, ok := prc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := prc.mutation.CreateTime(); ok {
		_spec.SetField(preimagerequest.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := prc.mutation.UpdateTime(); ok {
		_spec.SetField(preimagerequest.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := prc.mutation.PaymentHash(); ok {
		_spec.SetField(preimagerequest.FieldPaymentHash, field.TypeBytes, value)
		_node.PaymentHash = value
	}
	if value, ok := prc.mutation.Status(); ok {
		_spec.SetField(preimagerequest.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := prc.mutation.ReceiverIdentityPubkey(); ok {
		_spec.SetField(preimagerequest.FieldReceiverIdentityPubkey, field.TypeBytes, value)
		_node.ReceiverIdentityPubkey = value
	}
	if value, ok := prc.mutation.Preimage(); ok {
		_spec.SetField(preimagerequest.FieldPreimage, field.TypeBytes, value)
		_node.Preimage = value
	}
	if nodes := prc.mutation.TransactionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := prc.mutation.PreimageSharesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   preimagerequest.PreimageSharesTable,
			Columns: []string{preimagerequest.PreimageSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := prc.mutation.TransfersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   preimagerequest.TransfersTable,
			Columns: []string{preimagerequest.TransfersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.preimage_request_transfers = &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.PreimageRequest.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.PreimageRequestUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (prc *PreimageRequestCreate) OnConflict(opts ...sql.ConflictOption) *PreimageRequestUpsertOne {
	prc.conflict = opts
	return &PreimageRequestUpsertOne{
		create: prc,
	}
}

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

type (
	// PreimageRequestUpsertOne is the builder for "upsert"-ing
	//  one PreimageRequest node.
	PreimageRequestUpsertOne struct {
		create *PreimageRequestCreate
	}

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

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

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

// SetPaymentHash sets the "payment_hash" field.
func (u *PreimageRequestUpsert) SetPaymentHash(v []byte) *PreimageRequestUpsert {
	u.Set(preimagerequest.FieldPaymentHash, v)
	return u
}

// UpdatePaymentHash sets the "payment_hash" field to the value that was provided on create.
func (u *PreimageRequestUpsert) UpdatePaymentHash() *PreimageRequestUpsert {
	u.SetExcluded(preimagerequest.FieldPaymentHash)
	return u
}

// SetStatus sets the "status" field.
func (u *PreimageRequestUpsert) SetStatus(v schematype.PreimageRequestStatus) *PreimageRequestUpsert {
	u.Set(preimagerequest.FieldStatus, v)
	return u
}

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

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsert) SetReceiverIdentityPubkey(v keys.Public) *PreimageRequestUpsert {
	u.Set(preimagerequest.FieldReceiverIdentityPubkey, v)
	return u
}

// UpdateReceiverIdentityPubkey sets the "receiver_identity_pubkey" field to the value that was provided on create.
func (u *PreimageRequestUpsert) UpdateReceiverIdentityPubkey() *PreimageRequestUpsert {
	u.SetExcluded(preimagerequest.FieldReceiverIdentityPubkey)
	return u
}

// ClearReceiverIdentityPubkey clears the value of the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsert) ClearReceiverIdentityPubkey() *PreimageRequestUpsert {
	u.SetNull(preimagerequest.FieldReceiverIdentityPubkey)
	return u
}

// SetPreimage sets the "preimage" field.
func (u *PreimageRequestUpsert) SetPreimage(v []byte) *PreimageRequestUpsert {
	u.Set(preimagerequest.FieldPreimage, v)
	return u
}

// UpdatePreimage sets the "preimage" field to the value that was provided on create.
func (u *PreimageRequestUpsert) UpdatePreimage() *PreimageRequestUpsert {
	u.SetExcluded(preimagerequest.FieldPreimage)
	return u
}

// ClearPreimage clears the value of the "preimage" field.
func (u *PreimageRequestUpsert) ClearPreimage() *PreimageRequestUpsert {
	u.SetNull(preimagerequest.FieldPreimage)
	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.PreimageRequest.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(preimagerequest.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PreimageRequestUpsertOne) UpdateNewValues() *PreimageRequestUpsertOne {
	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(preimagerequest.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(preimagerequest.FieldCreateTime)
		}
	}))
	return u
}

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

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

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

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

// SetPaymentHash sets the "payment_hash" field.
func (u *PreimageRequestUpsertOne) SetPaymentHash(v []byte) *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetPaymentHash(v)
	})
}

// UpdatePaymentHash sets the "payment_hash" field to the value that was provided on create.
func (u *PreimageRequestUpsertOne) UpdatePaymentHash() *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdatePaymentHash()
	})
}

// SetStatus sets the "status" field.
func (u *PreimageRequestUpsertOne) SetStatus(v schematype.PreimageRequestStatus) *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetStatus(v)
	})
}

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

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsertOne) SetReceiverIdentityPubkey(v keys.Public) *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetReceiverIdentityPubkey(v)
	})
}

// UpdateReceiverIdentityPubkey sets the "receiver_identity_pubkey" field to the value that was provided on create.
func (u *PreimageRequestUpsertOne) UpdateReceiverIdentityPubkey() *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdateReceiverIdentityPubkey()
	})
}

// ClearReceiverIdentityPubkey clears the value of the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsertOne) ClearReceiverIdentityPubkey() *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.ClearReceiverIdentityPubkey()
	})
}

// SetPreimage sets the "preimage" field.
func (u *PreimageRequestUpsertOne) SetPreimage(v []byte) *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetPreimage(v)
	})
}

// UpdatePreimage sets the "preimage" field to the value that was provided on create.
func (u *PreimageRequestUpsertOne) UpdatePreimage() *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdatePreimage()
	})
}

// ClearPreimage clears the value of the "preimage" field.
func (u *PreimageRequestUpsertOne) ClearPreimage() *PreimageRequestUpsertOne {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.ClearPreimage()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *PreimageRequestUpsertOne) 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 *PreimageRequestUpsertOne) 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: PreimageRequestUpsertOne.ID is not supported by MySQL driver. Use PreimageRequestUpsertOne.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 *PreimageRequestUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

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

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

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

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

// PreimageRequestUpsertBulk is the builder for "upsert"-ing
// a bulk of PreimageRequest nodes.
type PreimageRequestUpsertBulk struct {
	create *PreimageRequestCreateBulk
}

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

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

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

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

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

// SetPaymentHash sets the "payment_hash" field.
func (u *PreimageRequestUpsertBulk) SetPaymentHash(v []byte) *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetPaymentHash(v)
	})
}

// UpdatePaymentHash sets the "payment_hash" field to the value that was provided on create.
func (u *PreimageRequestUpsertBulk) UpdatePaymentHash() *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdatePaymentHash()
	})
}

// SetStatus sets the "status" field.
func (u *PreimageRequestUpsertBulk) SetStatus(v schematype.PreimageRequestStatus) *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetStatus(v)
	})
}

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

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsertBulk) SetReceiverIdentityPubkey(v keys.Public) *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetReceiverIdentityPubkey(v)
	})
}

// UpdateReceiverIdentityPubkey sets the "receiver_identity_pubkey" field to the value that was provided on create.
func (u *PreimageRequestUpsertBulk) UpdateReceiverIdentityPubkey() *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdateReceiverIdentityPubkey()
	})
}

// ClearReceiverIdentityPubkey clears the value of the "receiver_identity_pubkey" field.
func (u *PreimageRequestUpsertBulk) ClearReceiverIdentityPubkey() *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.ClearReceiverIdentityPubkey()
	})
}

// SetPreimage sets the "preimage" field.
func (u *PreimageRequestUpsertBulk) SetPreimage(v []byte) *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.SetPreimage(v)
	})
}

// UpdatePreimage sets the "preimage" field to the value that was provided on create.
func (u *PreimageRequestUpsertBulk) UpdatePreimage() *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.UpdatePreimage()
	})
}

// ClearPreimage clears the value of the "preimage" field.
func (u *PreimageRequestUpsertBulk) ClearPreimage() *PreimageRequestUpsertBulk {
	return u.Update(func(s *PreimageRequestUpsert) {
		s.ClearPreimage()
	})
}

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

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