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

// Tree is the model entity for the Tree schema.
type Tree 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"`
	// OwnerIdentityPubkey holds the value of the "owner_identity_pubkey" field.
	OwnerIdentityPubkey keys.Public `json:"owner_identity_pubkey,omitempty"`
	// Status holds the value of the "status" field.
	Status schematype.TreeStatus `json:"status,omitempty"`
	// Network holds the value of the "network" field.
	Network schematype.Network `json:"network,omitempty"`
	// BaseTxid holds the value of the "base_txid" field.
	BaseTxid []byte `json:"base_txid,omitempty"`
	// Vout holds the value of the "vout" field.
	Vout int16 `json:"vout,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TreeQuery when eager-loading is set.
	Edges                TreeEdges `json:"edges"`
	deposit_address_tree *uuid.UUID
	tree_root            *uuid.UUID
	selectValues         sql.SelectValues
}

// TreeEdges holds the relations/edges for other nodes in the graph.
type TreeEdges struct {
	// Root holds the value of the root edge.
	Root *TreeNode `json:"root,omitempty"`
	// Nodes holds the value of the nodes edge.
	Nodes []*TreeNode `json:"nodes,omitempty"`
	// DepositAddress holds the value of the deposit_address edge.
	DepositAddress *DepositAddress `json:"deposit_address,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

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

// NodesOrErr returns the Nodes value or an error if the edge
// was not loaded in eager-loading.
func (e TreeEdges) NodesOrErr() ([]*TreeNode, error) {
	if e.loadedTypes[1] {
		return e.Nodes, nil
	}
	return nil, &NotLoadedError{edge: "nodes"}
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*Tree) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case tree.FieldBaseTxid:
			values[i] = new([]byte)
		case tree.FieldOwnerIdentityPubkey:
			values[i] = new(keys.Public)
		case tree.FieldVout:
			values[i] = new(sql.NullInt64)
		case tree.FieldStatus, tree.FieldNetwork:
			values[i] = new(sql.NullString)
		case tree.FieldCreateTime, tree.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case tree.FieldID:
			values[i] = new(uuid.UUID)
		case tree.ForeignKeys[0]: // deposit_address_tree
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case tree.ForeignKeys[1]: // tree_root
			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 Tree fields.
func (t *Tree) 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 tree.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				t.ID = *value
			}
		case tree.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 {
				t.CreateTime = value.Time
			}
		case tree.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 {
				t.UpdateTime = value.Time
			}
		case tree.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 {
				t.OwnerIdentityPubkey = *value
			}
		case tree.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				t.Status = schematype.TreeStatus(value.String)
			}
		case tree.FieldNetwork:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field network", values[i])
			} else if value.Valid {
				t.Network = schematype.Network(value.String)
			}
		case tree.FieldBaseTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field base_txid", values[i])
			} else if value != nil {
				t.BaseTxid = *value
			}
		case tree.FieldVout:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field vout", values[i])
			} else if value.Valid {
				t.Vout = int16(value.Int64)
			}
		case tree.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field deposit_address_tree", values[i])
			} else if value.Valid {
				t.deposit_address_tree = new(uuid.UUID)
				*t.deposit_address_tree = *value.S.(*uuid.UUID)
			}
		case tree.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field tree_root", values[i])
			} else if value.Valid {
				t.tree_root = new(uuid.UUID)
				*t.tree_root = *value.S.(*uuid.UUID)
			}
		default:
			t.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryRoot queries the "root" edge of the Tree entity.
func (t *Tree) QueryRoot() *TreeNodeQuery {
	return NewTreeClient(t.config).QueryRoot(t)
}

// QueryNodes queries the "nodes" edge of the Tree entity.
func (t *Tree) QueryNodes() *TreeNodeQuery {
	return NewTreeClient(t.config).QueryNodes(t)
}

// QueryDepositAddress queries the "deposit_address" edge of the Tree entity.
func (t *Tree) QueryDepositAddress() *DepositAddressQuery {
	return NewTreeClient(t.config).QueryDepositAddress(t)
}

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

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

// String implements the fmt.Stringer.
func (t *Tree) String() string {
	var builder strings.Builder
	builder.WriteString("Tree(")
	builder.WriteString(fmt.Sprintf("id=%v, ", t.ID))
	builder.WriteString("create_time=")
	builder.WriteString(t.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(t.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("owner_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", t.OwnerIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", t.Status))
	builder.WriteString(", ")
	builder.WriteString("network=")
	builder.WriteString(fmt.Sprintf("%v", t.Network))
	builder.WriteString(", ")
	builder.WriteString("base_txid=")
	builder.WriteString(fmt.Sprintf("%v", t.BaseTxid))
	builder.WriteString(", ")
	builder.WriteString("vout=")
	builder.WriteString(fmt.Sprintf("%v", t.Vout))
	builder.WriteByte(')')
	return builder.String()
}

// Trees is a parsable slice of Tree.
type Trees []*Tree
