// 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/paymentintent"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/sparkinvoice"
	"github.com/lightsparkdev/spark/so/ent/transfer"
	"github.com/lightsparkdev/spark/so/ent/transferleaf"
)

// TransferCreate is the builder for creating a Transfer entity.
type TransferCreate struct {
	config
	mutation *TransferMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetSenderIdentityPubkey sets the "sender_identity_pubkey" field.
func (tc *TransferCreate) SetSenderIdentityPubkey(k keys.Public) *TransferCreate {
	tc.mutation.SetSenderIdentityPubkey(k)
	return tc
}

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

// SetTotalValue sets the "total_value" field.
func (tc *TransferCreate) SetTotalValue(u uint64) *TransferCreate {
	tc.mutation.SetTotalValue(u)
	return tc
}

// SetStatus sets the "status" field.
func (tc *TransferCreate) SetStatus(ss schematype.TransferStatus) *TransferCreate {
	tc.mutation.SetStatus(ss)
	return tc
}

// SetType sets the "type" field.
func (tc *TransferCreate) SetType(st schematype.TransferType) *TransferCreate {
	tc.mutation.SetType(st)
	return tc
}

// SetExpiryTime sets the "expiry_time" field.
func (tc *TransferCreate) SetExpiryTime(t time.Time) *TransferCreate {
	tc.mutation.SetExpiryTime(t)
	return tc
}

// SetCompletionTime sets the "completion_time" field.
func (tc *TransferCreate) SetCompletionTime(t time.Time) *TransferCreate {
	tc.mutation.SetCompletionTime(t)
	return tc
}

// SetNillableCompletionTime sets the "completion_time" field if the given value is not nil.
func (tc *TransferCreate) SetNillableCompletionTime(t *time.Time) *TransferCreate {
	if t != nil {
		tc.SetCompletionTime(*t)
	}
	return tc
}

// SetSparkInvoiceID sets the "spark_invoice_id" field.
func (tc *TransferCreate) SetSparkInvoiceID(u uuid.UUID) *TransferCreate {
	tc.mutation.SetSparkInvoiceID(u)
	return tc
}

// SetNillableSparkInvoiceID sets the "spark_invoice_id" field if the given value is not nil.
func (tc *TransferCreate) SetNillableSparkInvoiceID(u *uuid.UUID) *TransferCreate {
	if u != nil {
		tc.SetSparkInvoiceID(*u)
	}
	return tc
}

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

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

// AddTransferLeafeIDs adds the "transfer_leaves" edge to the TransferLeaf entity by IDs.
func (tc *TransferCreate) AddTransferLeafeIDs(ids ...uuid.UUID) *TransferCreate {
	tc.mutation.AddTransferLeafeIDs(ids...)
	return tc
}

// AddTransferLeaves adds the "transfer_leaves" edges to the TransferLeaf entity.
func (tc *TransferCreate) AddTransferLeaves(t ...*TransferLeaf) *TransferCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tc.AddTransferLeafeIDs(ids...)
}

// SetPaymentIntentID sets the "payment_intent" edge to the PaymentIntent entity by ID.
func (tc *TransferCreate) SetPaymentIntentID(id uuid.UUID) *TransferCreate {
	tc.mutation.SetPaymentIntentID(id)
	return tc
}

// SetNillablePaymentIntentID sets the "payment_intent" edge to the PaymentIntent entity by ID if the given value is not nil.
func (tc *TransferCreate) SetNillablePaymentIntentID(id *uuid.UUID) *TransferCreate {
	if id != nil {
		tc = tc.SetPaymentIntentID(*id)
	}
	return tc
}

// SetPaymentIntent sets the "payment_intent" edge to the PaymentIntent entity.
func (tc *TransferCreate) SetPaymentIntent(p *PaymentIntent) *TransferCreate {
	return tc.SetPaymentIntentID(p.ID)
}

// SetSparkInvoice sets the "spark_invoice" edge to the SparkInvoice entity.
func (tc *TransferCreate) SetSparkInvoice(s *SparkInvoice) *TransferCreate {
	return tc.SetSparkInvoiceID(s.ID)
}

// AddCounterSwapTransferIDs adds the "counter_swap_transfer" edge to the Transfer entity by IDs.
func (tc *TransferCreate) AddCounterSwapTransferIDs(ids ...uuid.UUID) *TransferCreate {
	tc.mutation.AddCounterSwapTransferIDs(ids...)
	return tc
}

// AddCounterSwapTransfer adds the "counter_swap_transfer" edges to the Transfer entity.
func (tc *TransferCreate) AddCounterSwapTransfer(t ...*Transfer) *TransferCreate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tc.AddCounterSwapTransferIDs(ids...)
}

// SetPrimarySwapTransferID sets the "primary_swap_transfer" edge to the Transfer entity by ID.
func (tc *TransferCreate) SetPrimarySwapTransferID(id uuid.UUID) *TransferCreate {
	tc.mutation.SetPrimarySwapTransferID(id)
	return tc
}

// SetNillablePrimarySwapTransferID sets the "primary_swap_transfer" edge to the Transfer entity by ID if the given value is not nil.
func (tc *TransferCreate) SetNillablePrimarySwapTransferID(id *uuid.UUID) *TransferCreate {
	if id != nil {
		tc = tc.SetPrimarySwapTransferID(*id)
	}
	return tc
}

// SetPrimarySwapTransfer sets the "primary_swap_transfer" edge to the Transfer entity.
func (tc *TransferCreate) SetPrimarySwapTransfer(t *Transfer) *TransferCreate {
	return tc.SetPrimarySwapTransferID(t.ID)
}

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (tc *TransferCreate) defaults() error {
	if _, ok := tc.mutation.CreateTime(); !ok {
		if transfer.DefaultCreateTime == nil {
			return fmt.Errorf("ent: uninitialized transfer.DefaultCreateTime (forgotten import ent/runtime?)")
		}
		v := transfer.DefaultCreateTime()
		tc.mutation.SetCreateTime(v)
	}
	if _, ok := tc.mutation.UpdateTime(); !ok {
		if transfer.DefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized transfer.DefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := transfer.DefaultUpdateTime()
		tc.mutation.SetUpdateTime(v)
	}
	if _, ok := tc.mutation.ID(); !ok {
		if transfer.DefaultID == nil {
			return fmt.Errorf("ent: uninitialized transfer.DefaultID (forgotten import ent/runtime?)")
		}
		v := transfer.DefaultID()
		tc.mutation.SetID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tc *TransferCreate) check() error {
	if _, ok := tc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "Transfer.create_time"`)}
	}
	if _, ok := tc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "Transfer.update_time"`)}
	}
	if _, ok := tc.mutation.SenderIdentityPubkey(); !ok {
		return &ValidationError{Name: "sender_identity_pubkey", err: errors.New(`ent: missing required field "Transfer.sender_identity_pubkey"`)}
	}
	if _, ok := tc.mutation.ReceiverIdentityPubkey(); !ok {
		return &ValidationError{Name: "receiver_identity_pubkey", err: errors.New(`ent: missing required field "Transfer.receiver_identity_pubkey"`)}
	}
	if _, ok := tc.mutation.TotalValue(); !ok {
		return &ValidationError{Name: "total_value", err: errors.New(`ent: missing required field "Transfer.total_value"`)}
	}
	if _, ok := tc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Transfer.status"`)}
	}
	if v, ok := tc.mutation.Status(); ok {
		if err := transfer.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Transfer.status": %w`, err)}
		}
	}
	if _, ok := tc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Transfer.type"`)}
	}
	if v, ok := tc.mutation.GetType(); ok {
		if err := transfer.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Transfer.type": %w`, err)}
		}
	}
	if _, ok := tc.mutation.ExpiryTime(); !ok {
		return &ValidationError{Name: "expiry_time", err: errors.New(`ent: missing required field "Transfer.expiry_time"`)}
	}
	return nil
}

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

func (tc *TransferCreate) createSpec() (*Transfer, *sqlgraph.CreateSpec) {
	var (
		_node = &Transfer{config: tc.config}
		_spec = sqlgraph.NewCreateSpec(transfer.Table, sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = tc.conflict
	if id, ok := tc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := tc.mutation.CreateTime(); ok {
		_spec.SetField(transfer.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := tc.mutation.UpdateTime(); ok {
		_spec.SetField(transfer.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := tc.mutation.SenderIdentityPubkey(); ok {
		_spec.SetField(transfer.FieldSenderIdentityPubkey, field.TypeBytes, value)
		_node.SenderIdentityPubkey = value
	}
	if value, ok := tc.mutation.ReceiverIdentityPubkey(); ok {
		_spec.SetField(transfer.FieldReceiverIdentityPubkey, field.TypeBytes, value)
		_node.ReceiverIdentityPubkey = value
	}
	if value, ok := tc.mutation.TotalValue(); ok {
		_spec.SetField(transfer.FieldTotalValue, field.TypeUint64, value)
		_node.TotalValue = value
	}
	if value, ok := tc.mutation.Status(); ok {
		_spec.SetField(transfer.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := tc.mutation.GetType(); ok {
		_spec.SetField(transfer.FieldType, field.TypeEnum, value)
		_node.Type = value
	}
	if value, ok := tc.mutation.ExpiryTime(); ok {
		_spec.SetField(transfer.FieldExpiryTime, field.TypeTime, value)
		_node.ExpiryTime = value
	}
	if value, ok := tc.mutation.CompletionTime(); ok {
		_spec.SetField(transfer.FieldCompletionTime, field.TypeTime, value)
		_node.CompletionTime = &value
	}
	if nodes := tc.mutation.TransferLeavesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   transfer.TransferLeavesTable,
			Columns: []string{transfer.TransferLeavesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transferleaf.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tc.mutation.PaymentIntentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   transfer.PaymentIntentTable,
			Columns: []string{transfer.PaymentIntentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(paymentintent.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.transfer_payment_intent = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tc.mutation.SparkInvoiceIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   transfer.SparkInvoiceTable,
			Columns: []string{transfer.SparkInvoiceColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sparkinvoice.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.SparkInvoiceID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tc.mutation.CounterSwapTransferIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   transfer.CounterSwapTransferTable,
			Columns: []string{transfer.CounterSwapTransferColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := tc.mutation.PrimarySwapTransferIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   transfer.PrimarySwapTransferTable,
			Columns: []string{transfer.PrimarySwapTransferColumn},
			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.transfer_counter_swap_transfer = &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.Transfer.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.TransferUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (tc *TransferCreate) OnConflict(opts ...sql.ConflictOption) *TransferUpsertOne {
	tc.conflict = opts
	return &TransferUpsertOne{
		create: tc,
	}
}

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

type (
	// TransferUpsertOne is the builder for "upsert"-ing
	//  one Transfer node.
	TransferUpsertOne struct {
		create *TransferCreate
	}

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

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

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

// SetTotalValue sets the "total_value" field.
func (u *TransferUpsert) SetTotalValue(v uint64) *TransferUpsert {
	u.Set(transfer.FieldTotalValue, v)
	return u
}

// UpdateTotalValue sets the "total_value" field to the value that was provided on create.
func (u *TransferUpsert) UpdateTotalValue() *TransferUpsert {
	u.SetExcluded(transfer.FieldTotalValue)
	return u
}

// AddTotalValue adds v to the "total_value" field.
func (u *TransferUpsert) AddTotalValue(v uint64) *TransferUpsert {
	u.Add(transfer.FieldTotalValue, v)
	return u
}

// SetStatus sets the "status" field.
func (u *TransferUpsert) SetStatus(v schematype.TransferStatus) *TransferUpsert {
	u.Set(transfer.FieldStatus, v)
	return u
}

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

// SetType sets the "type" field.
func (u *TransferUpsert) SetType(v schematype.TransferType) *TransferUpsert {
	u.Set(transfer.FieldType, v)
	return u
}

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

// SetCompletionTime sets the "completion_time" field.
func (u *TransferUpsert) SetCompletionTime(v time.Time) *TransferUpsert {
	u.Set(transfer.FieldCompletionTime, v)
	return u
}

// UpdateCompletionTime sets the "completion_time" field to the value that was provided on create.
func (u *TransferUpsert) UpdateCompletionTime() *TransferUpsert {
	u.SetExcluded(transfer.FieldCompletionTime)
	return u
}

// ClearCompletionTime clears the value of the "completion_time" field.
func (u *TransferUpsert) ClearCompletionTime() *TransferUpsert {
	u.SetNull(transfer.FieldCompletionTime)
	return u
}

// SetSparkInvoiceID sets the "spark_invoice_id" field.
func (u *TransferUpsert) SetSparkInvoiceID(v uuid.UUID) *TransferUpsert {
	u.Set(transfer.FieldSparkInvoiceID, v)
	return u
}

// UpdateSparkInvoiceID sets the "spark_invoice_id" field to the value that was provided on create.
func (u *TransferUpsert) UpdateSparkInvoiceID() *TransferUpsert {
	u.SetExcluded(transfer.FieldSparkInvoiceID)
	return u
}

// ClearSparkInvoiceID clears the value of the "spark_invoice_id" field.
func (u *TransferUpsert) ClearSparkInvoiceID() *TransferUpsert {
	u.SetNull(transfer.FieldSparkInvoiceID)
	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.Transfer.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(transfer.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TransferUpsertOne) UpdateNewValues() *TransferUpsertOne {
	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(transfer.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(transfer.FieldCreateTime)
		}
		if _, exists := u.create.mutation.SenderIdentityPubkey(); exists {
			s.SetIgnore(transfer.FieldSenderIdentityPubkey)
		}
		if _, exists := u.create.mutation.ReceiverIdentityPubkey(); exists {
			s.SetIgnore(transfer.FieldReceiverIdentityPubkey)
		}
		if _, exists := u.create.mutation.ExpiryTime(); exists {
			s.SetIgnore(transfer.FieldExpiryTime)
		}
	}))
	return u
}

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

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

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

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

// SetTotalValue sets the "total_value" field.
func (u *TransferUpsertOne) SetTotalValue(v uint64) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.SetTotalValue(v)
	})
}

// AddTotalValue adds v to the "total_value" field.
func (u *TransferUpsertOne) AddTotalValue(v uint64) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.AddTotalValue(v)
	})
}

// UpdateTotalValue sets the "total_value" field to the value that was provided on create.
func (u *TransferUpsertOne) UpdateTotalValue() *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateTotalValue()
	})
}

// SetStatus sets the "status" field.
func (u *TransferUpsertOne) SetStatus(v schematype.TransferStatus) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.SetStatus(v)
	})
}

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

// SetType sets the "type" field.
func (u *TransferUpsertOne) SetType(v schematype.TransferType) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.SetType(v)
	})
}

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

// SetCompletionTime sets the "completion_time" field.
func (u *TransferUpsertOne) SetCompletionTime(v time.Time) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.SetCompletionTime(v)
	})
}

// UpdateCompletionTime sets the "completion_time" field to the value that was provided on create.
func (u *TransferUpsertOne) UpdateCompletionTime() *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateCompletionTime()
	})
}

// ClearCompletionTime clears the value of the "completion_time" field.
func (u *TransferUpsertOne) ClearCompletionTime() *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.ClearCompletionTime()
	})
}

// SetSparkInvoiceID sets the "spark_invoice_id" field.
func (u *TransferUpsertOne) SetSparkInvoiceID(v uuid.UUID) *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.SetSparkInvoiceID(v)
	})
}

// UpdateSparkInvoiceID sets the "spark_invoice_id" field to the value that was provided on create.
func (u *TransferUpsertOne) UpdateSparkInvoiceID() *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateSparkInvoiceID()
	})
}

// ClearSparkInvoiceID clears the value of the "spark_invoice_id" field.
func (u *TransferUpsertOne) ClearSparkInvoiceID() *TransferUpsertOne {
	return u.Update(func(s *TransferUpsert) {
		s.ClearSparkInvoiceID()
	})
}

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

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

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

// Save creates the Transfer entities in the database.
func (tcb *TransferCreateBulk) Save(ctx context.Context) ([]*Transfer, error) {
	if tcb.err != nil {
		return nil, tcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
	nodes := make([]*Transfer, len(tcb.builders))
	mutators := make([]Mutator, len(tcb.builders))
	for i := range tcb.builders {
		func(i int, root context.Context) {
			builder := tcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*TransferMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = tcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, tcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, tcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

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

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

// TransferUpsertBulk is the builder for "upsert"-ing
// a bulk of Transfer nodes.
type TransferUpsertBulk struct {
	create *TransferCreateBulk
}

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

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

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

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

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

// SetTotalValue sets the "total_value" field.
func (u *TransferUpsertBulk) SetTotalValue(v uint64) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.SetTotalValue(v)
	})
}

// AddTotalValue adds v to the "total_value" field.
func (u *TransferUpsertBulk) AddTotalValue(v uint64) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.AddTotalValue(v)
	})
}

// UpdateTotalValue sets the "total_value" field to the value that was provided on create.
func (u *TransferUpsertBulk) UpdateTotalValue() *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateTotalValue()
	})
}

// SetStatus sets the "status" field.
func (u *TransferUpsertBulk) SetStatus(v schematype.TransferStatus) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.SetStatus(v)
	})
}

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

// SetType sets the "type" field.
func (u *TransferUpsertBulk) SetType(v schematype.TransferType) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.SetType(v)
	})
}

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

// SetCompletionTime sets the "completion_time" field.
func (u *TransferUpsertBulk) SetCompletionTime(v time.Time) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.SetCompletionTime(v)
	})
}

// UpdateCompletionTime sets the "completion_time" field to the value that was provided on create.
func (u *TransferUpsertBulk) UpdateCompletionTime() *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateCompletionTime()
	})
}

// ClearCompletionTime clears the value of the "completion_time" field.
func (u *TransferUpsertBulk) ClearCompletionTime() *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.ClearCompletionTime()
	})
}

// SetSparkInvoiceID sets the "spark_invoice_id" field.
func (u *TransferUpsertBulk) SetSparkInvoiceID(v uuid.UUID) *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.SetSparkInvoiceID(v)
	})
}

// UpdateSparkInvoiceID sets the "spark_invoice_id" field to the value that was provided on create.
func (u *TransferUpsertBulk) UpdateSparkInvoiceID() *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.UpdateSparkInvoiceID()
	})
}

// ClearSparkInvoiceID clears the value of the "spark_invoice_id" field.
func (u *TransferUpsertBulk) ClearSparkInvoiceID() *TransferUpsertBulk {
	return u.Update(func(s *TransferUpsert) {
		s.ClearSparkInvoiceID()
	})
}

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

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