// 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/so/ent/preimagerequest"
	"github.com/lightsparkdev/spark/so/ent/treenode"
	"github.com/lightsparkdev/spark/so/ent/usersignedtransaction"
)

// UserSignedTransaction is the model entity for the UserSignedTransaction schema.
type UserSignedTransaction 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"`
	// Transaction holds the value of the "transaction" field.
	Transaction []byte `json:"transaction,omitempty"`
	// UserSignature holds the value of the "user_signature" field.
	UserSignature []byte `json:"user_signature,omitempty"`
	// SigningCommitments holds the value of the "signing_commitments" field.
	SigningCommitments []byte `json:"signing_commitments,omitempty"`
	// UserSignatureCommitment holds the value of the "user_signature_commitment" field.
	UserSignatureCommitment []byte `json:"user_signature_commitment,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserSignedTransactionQuery when eager-loading is set.
	Edges                                    UserSignedTransactionEdges `json:"edges"`
	user_signed_transaction_tree_node        *uuid.UUID
	user_signed_transaction_preimage_request *uuid.UUID
	selectValues                             sql.SelectValues
}

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

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*UserSignedTransaction) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case usersignedtransaction.FieldTransaction, usersignedtransaction.FieldUserSignature, usersignedtransaction.FieldSigningCommitments, usersignedtransaction.FieldUserSignatureCommitment:
			values[i] = new([]byte)
		case usersignedtransaction.FieldCreateTime, usersignedtransaction.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case usersignedtransaction.FieldID:
			values[i] = new(uuid.UUID)
		case usersignedtransaction.ForeignKeys[0]: // user_signed_transaction_tree_node
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case usersignedtransaction.ForeignKeys[1]: // user_signed_transaction_preimage_request
			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 UserSignedTransaction fields.
func (ust *UserSignedTransaction) 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 usersignedtransaction.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				ust.ID = *value
			}
		case usersignedtransaction.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 {
				ust.CreateTime = value.Time
			}
		case usersignedtransaction.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 {
				ust.UpdateTime = value.Time
			}
		case usersignedtransaction.FieldTransaction:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field transaction", values[i])
			} else if value != nil {
				ust.Transaction = *value
			}
		case usersignedtransaction.FieldUserSignature:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field user_signature", values[i])
			} else if value != nil {
				ust.UserSignature = *value
			}
		case usersignedtransaction.FieldSigningCommitments:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field signing_commitments", values[i])
			} else if value != nil {
				ust.SigningCommitments = *value
			}
		case usersignedtransaction.FieldUserSignatureCommitment:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field user_signature_commitment", values[i])
			} else if value != nil {
				ust.UserSignatureCommitment = *value
			}
		case usersignedtransaction.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_signed_transaction_tree_node", values[i])
			} else if value.Valid {
				ust.user_signed_transaction_tree_node = new(uuid.UUID)
				*ust.user_signed_transaction_tree_node = *value.S.(*uuid.UUID)
			}
		case usersignedtransaction.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_signed_transaction_preimage_request", values[i])
			} else if value.Valid {
				ust.user_signed_transaction_preimage_request = new(uuid.UUID)
				*ust.user_signed_transaction_preimage_request = *value.S.(*uuid.UUID)
			}
		default:
			ust.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTreeNode queries the "tree_node" edge of the UserSignedTransaction entity.
func (ust *UserSignedTransaction) QueryTreeNode() *TreeNodeQuery {
	return NewUserSignedTransactionClient(ust.config).QueryTreeNode(ust)
}

// QueryPreimageRequest queries the "preimage_request" edge of the UserSignedTransaction entity.
func (ust *UserSignedTransaction) QueryPreimageRequest() *PreimageRequestQuery {
	return NewUserSignedTransactionClient(ust.config).QueryPreimageRequest(ust)
}

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

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

// String implements the fmt.Stringer.
func (ust *UserSignedTransaction) String() string {
	var builder strings.Builder
	builder.WriteString("UserSignedTransaction(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ust.ID))
	builder.WriteString("create_time=")
	builder.WriteString(ust.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(ust.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("transaction=")
	builder.WriteString(fmt.Sprintf("%v", ust.Transaction))
	builder.WriteString(", ")
	builder.WriteString("user_signature=")
	builder.WriteString(fmt.Sprintf("%v", ust.UserSignature))
	builder.WriteString(", ")
	builder.WriteString("signing_commitments=")
	builder.WriteString(fmt.Sprintf("%v", ust.SigningCommitments))
	builder.WriteString(", ")
	builder.WriteString("user_signature_commitment=")
	builder.WriteString(fmt.Sprintf("%v", ust.UserSignatureCommitment))
	builder.WriteByte(')')
	return builder.String()
}

// UserSignedTransactions is a parsable slice of UserSignedTransaction.
type UserSignedTransactions []*UserSignedTransaction
