// 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/sparkinvoice"
)

// SparkInvoice is the model entity for the SparkInvoice schema.
type SparkInvoice 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 raw invoice string
	SparkInvoice string `json:"spark_invoice,omitempty"`
	// The expiry time of the invoice
	ExpiryTime time.Time `json:"expiry_time,omitempty"`
	// The public key of the receiver of the invoice
	ReceiverPublicKey keys.Public `json:"receiver_public_key,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the SparkInvoiceQuery when eager-loading is set.
	Edges        SparkInvoiceEdges `json:"edges"`
	selectValues sql.SelectValues
}

// SparkInvoiceEdges holds the relations/edges for other nodes in the graph.
type SparkInvoiceEdges struct {
	// The token transaction this invoice paid. Only set for invoices that paid a token transaction.
	TokenTransaction []*TokenTransaction `json:"token_transaction,omitempty"`
	// The sats transfer this invoice paid. Only set for invoices that paid a sats transfer.
	Transfer []*Transfer `json:"transfer,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
}

// TokenTransactionOrErr returns the TokenTransaction value or an error if the edge
// was not loaded in eager-loading.
func (e SparkInvoiceEdges) TokenTransactionOrErr() ([]*TokenTransaction, error) {
	if e.loadedTypes[0] {
		return e.TokenTransaction, nil
	}
	return nil, &NotLoadedError{edge: "token_transaction"}
}

// TransferOrErr returns the Transfer value or an error if the edge
// was not loaded in eager-loading.
func (e SparkInvoiceEdges) TransferOrErr() ([]*Transfer, error) {
	if e.loadedTypes[1] {
		return e.Transfer, nil
	}
	return nil, &NotLoadedError{edge: "transfer"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*SparkInvoice) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case sparkinvoice.FieldReceiverPublicKey:
			values[i] = new(keys.Public)
		case sparkinvoice.FieldSparkInvoice:
			values[i] = new(sql.NullString)
		case sparkinvoice.FieldCreateTime, sparkinvoice.FieldUpdateTime, sparkinvoice.FieldExpiryTime:
			values[i] = new(sql.NullTime)
		case sparkinvoice.FieldID:
			values[i] = 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 SparkInvoice fields.
func (si *SparkInvoice) 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 sparkinvoice.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				si.ID = *value
			}
		case sparkinvoice.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 {
				si.CreateTime = value.Time
			}
		case sparkinvoice.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 {
				si.UpdateTime = value.Time
			}
		case sparkinvoice.FieldSparkInvoice:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field spark_invoice", values[i])
			} else if value.Valid {
				si.SparkInvoice = value.String
			}
		case sparkinvoice.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 {
				si.ExpiryTime = value.Time
			}
		case sparkinvoice.FieldReceiverPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field receiver_public_key", values[i])
			} else if value != nil {
				si.ReceiverPublicKey = *value
			}
		default:
			si.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTokenTransaction queries the "token_transaction" edge of the SparkInvoice entity.
func (si *SparkInvoice) QueryTokenTransaction() *TokenTransactionQuery {
	return NewSparkInvoiceClient(si.config).QueryTokenTransaction(si)
}

// QueryTransfer queries the "transfer" edge of the SparkInvoice entity.
func (si *SparkInvoice) QueryTransfer() *TransferQuery {
	return NewSparkInvoiceClient(si.config).QueryTransfer(si)
}

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

// Unwrap unwraps the SparkInvoice 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 (si *SparkInvoice) Unwrap() *SparkInvoice {
	_tx, ok := si.config.driver.(*txDriver)
	if !ok {
		panic("ent: SparkInvoice is not a transactional entity")
	}
	si.config.driver = _tx.drv
	return si
}

// String implements the fmt.Stringer.
func (si *SparkInvoice) String() string {
	var builder strings.Builder
	builder.WriteString("SparkInvoice(")
	builder.WriteString(fmt.Sprintf("id=%v, ", si.ID))
	builder.WriteString("create_time=")
	builder.WriteString(si.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(si.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("spark_invoice=")
	builder.WriteString(si.SparkInvoice)
	builder.WriteString(", ")
	builder.WriteString("expiry_time=")
	builder.WriteString(si.ExpiryTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("receiver_public_key=")
	builder.WriteString(fmt.Sprintf("%v", si.ReceiverPublicKey))
	builder.WriteByte(')')
	return builder.String()
}

// SparkInvoices is a parsable slice of SparkInvoice.
type SparkInvoices []*SparkInvoice
