// 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/tokencreate"
	"github.com/lightsparkdev/spark/so/ent/tokenmint"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
)

// TokenTransaction is the model entity for the TokenTransaction schema.
type TokenTransaction 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"`
	// PartialTokenTransactionHash holds the value of the "partial_token_transaction_hash" field.
	PartialTokenTransactionHash []byte `json:"partial_token_transaction_hash,omitempty"`
	// FinalizedTokenTransactionHash holds the value of the "finalized_token_transaction_hash" field.
	FinalizedTokenTransactionHash []byte `json:"finalized_token_transaction_hash,omitempty"`
	// OperatorSignature holds the value of the "operator_signature" field.
	OperatorSignature []byte `json:"operator_signature,omitempty"`
	// Status holds the value of the "status" field.
	Status schematype.TokenTransactionStatus `json:"status,omitempty"`
	// ExpiryTime holds the value of the "expiry_time" field.
	ExpiryTime time.Time `json:"expiry_time,omitempty"`
	// CoordinatorPublicKey holds the value of the "coordinator_public_key" field.
	CoordinatorPublicKey keys.Public `json:"coordinator_public_key,omitempty"`
	// ClientCreatedTimestamp holds the value of the "client_created_timestamp" field.
	ClientCreatedTimestamp time.Time `json:"client_created_timestamp,omitempty"`
	// Version holds the value of the "version" field.
	Version schematype.TokenTransactionVersion `json:"version,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TokenTransactionQuery when eager-loading is set.
	Edges                            TokenTransactionEdges `json:"edges"`
	token_transaction_mint           *uuid.UUID
	token_transaction_create         *uuid.UUID
	token_transaction_payment_intent *uuid.UUID
	selectValues                     sql.SelectValues
}

// TokenTransactionEdges holds the relations/edges for other nodes in the graph.
type TokenTransactionEdges struct {
	// SpentOutput holds the value of the spent_output edge.
	SpentOutput []*TokenOutput `json:"spent_output,omitempty"`
	// SpentOutputV2 holds the value of the spent_output_v2 edge.
	SpentOutputV2 []*TokenOutput `json:"spent_output_v2,omitempty"`
	// CreatedOutput holds the value of the created_output edge.
	CreatedOutput []*TokenOutput `json:"created_output,omitempty"`
	// Mint holds the value of the mint edge.
	Mint *TokenMint `json:"mint,omitempty"`
	// Create holds the value of the create edge.
	Create *TokenCreate `json:"create,omitempty"`
	// PaymentIntent holds the value of the payment_intent edge.
	PaymentIntent *PaymentIntent `json:"payment_intent,omitempty"`
	// PeerSignatures holds the value of the peer_signatures edge.
	PeerSignatures []*TokenTransactionPeerSignature `json:"peer_signatures,omitempty"`
	// SparkInvoice holds the value of the spark_invoice edge.
	SparkInvoice []*SparkInvoice `json:"spark_invoice,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [8]bool
}

// SpentOutputOrErr returns the SpentOutput value or an error if the edge
// was not loaded in eager-loading.
func (e TokenTransactionEdges) SpentOutputOrErr() ([]*TokenOutput, error) {
	if e.loadedTypes[0] {
		return e.SpentOutput, nil
	}
	return nil, &NotLoadedError{edge: "spent_output"}
}

// SpentOutputV2OrErr returns the SpentOutputV2 value or an error if the edge
// was not loaded in eager-loading.
func (e TokenTransactionEdges) SpentOutputV2OrErr() ([]*TokenOutput, error) {
	if e.loadedTypes[1] {
		return e.SpentOutputV2, nil
	}
	return nil, &NotLoadedError{edge: "spent_output_v2"}
}

// CreatedOutputOrErr returns the CreatedOutput value or an error if the edge
// was not loaded in eager-loading.
func (e TokenTransactionEdges) CreatedOutputOrErr() ([]*TokenOutput, error) {
	if e.loadedTypes[2] {
		return e.CreatedOutput, nil
	}
	return nil, &NotLoadedError{edge: "created_output"}
}

// MintOrErr returns the Mint value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TokenTransactionEdges) MintOrErr() (*TokenMint, error) {
	if e.Mint != nil {
		return e.Mint, nil
	} else if e.loadedTypes[3] {
		return nil, &NotFoundError{label: tokenmint.Label}
	}
	return nil, &NotLoadedError{edge: "mint"}
}

// CreateOrErr returns the Create value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TokenTransactionEdges) CreateOrErr() (*TokenCreate, error) {
	if e.Create != nil {
		return e.Create, nil
	} else if e.loadedTypes[4] {
		return nil, &NotFoundError{label: tokencreate.Label}
	}
	return nil, &NotLoadedError{edge: "create"}
}

// 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 TokenTransactionEdges) PaymentIntentOrErr() (*PaymentIntent, error) {
	if e.PaymentIntent != nil {
		return e.PaymentIntent, nil
	} else if e.loadedTypes[5] {
		return nil, &NotFoundError{label: paymentintent.Label}
	}
	return nil, &NotLoadedError{edge: "payment_intent"}
}

// PeerSignaturesOrErr returns the PeerSignatures value or an error if the edge
// was not loaded in eager-loading.
func (e TokenTransactionEdges) PeerSignaturesOrErr() ([]*TokenTransactionPeerSignature, error) {
	if e.loadedTypes[6] {
		return e.PeerSignatures, nil
	}
	return nil, &NotLoadedError{edge: "peer_signatures"}
}

// SparkInvoiceOrErr returns the SparkInvoice value or an error if the edge
// was not loaded in eager-loading.
func (e TokenTransactionEdges) SparkInvoiceOrErr() ([]*SparkInvoice, error) {
	if e.loadedTypes[7] {
		return e.SparkInvoice, nil
	}
	return nil, &NotLoadedError{edge: "spark_invoice"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TokenTransaction) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case tokentransaction.FieldPartialTokenTransactionHash, tokentransaction.FieldFinalizedTokenTransactionHash, tokentransaction.FieldOperatorSignature:
			values[i] = new([]byte)
		case tokentransaction.FieldCoordinatorPublicKey:
			values[i] = new(keys.Public)
		case tokentransaction.FieldVersion:
			values[i] = new(sql.NullInt64)
		case tokentransaction.FieldStatus:
			values[i] = new(sql.NullString)
		case tokentransaction.FieldCreateTime, tokentransaction.FieldUpdateTime, tokentransaction.FieldExpiryTime, tokentransaction.FieldClientCreatedTimestamp:
			values[i] = new(sql.NullTime)
		case tokentransaction.FieldID:
			values[i] = new(uuid.UUID)
		case tokentransaction.ForeignKeys[0]: // token_transaction_mint
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case tokentransaction.ForeignKeys[1]: // token_transaction_create
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case tokentransaction.ForeignKeys[2]: // token_transaction_payment_intent
			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 TokenTransaction fields.
func (tt *TokenTransaction) 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 tokentransaction.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				tt.ID = *value
			}
		case tokentransaction.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 {
				tt.CreateTime = value.Time
			}
		case tokentransaction.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 {
				tt.UpdateTime = value.Time
			}
		case tokentransaction.FieldPartialTokenTransactionHash:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field partial_token_transaction_hash", values[i])
			} else if value != nil {
				tt.PartialTokenTransactionHash = *value
			}
		case tokentransaction.FieldFinalizedTokenTransactionHash:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field finalized_token_transaction_hash", values[i])
			} else if value != nil {
				tt.FinalizedTokenTransactionHash = *value
			}
		case tokentransaction.FieldOperatorSignature:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field operator_signature", values[i])
			} else if value != nil {
				tt.OperatorSignature = *value
			}
		case tokentransaction.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				tt.Status = schematype.TokenTransactionStatus(value.String)
			}
		case tokentransaction.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 {
				tt.ExpiryTime = value.Time
			}
		case tokentransaction.FieldCoordinatorPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field coordinator_public_key", values[i])
			} else if value != nil {
				tt.CoordinatorPublicKey = *value
			}
		case tokentransaction.FieldClientCreatedTimestamp:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field client_created_timestamp", values[i])
			} else if value.Valid {
				tt.ClientCreatedTimestamp = value.Time
			}
		case tokentransaction.FieldVersion:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				tt.Version = schematype.TokenTransactionVersion(value.Int64)
			}
		case tokentransaction.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field token_transaction_mint", values[i])
			} else if value.Valid {
				tt.token_transaction_mint = new(uuid.UUID)
				*tt.token_transaction_mint = *value.S.(*uuid.UUID)
			}
		case tokentransaction.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field token_transaction_create", values[i])
			} else if value.Valid {
				tt.token_transaction_create = new(uuid.UUID)
				*tt.token_transaction_create = *value.S.(*uuid.UUID)
			}
		case tokentransaction.ForeignKeys[2]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field token_transaction_payment_intent", values[i])
			} else if value.Valid {
				tt.token_transaction_payment_intent = new(uuid.UUID)
				*tt.token_transaction_payment_intent = *value.S.(*uuid.UUID)
			}
		default:
			tt.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QuerySpentOutput queries the "spent_output" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QuerySpentOutput() *TokenOutputQuery {
	return NewTokenTransactionClient(tt.config).QuerySpentOutput(tt)
}

// QuerySpentOutputV2 queries the "spent_output_v2" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QuerySpentOutputV2() *TokenOutputQuery {
	return NewTokenTransactionClient(tt.config).QuerySpentOutputV2(tt)
}

// QueryCreatedOutput queries the "created_output" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QueryCreatedOutput() *TokenOutputQuery {
	return NewTokenTransactionClient(tt.config).QueryCreatedOutput(tt)
}

// QueryMint queries the "mint" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QueryMint() *TokenMintQuery {
	return NewTokenTransactionClient(tt.config).QueryMint(tt)
}

// QueryCreate queries the "create" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QueryCreate() *TokenCreateQuery {
	return NewTokenTransactionClient(tt.config).QueryCreate(tt)
}

// QueryPaymentIntent queries the "payment_intent" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QueryPaymentIntent() *PaymentIntentQuery {
	return NewTokenTransactionClient(tt.config).QueryPaymentIntent(tt)
}

// QueryPeerSignatures queries the "peer_signatures" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QueryPeerSignatures() *TokenTransactionPeerSignatureQuery {
	return NewTokenTransactionClient(tt.config).QueryPeerSignatures(tt)
}

// QuerySparkInvoice queries the "spark_invoice" edge of the TokenTransaction entity.
func (tt *TokenTransaction) QuerySparkInvoice() *SparkInvoiceQuery {
	return NewTokenTransactionClient(tt.config).QuerySparkInvoice(tt)
}

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

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

// String implements the fmt.Stringer.
func (tt *TokenTransaction) String() string {
	var builder strings.Builder
	builder.WriteString("TokenTransaction(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tt.ID))
	builder.WriteString("create_time=")
	builder.WriteString(tt.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(tt.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("partial_token_transaction_hash=")
	builder.WriteString(fmt.Sprintf("%v", tt.PartialTokenTransactionHash))
	builder.WriteString(", ")
	builder.WriteString("finalized_token_transaction_hash=")
	builder.WriteString(fmt.Sprintf("%v", tt.FinalizedTokenTransactionHash))
	builder.WriteString(", ")
	builder.WriteString("operator_signature=")
	builder.WriteString(fmt.Sprintf("%v", tt.OperatorSignature))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", tt.Status))
	builder.WriteString(", ")
	builder.WriteString("expiry_time=")
	builder.WriteString(tt.ExpiryTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("coordinator_public_key=")
	builder.WriteString(fmt.Sprintf("%v", tt.CoordinatorPublicKey))
	builder.WriteString(", ")
	builder.WriteString("client_created_timestamp=")
	builder.WriteString(tt.ClientCreatedTimestamp.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("version=")
	builder.WriteString(fmt.Sprintf("%v", tt.Version))
	builder.WriteByte(')')
	return builder.String()
}

// TokenTransactions is a parsable slice of TokenTransaction.
type TokenTransactions []*TokenTransaction
