// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/so/ent/paymentintent"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"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"
)

// TransferUpdate is the builder for updating Transfer entities.
type TransferUpdate struct {
	config
	hooks    []Hook
	mutation *TransferMutation
}

// Where appends a list predicates to the TransferUpdate builder.
func (tu *TransferUpdate) Where(ps ...predicate.Transfer) *TransferUpdate {
	tu.mutation.Where(ps...)
	return tu
}

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

// SetTotalValue sets the "total_value" field.
func (tu *TransferUpdate) SetTotalValue(u uint64) *TransferUpdate {
	tu.mutation.ResetTotalValue()
	tu.mutation.SetTotalValue(u)
	return tu
}

// SetNillableTotalValue sets the "total_value" field if the given value is not nil.
func (tu *TransferUpdate) SetNillableTotalValue(u *uint64) *TransferUpdate {
	if u != nil {
		tu.SetTotalValue(*u)
	}
	return tu
}

// AddTotalValue adds u to the "total_value" field.
func (tu *TransferUpdate) AddTotalValue(u int64) *TransferUpdate {
	tu.mutation.AddTotalValue(u)
	return tu
}

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

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tu *TransferUpdate) SetNillableStatus(ss *schematype.TransferStatus) *TransferUpdate {
	if ss != nil {
		tu.SetStatus(*ss)
	}
	return tu
}

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

// SetNillableType sets the "type" field if the given value is not nil.
func (tu *TransferUpdate) SetNillableType(st *schematype.TransferType) *TransferUpdate {
	if st != nil {
		tu.SetType(*st)
	}
	return tu
}

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

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

// ClearCompletionTime clears the value of the "completion_time" field.
func (tu *TransferUpdate) ClearCompletionTime() *TransferUpdate {
	tu.mutation.ClearCompletionTime()
	return tu
}

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

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

// ClearSparkInvoiceID clears the value of the "spark_invoice_id" field.
func (tu *TransferUpdate) ClearSparkInvoiceID() *TransferUpdate {
	tu.mutation.ClearSparkInvoiceID()
	return tu
}

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

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

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

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

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

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

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

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

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

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

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

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

// ClearTransferLeaves clears all "transfer_leaves" edges to the TransferLeaf entity.
func (tu *TransferUpdate) ClearTransferLeaves() *TransferUpdate {
	tu.mutation.ClearTransferLeaves()
	return tu
}

// RemoveTransferLeafeIDs removes the "transfer_leaves" edge to TransferLeaf entities by IDs.
func (tu *TransferUpdate) RemoveTransferLeafeIDs(ids ...uuid.UUID) *TransferUpdate {
	tu.mutation.RemoveTransferLeafeIDs(ids...)
	return tu
}

// RemoveTransferLeaves removes "transfer_leaves" edges to TransferLeaf entities.
func (tu *TransferUpdate) RemoveTransferLeaves(t ...*TransferLeaf) *TransferUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tu.RemoveTransferLeafeIDs(ids...)
}

// ClearPaymentIntent clears the "payment_intent" edge to the PaymentIntent entity.
func (tu *TransferUpdate) ClearPaymentIntent() *TransferUpdate {
	tu.mutation.ClearPaymentIntent()
	return tu
}

// ClearSparkInvoice clears the "spark_invoice" edge to the SparkInvoice entity.
func (tu *TransferUpdate) ClearSparkInvoice() *TransferUpdate {
	tu.mutation.ClearSparkInvoice()
	return tu
}

// ClearCounterSwapTransfer clears all "counter_swap_transfer" edges to the Transfer entity.
func (tu *TransferUpdate) ClearCounterSwapTransfer() *TransferUpdate {
	tu.mutation.ClearCounterSwapTransfer()
	return tu
}

// RemoveCounterSwapTransferIDs removes the "counter_swap_transfer" edge to Transfer entities by IDs.
func (tu *TransferUpdate) RemoveCounterSwapTransferIDs(ids ...uuid.UUID) *TransferUpdate {
	tu.mutation.RemoveCounterSwapTransferIDs(ids...)
	return tu
}

// RemoveCounterSwapTransfer removes "counter_swap_transfer" edges to Transfer entities.
func (tu *TransferUpdate) RemoveCounterSwapTransfer(t ...*Transfer) *TransferUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tu.RemoveCounterSwapTransferIDs(ids...)
}

// ClearPrimarySwapTransfer clears the "primary_swap_transfer" edge to the Transfer entity.
func (tu *TransferUpdate) ClearPrimarySwapTransfer() *TransferUpdate {
	tu.mutation.ClearPrimarySwapTransfer()
	return tu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (tu *TransferUpdate) Save(ctx context.Context) (int, error) {
	if err := tu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tu *TransferUpdate) SaveX(ctx context.Context) int {
	affected, err := tu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (tu *TransferUpdate) defaults() error {
	if _, ok := tu.mutation.UpdateTime(); !ok {
		if transfer.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized transfer.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := transfer.UpdateDefaultUpdateTime()
		tu.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tu *TransferUpdate) check() error {
	if v, ok := tu.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 v, ok := tu.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)}
		}
	}
	return nil
}

func (tu *TransferUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(transfer.Table, transfer.Columns, sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID))
	if ps := tu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tu.mutation.UpdateTime(); ok {
		_spec.SetField(transfer.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tu.mutation.TotalValue(); ok {
		_spec.SetField(transfer.FieldTotalValue, field.TypeUint64, value)
	}
	if value, ok := tu.mutation.AddedTotalValue(); ok {
		_spec.AddField(transfer.FieldTotalValue, field.TypeUint64, value)
	}
	if value, ok := tu.mutation.Status(); ok {
		_spec.SetField(transfer.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tu.mutation.GetType(); ok {
		_spec.SetField(transfer.FieldType, field.TypeEnum, value)
	}
	if value, ok := tu.mutation.CompletionTime(); ok {
		_spec.SetField(transfer.FieldCompletionTime, field.TypeTime, value)
	}
	if tu.mutation.CompletionTimeCleared() {
		_spec.ClearField(transfer.FieldCompletionTime, field.TypeTime)
	}
	if tu.mutation.TransferLeavesCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.RemovedTransferLeavesIDs(); len(nodes) > 0 && !tu.mutation.TransferLeavesCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.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.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.PaymentIntentCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.SparkInvoiceCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.CounterSwapTransferCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.RemovedCounterSwapTransferIDs(); len(nodes) > 0 && !tu.mutation.CounterSwapTransferCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.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.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.PrimarySwapTransferCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{transfer.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tu.mutation.done = true
	return n, nil
}

// TransferUpdateOne is the builder for updating a single Transfer entity.
type TransferUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TransferMutation
}

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

// SetTotalValue sets the "total_value" field.
func (tuo *TransferUpdateOne) SetTotalValue(u uint64) *TransferUpdateOne {
	tuo.mutation.ResetTotalValue()
	tuo.mutation.SetTotalValue(u)
	return tuo
}

// SetNillableTotalValue sets the "total_value" field if the given value is not nil.
func (tuo *TransferUpdateOne) SetNillableTotalValue(u *uint64) *TransferUpdateOne {
	if u != nil {
		tuo.SetTotalValue(*u)
	}
	return tuo
}

// AddTotalValue adds u to the "total_value" field.
func (tuo *TransferUpdateOne) AddTotalValue(u int64) *TransferUpdateOne {
	tuo.mutation.AddTotalValue(u)
	return tuo
}

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

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tuo *TransferUpdateOne) SetNillableStatus(ss *schematype.TransferStatus) *TransferUpdateOne {
	if ss != nil {
		tuo.SetStatus(*ss)
	}
	return tuo
}

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

// SetNillableType sets the "type" field if the given value is not nil.
func (tuo *TransferUpdateOne) SetNillableType(st *schematype.TransferType) *TransferUpdateOne {
	if st != nil {
		tuo.SetType(*st)
	}
	return tuo
}

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

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

// ClearCompletionTime clears the value of the "completion_time" field.
func (tuo *TransferUpdateOne) ClearCompletionTime() *TransferUpdateOne {
	tuo.mutation.ClearCompletionTime()
	return tuo
}

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

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

// ClearSparkInvoiceID clears the value of the "spark_invoice_id" field.
func (tuo *TransferUpdateOne) ClearSparkInvoiceID() *TransferUpdateOne {
	tuo.mutation.ClearSparkInvoiceID()
	return tuo
}

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

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

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

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

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

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

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

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

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

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

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

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

// ClearTransferLeaves clears all "transfer_leaves" edges to the TransferLeaf entity.
func (tuo *TransferUpdateOne) ClearTransferLeaves() *TransferUpdateOne {
	tuo.mutation.ClearTransferLeaves()
	return tuo
}

// RemoveTransferLeafeIDs removes the "transfer_leaves" edge to TransferLeaf entities by IDs.
func (tuo *TransferUpdateOne) RemoveTransferLeafeIDs(ids ...uuid.UUID) *TransferUpdateOne {
	tuo.mutation.RemoveTransferLeafeIDs(ids...)
	return tuo
}

// RemoveTransferLeaves removes "transfer_leaves" edges to TransferLeaf entities.
func (tuo *TransferUpdateOne) RemoveTransferLeaves(t ...*TransferLeaf) *TransferUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tuo.RemoveTransferLeafeIDs(ids...)
}

// ClearPaymentIntent clears the "payment_intent" edge to the PaymentIntent entity.
func (tuo *TransferUpdateOne) ClearPaymentIntent() *TransferUpdateOne {
	tuo.mutation.ClearPaymentIntent()
	return tuo
}

// ClearSparkInvoice clears the "spark_invoice" edge to the SparkInvoice entity.
func (tuo *TransferUpdateOne) ClearSparkInvoice() *TransferUpdateOne {
	tuo.mutation.ClearSparkInvoice()
	return tuo
}

// ClearCounterSwapTransfer clears all "counter_swap_transfer" edges to the Transfer entity.
func (tuo *TransferUpdateOne) ClearCounterSwapTransfer() *TransferUpdateOne {
	tuo.mutation.ClearCounterSwapTransfer()
	return tuo
}

// RemoveCounterSwapTransferIDs removes the "counter_swap_transfer" edge to Transfer entities by IDs.
func (tuo *TransferUpdateOne) RemoveCounterSwapTransferIDs(ids ...uuid.UUID) *TransferUpdateOne {
	tuo.mutation.RemoveCounterSwapTransferIDs(ids...)
	return tuo
}

// RemoveCounterSwapTransfer removes "counter_swap_transfer" edges to Transfer entities.
func (tuo *TransferUpdateOne) RemoveCounterSwapTransfer(t ...*Transfer) *TransferUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tuo.RemoveCounterSwapTransferIDs(ids...)
}

// ClearPrimarySwapTransfer clears the "primary_swap_transfer" edge to the Transfer entity.
func (tuo *TransferUpdateOne) ClearPrimarySwapTransfer() *TransferUpdateOne {
	tuo.mutation.ClearPrimarySwapTransfer()
	return tuo
}

// Where appends a list predicates to the TransferUpdate builder.
func (tuo *TransferUpdateOne) Where(ps ...predicate.Transfer) *TransferUpdateOne {
	tuo.mutation.Where(ps...)
	return tuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (tuo *TransferUpdateOne) Select(field string, fields ...string) *TransferUpdateOne {
	tuo.fields = append([]string{field}, fields...)
	return tuo
}

// Save executes the query and returns the updated Transfer entity.
func (tuo *TransferUpdateOne) Save(ctx context.Context) (*Transfer, error) {
	if err := tuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tuo *TransferUpdateOne) SaveX(ctx context.Context) *Transfer {
	node, err := tuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (tuo *TransferUpdateOne) Exec(ctx context.Context) error {
	_, err := tuo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (tuo *TransferUpdateOne) defaults() error {
	if _, ok := tuo.mutation.UpdateTime(); !ok {
		if transfer.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized transfer.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := transfer.UpdateDefaultUpdateTime()
		tuo.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tuo *TransferUpdateOne) check() error {
	if v, ok := tuo.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 v, ok := tuo.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)}
		}
	}
	return nil
}

func (tuo *TransferUpdateOne) sqlSave(ctx context.Context) (_node *Transfer, err error) {
	if err := tuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(transfer.Table, transfer.Columns, sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID))
	id, ok := tuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Transfer.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, transfer.FieldID)
		for _, f := range fields {
			if !transfer.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != transfer.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tuo.mutation.UpdateTime(); ok {
		_spec.SetField(transfer.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tuo.mutation.TotalValue(); ok {
		_spec.SetField(transfer.FieldTotalValue, field.TypeUint64, value)
	}
	if value, ok := tuo.mutation.AddedTotalValue(); ok {
		_spec.AddField(transfer.FieldTotalValue, field.TypeUint64, value)
	}
	if value, ok := tuo.mutation.Status(); ok {
		_spec.SetField(transfer.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tuo.mutation.GetType(); ok {
		_spec.SetField(transfer.FieldType, field.TypeEnum, value)
	}
	if value, ok := tuo.mutation.CompletionTime(); ok {
		_spec.SetField(transfer.FieldCompletionTime, field.TypeTime, value)
	}
	if tuo.mutation.CompletionTimeCleared() {
		_spec.ClearField(transfer.FieldCompletionTime, field.TypeTime)
	}
	if tuo.mutation.TransferLeavesCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.RemovedTransferLeavesIDs(); len(nodes) > 0 && !tuo.mutation.TransferLeavesCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.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.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.PaymentIntentCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.SparkInvoiceCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.CounterSwapTransferCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.RemovedCounterSwapTransferIDs(); len(nodes) > 0 && !tuo.mutation.CounterSwapTransferCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.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.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.PrimarySwapTransferCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Transfer{config: tuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{transfer.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tuo.mutation.done = true
	return _node, nil
}
