// 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/transfer"
	"github.com/lightsparkdev/spark/so/ent/utxo"
	"github.com/lightsparkdev/spark/so/ent/utxoswap"
)

// UtxoSwap is the model entity for the UtxoSwap schema.
type UtxoSwap 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"`
	// Status holds the value of the "status" field.
	Status schematype.UtxoSwapStatus `json:"status,omitempty"`
	// RequestType holds the value of the "request_type" field.
	RequestType schematype.UtxoSwapRequestType `json:"request_type,omitempty"`
	// CreditAmountSats holds the value of the "credit_amount_sats" field.
	CreditAmountSats uint64 `json:"credit_amount_sats,omitempty"`
	// MaxFeeSats holds the value of the "max_fee_sats" field.
	MaxFeeSats uint64 `json:"max_fee_sats,omitempty"`
	// SspSignature holds the value of the "ssp_signature" field.
	SspSignature []byte `json:"ssp_signature,omitempty"`
	// SspIdentityPublicKey holds the value of the "ssp_identity_public_key" field.
	SspIdentityPublicKey keys.Public `json:"ssp_identity_public_key,omitempty"`
	// UserSignature holds the value of the "user_signature" field.
	UserSignature []byte `json:"user_signature,omitempty"`
	// UserIdentityPublicKey holds the value of the "user_identity_public_key" field.
	UserIdentityPublicKey keys.Public `json:"user_identity_public_key,omitempty"`
	// CoordinatorIdentityPublicKey holds the value of the "coordinator_identity_public_key" field.
	CoordinatorIdentityPublicKey keys.Public `json:"coordinator_identity_public_key,omitempty"`
	// RequestedTransferID holds the value of the "requested_transfer_id" field.
	RequestedTransferID uuid.UUID `json:"requested_transfer_id,omitempty"`
	// SpendTxSigningResult holds the value of the "spend_tx_signing_result" field.
	SpendTxSigningResult []byte `json:"spend_tx_signing_result,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UtxoSwapQuery when eager-loading is set.
	Edges                     UtxoSwapEdges `json:"edges"`
	deposit_address_utxoswaps *uuid.UUID
	utxo_swap_utxo            *uuid.UUID
	utxo_swap_transfer        *uuid.UUID
	selectValues              sql.SelectValues
}

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

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*UtxoSwap) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case utxoswap.FieldSspSignature, utxoswap.FieldUserSignature, utxoswap.FieldSpendTxSigningResult:
			values[i] = new([]byte)
		case utxoswap.FieldSspIdentityPublicKey, utxoswap.FieldUserIdentityPublicKey, utxoswap.FieldCoordinatorIdentityPublicKey:
			values[i] = new(keys.Public)
		case utxoswap.FieldCreditAmountSats, utxoswap.FieldMaxFeeSats:
			values[i] = new(sql.NullInt64)
		case utxoswap.FieldStatus, utxoswap.FieldRequestType:
			values[i] = new(sql.NullString)
		case utxoswap.FieldCreateTime, utxoswap.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case utxoswap.FieldID, utxoswap.FieldRequestedTransferID:
			values[i] = new(uuid.UUID)
		case utxoswap.ForeignKeys[0]: // deposit_address_utxoswaps
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case utxoswap.ForeignKeys[1]: // utxo_swap_utxo
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case utxoswap.ForeignKeys[2]: // utxo_swap_transfer
			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 UtxoSwap fields.
func (us *UtxoSwap) 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 utxoswap.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				us.ID = *value
			}
		case utxoswap.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 {
				us.CreateTime = value.Time
			}
		case utxoswap.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 {
				us.UpdateTime = value.Time
			}
		case utxoswap.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				us.Status = schematype.UtxoSwapStatus(value.String)
			}
		case utxoswap.FieldRequestType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field request_type", values[i])
			} else if value.Valid {
				us.RequestType = schematype.UtxoSwapRequestType(value.String)
			}
		case utxoswap.FieldCreditAmountSats:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field credit_amount_sats", values[i])
			} else if value.Valid {
				us.CreditAmountSats = uint64(value.Int64)
			}
		case utxoswap.FieldMaxFeeSats:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field max_fee_sats", values[i])
			} else if value.Valid {
				us.MaxFeeSats = uint64(value.Int64)
			}
		case utxoswap.FieldSspSignature:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field ssp_signature", values[i])
			} else if value != nil {
				us.SspSignature = *value
			}
		case utxoswap.FieldSspIdentityPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field ssp_identity_public_key", values[i])
			} else if value != nil {
				us.SspIdentityPublicKey = *value
			}
		case utxoswap.FieldUserSignature:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field user_signature", values[i])
			} else if value != nil {
				us.UserSignature = *value
			}
		case utxoswap.FieldUserIdentityPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field user_identity_public_key", values[i])
			} else if value != nil {
				us.UserIdentityPublicKey = *value
			}
		case utxoswap.FieldCoordinatorIdentityPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field coordinator_identity_public_key", values[i])
			} else if value != nil {
				us.CoordinatorIdentityPublicKey = *value
			}
		case utxoswap.FieldRequestedTransferID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field requested_transfer_id", values[i])
			} else if value != nil {
				us.RequestedTransferID = *value
			}
		case utxoswap.FieldSpendTxSigningResult:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field spend_tx_signing_result", values[i])
			} else if value != nil {
				us.SpendTxSigningResult = *value
			}
		case utxoswap.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field deposit_address_utxoswaps", values[i])
			} else if value.Valid {
				us.deposit_address_utxoswaps = new(uuid.UUID)
				*us.deposit_address_utxoswaps = *value.S.(*uuid.UUID)
			}
		case utxoswap.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field utxo_swap_utxo", values[i])
			} else if value.Valid {
				us.utxo_swap_utxo = new(uuid.UUID)
				*us.utxo_swap_utxo = *value.S.(*uuid.UUID)
			}
		case utxoswap.ForeignKeys[2]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field utxo_swap_transfer", values[i])
			} else if value.Valid {
				us.utxo_swap_transfer = new(uuid.UUID)
				*us.utxo_swap_transfer = *value.S.(*uuid.UUID)
			}
		default:
			us.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUtxo queries the "utxo" edge of the UtxoSwap entity.
func (us *UtxoSwap) QueryUtxo() *UtxoQuery {
	return NewUtxoSwapClient(us.config).QueryUtxo(us)
}

// QueryTransfer queries the "transfer" edge of the UtxoSwap entity.
func (us *UtxoSwap) QueryTransfer() *TransferQuery {
	return NewUtxoSwapClient(us.config).QueryTransfer(us)
}

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

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

// String implements the fmt.Stringer.
func (us *UtxoSwap) String() string {
	var builder strings.Builder
	builder.WriteString("UtxoSwap(")
	builder.WriteString(fmt.Sprintf("id=%v, ", us.ID))
	builder.WriteString("create_time=")
	builder.WriteString(us.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(us.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", us.Status))
	builder.WriteString(", ")
	builder.WriteString("request_type=")
	builder.WriteString(fmt.Sprintf("%v", us.RequestType))
	builder.WriteString(", ")
	builder.WriteString("credit_amount_sats=")
	builder.WriteString(fmt.Sprintf("%v", us.CreditAmountSats))
	builder.WriteString(", ")
	builder.WriteString("max_fee_sats=")
	builder.WriteString(fmt.Sprintf("%v", us.MaxFeeSats))
	builder.WriteString(", ")
	builder.WriteString("ssp_signature=")
	builder.WriteString(fmt.Sprintf("%v", us.SspSignature))
	builder.WriteString(", ")
	builder.WriteString("ssp_identity_public_key=")
	builder.WriteString(fmt.Sprintf("%v", us.SspIdentityPublicKey))
	builder.WriteString(", ")
	builder.WriteString("user_signature=")
	builder.WriteString(fmt.Sprintf("%v", us.UserSignature))
	builder.WriteString(", ")
	builder.WriteString("user_identity_public_key=")
	builder.WriteString(fmt.Sprintf("%v", us.UserIdentityPublicKey))
	builder.WriteString(", ")
	builder.WriteString("coordinator_identity_public_key=")
	builder.WriteString(fmt.Sprintf("%v", us.CoordinatorIdentityPublicKey))
	builder.WriteString(", ")
	builder.WriteString("requested_transfer_id=")
	builder.WriteString(fmt.Sprintf("%v", us.RequestedTransferID))
	builder.WriteString(", ")
	builder.WriteString("spend_tx_signing_result=")
	builder.WriteString(fmt.Sprintf("%v", us.SpendTxSigningResult))
	builder.WriteByte(')')
	return builder.String()
}

// UtxoSwaps is a parsable slice of UtxoSwap.
type UtxoSwaps []*UtxoSwap
