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

// PreimageRequest is the model entity for the PreimageRequest schema.
type PreimageRequest 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"`
	// PaymentHash holds the value of the "payment_hash" field.
	PaymentHash []byte `json:"payment_hash,omitempty"`
	// Status holds the value of the "status" field.
	Status schematype.PreimageRequestStatus `json:"status,omitempty"`
	// ReceiverIdentityPubkey holds the value of the "receiver_identity_pubkey" field.
	ReceiverIdentityPubkey keys.Public `json:"receiver_identity_pubkey,omitempty"`
	// Preimage holds the value of the "preimage" field.
	Preimage []byte `json:"preimage,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PreimageRequestQuery when eager-loading is set.
	Edges                      PreimageRequestEdges `json:"edges"`
	preimage_request_transfers *uuid.UUID
	selectValues               sql.SelectValues
}

// PreimageRequestEdges holds the relations/edges for other nodes in the graph.
type PreimageRequestEdges struct {
	// Transactions holds the value of the transactions edge.
	Transactions []*UserSignedTransaction `json:"transactions,omitempty"`
	// PreimageShares holds the value of the preimage_shares edge.
	PreimageShares *PreimageShare `json:"preimage_shares,omitempty"`
	// Transfers holds the value of the transfers edge.
	Transfers *Transfer `json:"transfers,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// TransactionsOrErr returns the Transactions value or an error if the edge
// was not loaded in eager-loading.
func (e PreimageRequestEdges) TransactionsOrErr() ([]*UserSignedTransaction, error) {
	if e.loadedTypes[0] {
		return e.Transactions, nil
	}
	return nil, &NotLoadedError{edge: "transactions"}
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*PreimageRequest) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case preimagerequest.FieldPaymentHash, preimagerequest.FieldPreimage:
			values[i] = new([]byte)
		case preimagerequest.FieldReceiverIdentityPubkey:
			values[i] = new(keys.Public)
		case preimagerequest.FieldStatus:
			values[i] = new(sql.NullString)
		case preimagerequest.FieldCreateTime, preimagerequest.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case preimagerequest.FieldID:
			values[i] = new(uuid.UUID)
		case preimagerequest.ForeignKeys[0]: // preimage_request_transfers
			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 PreimageRequest fields.
func (pr *PreimageRequest) 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 preimagerequest.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				pr.ID = *value
			}
		case preimagerequest.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 {
				pr.CreateTime = value.Time
			}
		case preimagerequest.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 {
				pr.UpdateTime = value.Time
			}
		case preimagerequest.FieldPaymentHash:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field payment_hash", values[i])
			} else if value != nil {
				pr.PaymentHash = *value
			}
		case preimagerequest.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				pr.Status = schematype.PreimageRequestStatus(value.String)
			}
		case preimagerequest.FieldReceiverIdentityPubkey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field receiver_identity_pubkey", values[i])
			} else if value != nil {
				pr.ReceiverIdentityPubkey = *value
			}
		case preimagerequest.FieldPreimage:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field preimage", values[i])
			} else if value != nil {
				pr.Preimage = *value
			}
		case preimagerequest.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field preimage_request_transfers", values[i])
			} else if value.Valid {
				pr.preimage_request_transfers = new(uuid.UUID)
				*pr.preimage_request_transfers = *value.S.(*uuid.UUID)
			}
		default:
			pr.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTransactions queries the "transactions" edge of the PreimageRequest entity.
func (pr *PreimageRequest) QueryTransactions() *UserSignedTransactionQuery {
	return NewPreimageRequestClient(pr.config).QueryTransactions(pr)
}

// QueryPreimageShares queries the "preimage_shares" edge of the PreimageRequest entity.
func (pr *PreimageRequest) QueryPreimageShares() *PreimageShareQuery {
	return NewPreimageRequestClient(pr.config).QueryPreimageShares(pr)
}

// QueryTransfers queries the "transfers" edge of the PreimageRequest entity.
func (pr *PreimageRequest) QueryTransfers() *TransferQuery {
	return NewPreimageRequestClient(pr.config).QueryTransfers(pr)
}

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

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

// String implements the fmt.Stringer.
func (pr *PreimageRequest) String() string {
	var builder strings.Builder
	builder.WriteString("PreimageRequest(")
	builder.WriteString(fmt.Sprintf("id=%v, ", pr.ID))
	builder.WriteString("create_time=")
	builder.WriteString(pr.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(pr.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("payment_hash=")
	builder.WriteString(fmt.Sprintf("%v", pr.PaymentHash))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", pr.Status))
	builder.WriteString(", ")
	builder.WriteString("receiver_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", pr.ReceiverIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("preimage=")
	builder.WriteString(fmt.Sprintf("%v", pr.Preimage))
	builder.WriteByte(')')
	return builder.String()
}

// PreimageRequests is a parsable slice of PreimageRequest.
type PreimageRequests []*PreimageRequest
