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

// Holds the signatures for a token transaction from the peer operators. DO NOT WRITE an operator's own signature in this table. That already exists in the TokenTransaction table.
type TokenTransactionPeerSignature 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"`
	// OperatorIdentityPublicKey holds the value of the "operator_identity_public_key" field.
	OperatorIdentityPublicKey keys.Public `json:"operator_identity_public_key,omitempty"`
	// Signature holds the value of the "signature" field.
	Signature []byte `json:"signature,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TokenTransactionPeerSignatureQuery when eager-loading is set.
	Edges                             TokenTransactionPeerSignatureEdges `json:"edges"`
	token_transaction_peer_signatures *uuid.UUID
	selectValues                      sql.SelectValues
}

// TokenTransactionPeerSignatureEdges holds the relations/edges for other nodes in the graph.
type TokenTransactionPeerSignatureEdges struct {
	// TokenTransaction holds the value of the token_transaction edge.
	TokenTransaction *TokenTransaction `json:"token_transaction,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// TokenTransactionOrErr returns the TokenTransaction value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TokenTransactionPeerSignatureEdges) TokenTransactionOrErr() (*TokenTransaction, error) {
	if e.TokenTransaction != nil {
		return e.TokenTransaction, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: tokentransaction.Label}
	}
	return nil, &NotLoadedError{edge: "token_transaction"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TokenTransactionPeerSignature) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case tokentransactionpeersignature.FieldSignature:
			values[i] = new([]byte)
		case tokentransactionpeersignature.FieldOperatorIdentityPublicKey:
			values[i] = new(keys.Public)
		case tokentransactionpeersignature.FieldCreateTime, tokentransactionpeersignature.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case tokentransactionpeersignature.FieldID:
			values[i] = new(uuid.UUID)
		case tokentransactionpeersignature.ForeignKeys[0]: // token_transaction_peer_signatures
			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 TokenTransactionPeerSignature fields.
func (ttps *TokenTransactionPeerSignature) 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 tokentransactionpeersignature.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				ttps.ID = *value
			}
		case tokentransactionpeersignature.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 {
				ttps.CreateTime = value.Time
			}
		case tokentransactionpeersignature.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 {
				ttps.UpdateTime = value.Time
			}
		case tokentransactionpeersignature.FieldOperatorIdentityPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field operator_identity_public_key", values[i])
			} else if value != nil {
				ttps.OperatorIdentityPublicKey = *value
			}
		case tokentransactionpeersignature.FieldSignature:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field signature", values[i])
			} else if value != nil {
				ttps.Signature = *value
			}
		case tokentransactionpeersignature.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field token_transaction_peer_signatures", values[i])
			} else if value.Valid {
				ttps.token_transaction_peer_signatures = new(uuid.UUID)
				*ttps.token_transaction_peer_signatures = *value.S.(*uuid.UUID)
			}
		default:
			ttps.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTokenTransaction queries the "token_transaction" edge of the TokenTransactionPeerSignature entity.
func (ttps *TokenTransactionPeerSignature) QueryTokenTransaction() *TokenTransactionQuery {
	return NewTokenTransactionPeerSignatureClient(ttps.config).QueryTokenTransaction(ttps)
}

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

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

// String implements the fmt.Stringer.
func (ttps *TokenTransactionPeerSignature) String() string {
	var builder strings.Builder
	builder.WriteString("TokenTransactionPeerSignature(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ttps.ID))
	builder.WriteString("create_time=")
	builder.WriteString(ttps.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(ttps.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("operator_identity_public_key=")
	builder.WriteString(fmt.Sprintf("%v", ttps.OperatorIdentityPublicKey))
	builder.WriteString(", ")
	builder.WriteString("signature=")
	builder.WriteString(fmt.Sprintf("%v", ttps.Signature))
	builder.WriteByte(')')
	return builder.String()
}

// TokenTransactionPeerSignatures is a parsable slice of TokenTransactionPeerSignature.
type TokenTransactionPeerSignatures []*TokenTransactionPeerSignature
