// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"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"
)

// Transfer is the model entity for the Transfer schema.
type Transfer struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// CreateTime holds the value of the "create_time" field.
	CreateTime time.Time `json:"create_time,omitempty"`
	// UpdateTime holds the value of the "update_time" field.
	UpdateTime time.Time `json:"update_time,omitempty"`
	// The identity public key of the sender of the transfer.
	SenderIdentityPubkey keys.Public `json:"sender_identity_pubkey,omitempty"`
	// ReceiverIdentityPubkey holds the value of the "receiver_identity_pubkey" field.
	ReceiverIdentityPubkey keys.Public `json:"receiver_identity_pubkey,omitempty"`
	// TotalValue holds the value of the "total_value" field.
	TotalValue uint64 `json:"total_value,omitempty"`
	// Status holds the value of the "status" field.
	Status schematype.TransferStatus `json:"status,omitempty"`
	// Type holds the value of the "type" field.
	Type schematype.TransferType `json:"type,omitempty"`
	// ExpiryTime holds the value of the "expiry_time" field.
	ExpiryTime time.Time `json:"expiry_time,omitempty"`
	// CompletionTime holds the value of the "completion_time" field.
	CompletionTime *time.Time `json:"completion_time,omitempty"`
	// Foreign key to spark_invoice
	SparkInvoiceID uuid.UUID `json:"spark_invoice_id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TransferQuery when eager-loading is set.
	Edges                          TransferEdges `json:"edges"`
	transfer_payment_intent        *uuid.UUID
	transfer_counter_swap_transfer *uuid.UUID
	selectValues                   sql.SelectValues
}

// TransferEdges holds the relations/edges for other nodes in the graph.
type TransferEdges struct {
	// TransferLeaves holds the value of the transfer_leaves edge.
	TransferLeaves []*TransferLeaf `json:"transfer_leaves,omitempty"`
	// PaymentIntent holds the value of the payment_intent edge.
	PaymentIntent *PaymentIntent `json:"payment_intent,omitempty"`
	// Invoice that this transfer pays. Only set for transfers that paid an invoice.
	SparkInvoice *SparkInvoice `json:"spark_invoice,omitempty"`
	// For SWAP type transfer, this field references the corresponding counter transfer (type COUNTER_SWAP), which will establish this edge automatically upon creation.
	CounterSwapTransfer []*Transfer `json:"counter_swap_transfer,omitempty"`
	// For counter transfers of type COUNTER_SWAP, this field references the corresponding primary transfer (type SWAP) that initiated the atomic swap. There are multiple counter transfers possible for a single primary transfer, because if a counter transfer fails the SSP will create a new one.
	PrimarySwapTransfer *Transfer `json:"primary_swap_transfer,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [5]bool
}

// TransferLeavesOrErr returns the TransferLeaves value or an error if the edge
// was not loaded in eager-loading.
func (e TransferEdges) TransferLeavesOrErr() ([]*TransferLeaf, error) {
	if e.loadedTypes[0] {
		return e.TransferLeaves, nil
	}
	return nil, &NotLoadedError{edge: "transfer_leaves"}
}

// PaymentIntentOrErr returns the PaymentIntent value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TransferEdges) PaymentIntentOrErr() (*PaymentIntent, error) {
	if e.PaymentIntent != nil {
		return e.PaymentIntent, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: paymentintent.Label}
	}
	return nil, &NotLoadedError{edge: "payment_intent"}
}

// SparkInvoiceOrErr returns the SparkInvoice value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TransferEdges) SparkInvoiceOrErr() (*SparkInvoice, error) {
	if e.SparkInvoice != nil {
		return e.SparkInvoice, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: sparkinvoice.Label}
	}
	return nil, &NotLoadedError{edge: "spark_invoice"}
}

// CounterSwapTransferOrErr returns the CounterSwapTransfer value or an error if the edge
// was not loaded in eager-loading.
func (e TransferEdges) CounterSwapTransferOrErr() ([]*Transfer, error) {
	if e.loadedTypes[3] {
		return e.CounterSwapTransfer, nil
	}
	return nil, &NotLoadedError{edge: "counter_swap_transfer"}
}

// PrimarySwapTransferOrErr returns the PrimarySwapTransfer value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TransferEdges) PrimarySwapTransferOrErr() (*Transfer, error) {
	if e.PrimarySwapTransfer != nil {
		return e.PrimarySwapTransfer, nil
	} else if e.loadedTypes[4] {
		return nil, &NotFoundError{label: transfer.Label}
	}
	return nil, &NotLoadedError{edge: "primary_swap_transfer"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Transfer) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case transfer.FieldSenderIdentityPubkey, transfer.FieldReceiverIdentityPubkey:
			values[i] = new(keys.Public)
		case transfer.FieldTotalValue:
			values[i] = new(sql.NullInt64)
		case transfer.FieldStatus, transfer.FieldType:
			values[i] = new(sql.NullString)
		case transfer.FieldCreateTime, transfer.FieldUpdateTime, transfer.FieldExpiryTime, transfer.FieldCompletionTime:
			values[i] = new(sql.NullTime)
		case transfer.FieldID, transfer.FieldSparkInvoiceID:
			values[i] = new(uuid.UUID)
		case transfer.ForeignKeys[0]: // transfer_payment_intent
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case transfer.ForeignKeys[1]: // transfer_counter_swap_transfer
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Transfer fields.
func (t *Transfer) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case transfer.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				t.ID = *value
			}
		case transfer.FieldCreateTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field create_time", values[i])
			} else if value.Valid {
				t.CreateTime = value.Time
			}
		case transfer.FieldUpdateTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field update_time", values[i])
			} else if value.Valid {
				t.UpdateTime = value.Time
			}
		case transfer.FieldSenderIdentityPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field sender_identity_pubkey", values[i])
			} else if value != nil {
				t.SenderIdentityPubkey = *value
			}
		case transfer.FieldReceiverIdentityPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field receiver_identity_pubkey", values[i])
			} else if value != nil {
				t.ReceiverIdentityPubkey = *value
			}
		case transfer.FieldTotalValue:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total_value", values[i])
			} else if value.Valid {
				t.TotalValue = uint64(value.Int64)
			}
		case transfer.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				t.Status = schematype.TransferStatus(value.String)
			}
		case transfer.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				t.Type = schematype.TransferType(value.String)
			}
		case transfer.FieldExpiryTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expiry_time", values[i])
			} else if value.Valid {
				t.ExpiryTime = value.Time
			}
		case transfer.FieldCompletionTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field completion_time", values[i])
			} else if value.Valid {
				t.CompletionTime = new(time.Time)
				*t.CompletionTime = value.Time
			}
		case transfer.FieldSparkInvoiceID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field spark_invoice_id", values[i])
			} else if value != nil {
				t.SparkInvoiceID = *value
			}
		case transfer.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field transfer_payment_intent", values[i])
			} else if value.Valid {
				t.transfer_payment_intent = new(uuid.UUID)
				*t.transfer_payment_intent = *value.S.(*uuid.UUID)
			}
		case transfer.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field transfer_counter_swap_transfer", values[i])
			} else if value.Valid {
				t.transfer_counter_swap_transfer = new(uuid.UUID)
				*t.transfer_counter_swap_transfer = *value.S.(*uuid.UUID)
			}
		default:
			t.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Transfer.
// This includes values selected through modifiers, order, etc.
func (t *Transfer) Value(name string) (ent.Value, error) {
	return t.selectValues.Get(name)
}

// QueryTransferLeaves queries the "transfer_leaves" edge of the Transfer entity.
func (t *Transfer) QueryTransferLeaves() *TransferLeafQuery {
	return NewTransferClient(t.config).QueryTransferLeaves(t)
}

// QueryPaymentIntent queries the "payment_intent" edge of the Transfer entity.
func (t *Transfer) QueryPaymentIntent() *PaymentIntentQuery {
	return NewTransferClient(t.config).QueryPaymentIntent(t)
}

// QuerySparkInvoice queries the "spark_invoice" edge of the Transfer entity.
func (t *Transfer) QuerySparkInvoice() *SparkInvoiceQuery {
	return NewTransferClient(t.config).QuerySparkInvoice(t)
}

// QueryCounterSwapTransfer queries the "counter_swap_transfer" edge of the Transfer entity.
func (t *Transfer) QueryCounterSwapTransfer() *TransferQuery {
	return NewTransferClient(t.config).QueryCounterSwapTransfer(t)
}

// QueryPrimarySwapTransfer queries the "primary_swap_transfer" edge of the Transfer entity.
func (t *Transfer) QueryPrimarySwapTransfer() *TransferQuery {
	return NewTransferClient(t.config).QueryPrimarySwapTransfer(t)
}

// Update returns a builder for updating this Transfer.
// Note that you need to call Transfer.Unwrap() before calling this method if this Transfer
// was returned from a transaction, and the transaction was committed or rolled back.
func (t *Transfer) Update() *TransferUpdateOne {
	return NewTransferClient(t.config).UpdateOne(t)
}

// Unwrap unwraps the Transfer entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (t *Transfer) Unwrap() *Transfer {
	_tx, ok := t.config.driver.(*txDriver)
	if !ok {
		panic("ent: Transfer is not a transactional entity")
	}
	t.config.driver = _tx.drv
	return t
}

// String implements the fmt.Stringer.
func (t *Transfer) String() string {
	var builder strings.Builder
	builder.WriteString("Transfer(")
	builder.WriteString(fmt.Sprintf("id=%v, ", t.ID))
	builder.WriteString("create_time=")
	builder.WriteString(t.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(t.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("sender_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", t.SenderIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("receiver_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", t.ReceiverIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("total_value=")
	builder.WriteString(fmt.Sprintf("%v", t.TotalValue))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", t.Status))
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", t.Type))
	builder.WriteString(", ")
	builder.WriteString("expiry_time=")
	builder.WriteString(t.ExpiryTime.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := t.CompletionTime; v != nil {
		builder.WriteString("completion_time=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("spark_invoice_id=")
	builder.WriteString(fmt.Sprintf("%v", t.SparkInvoiceID))
	builder.WriteByte(')')
	return builder.String()
}

// Transfers is a parsable slice of Transfer.
type Transfers []*Transfer
