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

// Utxo is the model entity for the Utxo schema.
type Utxo 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"`
	// BlockHeight holds the value of the "block_height" field.
	BlockHeight int64 `json:"block_height,omitempty"`
	// Txid holds the value of the "txid" field.
	Txid []byte `json:"txid,omitempty"`
	// Vout holds the value of the "vout" field.
	Vout uint32 `json:"vout,omitempty"`
	// Amount holds the value of the "amount" field.
	Amount uint64 `json:"amount,omitempty"`
	// Network holds the value of the "network" field.
	Network schematype.Network `json:"network,omitempty"`
	// PkScript holds the value of the "pk_script" field.
	PkScript []byte `json:"pk_script,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UtxoQuery when eager-loading is set.
	Edges                UtxoEdges `json:"edges"`
	deposit_address_utxo *uuid.UUID
	selectValues         sql.SelectValues
}

// UtxoEdges holds the relations/edges for other nodes in the graph.
type UtxoEdges struct {
	// 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 [1]bool
}

// 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 UtxoEdges) DepositAddressOrErr() (*DepositAddress, error) {
	if e.DepositAddress != nil {
		return e.DepositAddress, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: depositaddress.Label}
	}
	return nil, &NotLoadedError{edge: "deposit_address"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Utxo) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case utxo.FieldTxid, utxo.FieldPkScript:
			values[i] = new([]byte)
		case utxo.FieldBlockHeight, utxo.FieldVout, utxo.FieldAmount:
			values[i] = new(sql.NullInt64)
		case utxo.FieldNetwork:
			values[i] = new(sql.NullString)
		case utxo.FieldCreateTime, utxo.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case utxo.FieldID:
			values[i] = new(uuid.UUID)
		case utxo.ForeignKeys[0]: // deposit_address_utxo
			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 Utxo fields.
func (u *Utxo) 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 utxo.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				u.ID = *value
			}
		case utxo.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 {
				u.CreateTime = value.Time
			}
		case utxo.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 {
				u.UpdateTime = value.Time
			}
		case utxo.FieldBlockHeight:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field block_height", values[i])
			} else if value.Valid {
				u.BlockHeight = value.Int64
			}
		case utxo.FieldTxid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field txid", values[i])
			} else if value != nil {
				u.Txid = *value
			}
		case utxo.FieldVout:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field vout", values[i])
			} else if value.Valid {
				u.Vout = uint32(value.Int64)
			}
		case utxo.FieldAmount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field amount", values[i])
			} else if value.Valid {
				u.Amount = uint64(value.Int64)
			}
		case utxo.FieldNetwork:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field network", values[i])
			} else if value.Valid {
				u.Network = schematype.Network(value.String)
			}
		case utxo.FieldPkScript:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field pk_script", values[i])
			} else if value != nil {
				u.PkScript = *value
			}
		case utxo.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field deposit_address_utxo", values[i])
			} else if value.Valid {
				u.deposit_address_utxo = new(uuid.UUID)
				*u.deposit_address_utxo = *value.S.(*uuid.UUID)
			}
		default:
			u.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryDepositAddress queries the "deposit_address" edge of the Utxo entity.
func (u *Utxo) QueryDepositAddress() *DepositAddressQuery {
	return NewUtxoClient(u.config).QueryDepositAddress(u)
}

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

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

// String implements the fmt.Stringer.
func (u *Utxo) String() string {
	var builder strings.Builder
	builder.WriteString("Utxo(")
	builder.WriteString(fmt.Sprintf("id=%v, ", u.ID))
	builder.WriteString("create_time=")
	builder.WriteString(u.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(u.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("block_height=")
	builder.WriteString(fmt.Sprintf("%v", u.BlockHeight))
	builder.WriteString(", ")
	builder.WriteString("txid=")
	builder.WriteString(fmt.Sprintf("%v", u.Txid))
	builder.WriteString(", ")
	builder.WriteString("vout=")
	builder.WriteString(fmt.Sprintf("%v", u.Vout))
	builder.WriteString(", ")
	builder.WriteString("amount=")
	builder.WriteString(fmt.Sprintf("%v", u.Amount))
	builder.WriteString(", ")
	builder.WriteString("network=")
	builder.WriteString(fmt.Sprintf("%v", u.Network))
	builder.WriteString(", ")
	builder.WriteString("pk_script=")
	builder.WriteString(fmt.Sprintf("%v", u.PkScript))
	builder.WriteByte(')')
	return builder.String()
}

// Utxos is a parsable slice of Utxo.
type Utxos []*Utxo
