// 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/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/signingkeyshare"
	"github.com/lightsparkdev/spark/so/ent/tree"
	"github.com/lightsparkdev/spark/so/ent/treenode"
)

// TreeNode is the model entity for the TreeNode schema.
type TreeNode 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"`
	// Value holds the value of the "value" field.
	Value uint64 `json:"value,omitempty"`
	// Status holds the value of the "status" field.
	Status schematype.TreeNodeStatus `json:"status,omitempty"`
	// VerifyingPubkey holds the value of the "verifying_pubkey" field.
	VerifyingPubkey keys.Public `json:"verifying_pubkey,omitempty"`
	// OwnerIdentityPubkey holds the value of the "owner_identity_pubkey" field.
	OwnerIdentityPubkey keys.Public `json:"owner_identity_pubkey,omitempty"`
	// OwnerSigningPubkey holds the value of the "owner_signing_pubkey" field.
	OwnerSigningPubkey keys.Public `json:"owner_signing_pubkey,omitempty"`
	// Vout holds the value of the "vout" field.
	Vout int16 `json:"vout,omitempty"`
	// NodeConfirmationHeight holds the value of the "node_confirmation_height" field.
	NodeConfirmationHeight uint64 `json:"node_confirmation_height,omitempty"`
	// RefundConfirmationHeight holds the value of the "refund_confirmation_height" field.
	RefundConfirmationHeight uint64 `json:"refund_confirmation_height,omitempty"`
	// RawTx holds the value of the "raw_tx" field.
	RawTx []byte `json:"raw_tx,omitempty"`
	// DirectTx holds the value of the "direct_tx" field.
	DirectTx []byte `json:"direct_tx,omitempty"`
	// DirectFromCpfpRefundTx holds the value of the "direct_from_cpfp_refund_tx" field.
	DirectFromCpfpRefundTx []byte `json:"direct_from_cpfp_refund_tx,omitempty"`
	// Valid transaction ID of the stored node transaction. Generated by hooks from raw_tx.
	RawTxid []byte `json:"raw_txid,omitempty"`
	// Valid transaction ID of the stored direct node transaction. Generated by hooks from direct_tx.
	DirectTxid []byte `json:"direct_txid,omitempty"`
	// Valid transaction ID of the stored direct from CPFP node transaction. Generated by hooks from direct_from_cpfp_refund_tx.
	DirectFromCpfpRefundTxid []byte `json:"direct_from_cpfp_refund_txid,omitempty"`
	// A transaction to exit Spark unilaterally. Only leafs have this transaction.
	RawRefundTx []byte `json:"raw_refund_tx,omitempty"`
	// DirectRefundTx holds the value of the "direct_refund_tx" field.
	DirectRefundTx []byte `json:"direct_refund_tx,omitempty"`
	// Valid transaction ID of the stored refund transaction. Generated by hooks from raw_refund_tx.
	RawRefundTxid []byte `json:"raw_refund_txid,omitempty"`
	// Valid transaction ID of the direct refund transaction. Generated by hooks from direct_refund_tx.
	DirectRefundTxid []byte `json:"direct_refund_txid,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TreeNodeQuery when eager-loading is set.
	Edges                      TreeNodeEdges `json:"edges"`
	tree_node_tree             *uuid.UUID
	tree_node_parent           *uuid.UUID
	tree_node_signing_keyshare *uuid.UUID
	selectValues               sql.SelectValues
}

// TreeNodeEdges holds the relations/edges for other nodes in the graph.
type TreeNodeEdges struct {
	// Tree holds the value of the tree edge.
	Tree *Tree `json:"tree,omitempty"`
	// Parent holds the value of the parent edge.
	Parent *TreeNode `json:"parent,omitempty"`
	// SigningKeyshare holds the value of the signing_keyshare edge.
	SigningKeyshare *SigningKeyshare `json:"signing_keyshare,omitempty"`
	// Children holds the value of the children edge.
	Children []*TreeNode `json:"children,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
}

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

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

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

// ChildrenOrErr returns the Children value or an error if the edge
// was not loaded in eager-loading.
func (e TreeNodeEdges) ChildrenOrErr() ([]*TreeNode, error) {
	if e.loadedTypes[3] {
		return e.Children, nil
	}
	return nil, &NotLoadedError{edge: "children"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TreeNode) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case treenode.FieldRawTx, treenode.FieldDirectTx, treenode.FieldDirectFromCpfpRefundTx, treenode.FieldRawTxid, treenode.FieldDirectTxid, treenode.FieldDirectFromCpfpRefundTxid, treenode.FieldRawRefundTx, treenode.FieldDirectRefundTx, treenode.FieldRawRefundTxid, treenode.FieldDirectRefundTxid:
			values[i] = new([]byte)
		case treenode.FieldVerifyingPubkey, treenode.FieldOwnerIdentityPubkey, treenode.FieldOwnerSigningPubkey:
			values[i] = new(keys.Public)
		case treenode.FieldValue, treenode.FieldVout, treenode.FieldNodeConfirmationHeight, treenode.FieldRefundConfirmationHeight:
			values[i] = new(sql.NullInt64)
		case treenode.FieldStatus:
			values[i] = new(sql.NullString)
		case treenode.FieldCreateTime, treenode.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case treenode.FieldID:
			values[i] = new(uuid.UUID)
		case treenode.ForeignKeys[0]: // tree_node_tree
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case treenode.ForeignKeys[1]: // tree_node_parent
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case treenode.ForeignKeys[2]: // tree_node_signing_keyshare
			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 TreeNode fields.
func (tn *TreeNode) 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 treenode.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				tn.ID = *value
			}
		case treenode.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 {
				tn.CreateTime = value.Time
			}
		case treenode.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 {
				tn.UpdateTime = value.Time
			}
		case treenode.FieldValue:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field value", values[i])
			} else if value.Valid {
				tn.Value = uint64(value.Int64)
			}
		case treenode.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				tn.Status = schematype.TreeNodeStatus(value.String)
			}
		case treenode.FieldVerifyingPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field verifying_pubkey", values[i])
			} else if value != nil {
				tn.VerifyingPubkey = *value
			}
		case treenode.FieldOwnerIdentityPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field owner_identity_pubkey", values[i])
			} else if value != nil {
				tn.OwnerIdentityPubkey = *value
			}
		case treenode.FieldOwnerSigningPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field owner_signing_pubkey", values[i])
			} else if value != nil {
				tn.OwnerSigningPubkey = *value
			}
		case treenode.FieldVout:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field vout", values[i])
			} else if value.Valid {
				tn.Vout = int16(value.Int64)
			}
		case treenode.FieldNodeConfirmationHeight:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field node_confirmation_height", values[i])
			} else if value.Valid {
				tn.NodeConfirmationHeight = uint64(value.Int64)
			}
		case treenode.FieldRefundConfirmationHeight:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field refund_confirmation_height", values[i])
			} else if value.Valid {
				tn.RefundConfirmationHeight = uint64(value.Int64)
			}
		case treenode.FieldRawTx:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field raw_tx", values[i])
			} else if value != nil {
				tn.RawTx = *value
			}
		case treenode.FieldDirectTx:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_tx", values[i])
			} else if value != nil {
				tn.DirectTx = *value
			}
		case treenode.FieldDirectFromCpfpRefundTx:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_from_cpfp_refund_tx", values[i])
			} else if value != nil {
				tn.DirectFromCpfpRefundTx = *value
			}
		case treenode.FieldRawTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field raw_txid", values[i])
			} else if value != nil {
				tn.RawTxid = *value
			}
		case treenode.FieldDirectTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_txid", values[i])
			} else if value != nil {
				tn.DirectTxid = *value
			}
		case treenode.FieldDirectFromCpfpRefundTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_from_cpfp_refund_txid", values[i])
			} else if value != nil {
				tn.DirectFromCpfpRefundTxid = *value
			}
		case treenode.FieldRawRefundTx:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field raw_refund_tx", values[i])
			} else if value != nil {
				tn.RawRefundTx = *value
			}
		case treenode.FieldDirectRefundTx:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_refund_tx", values[i])
			} else if value != nil {
				tn.DirectRefundTx = *value
			}
		case treenode.FieldRawRefundTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field raw_refund_txid", values[i])
			} else if value != nil {
				tn.RawRefundTxid = *value
			}
		case treenode.FieldDirectRefundTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field direct_refund_txid", values[i])
			} else if value != nil {
				tn.DirectRefundTxid = *value
			}
		case treenode.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field tree_node_tree", values[i])
			} else if value.Valid {
				tn.tree_node_tree = new(uuid.UUID)
				*tn.tree_node_tree = *value.S.(*uuid.UUID)
			}
		case treenode.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field tree_node_parent", values[i])
			} else if value.Valid {
				tn.tree_node_parent = new(uuid.UUID)
				*tn.tree_node_parent = *value.S.(*uuid.UUID)
			}
		case treenode.ForeignKeys[2]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field tree_node_signing_keyshare", values[i])
			} else if value.Valid {
				tn.tree_node_signing_keyshare = new(uuid.UUID)
				*tn.tree_node_signing_keyshare = *value.S.(*uuid.UUID)
			}
		default:
			tn.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTree queries the "tree" edge of the TreeNode entity.
func (tn *TreeNode) QueryTree() *TreeQuery {
	return NewTreeNodeClient(tn.config).QueryTree(tn)
}

// QueryParent queries the "parent" edge of the TreeNode entity.
func (tn *TreeNode) QueryParent() *TreeNodeQuery {
	return NewTreeNodeClient(tn.config).QueryParent(tn)
}

// QuerySigningKeyshare queries the "signing_keyshare" edge of the TreeNode entity.
func (tn *TreeNode) QuerySigningKeyshare() *SigningKeyshareQuery {
	return NewTreeNodeClient(tn.config).QuerySigningKeyshare(tn)
}

// QueryChildren queries the "children" edge of the TreeNode entity.
func (tn *TreeNode) QueryChildren() *TreeNodeQuery {
	return NewTreeNodeClient(tn.config).QueryChildren(tn)
}

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

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

// String implements the fmt.Stringer.
func (tn *TreeNode) String() string {
	var builder strings.Builder
	builder.WriteString("TreeNode(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tn.ID))
	builder.WriteString("create_time=")
	builder.WriteString(tn.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(tn.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("value=")
	builder.WriteString(fmt.Sprintf("%v", tn.Value))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", tn.Status))
	builder.WriteString(", ")
	builder.WriteString("verifying_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", tn.VerifyingPubkey))
	builder.WriteString(", ")
	builder.WriteString("owner_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", tn.OwnerIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("owner_signing_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", tn.OwnerSigningPubkey))
	builder.WriteString(", ")
	builder.WriteString("vout=")
	builder.WriteString(fmt.Sprintf("%v", tn.Vout))
	builder.WriteString(", ")
	builder.WriteString("node_confirmation_height=")
	builder.WriteString(fmt.Sprintf("%v", tn.NodeConfirmationHeight))
	builder.WriteString(", ")
	builder.WriteString("refund_confirmation_height=")
	builder.WriteString(fmt.Sprintf("%v", tn.RefundConfirmationHeight))
	builder.WriteString(", ")
	builder.WriteString("raw_tx=")
	builder.WriteString(fmt.Sprintf("%v", tn.RawTx))
	builder.WriteString(", ")
	builder.WriteString("direct_tx=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectTx))
	builder.WriteString(", ")
	builder.WriteString("direct_from_cpfp_refund_tx=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectFromCpfpRefundTx))
	builder.WriteString(", ")
	builder.WriteString("raw_txid=")
	builder.WriteString(fmt.Sprintf("%v", tn.RawTxid))
	builder.WriteString(", ")
	builder.WriteString("direct_txid=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectTxid))
	builder.WriteString(", ")
	builder.WriteString("direct_from_cpfp_refund_txid=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectFromCpfpRefundTxid))
	builder.WriteString(", ")
	builder.WriteString("raw_refund_tx=")
	builder.WriteString(fmt.Sprintf("%v", tn.RawRefundTx))
	builder.WriteString(", ")
	builder.WriteString("direct_refund_tx=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectRefundTx))
	builder.WriteString(", ")
	builder.WriteString("raw_refund_txid=")
	builder.WriteString(fmt.Sprintf("%v", tn.RawRefundTxid))
	builder.WriteString(", ")
	builder.WriteString("direct_refund_txid=")
	builder.WriteString(fmt.Sprintf("%v", tn.DirectRefundTxid))
	builder.WriteByte(')')
	return builder.String()
}

// TreeNodes is a parsable slice of TreeNode.
type TreeNodes []*TreeNode
