// 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"
)

// PreimageShare is the model entity for the PreimageShare schema.
type PreimageShare 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"`
	// PreimageShare holds the value of the "preimage_share" field.
	PreimageShare []byte `json:"preimage_share,omitempty"`
	// Threshold holds the value of the "threshold" field.
	Threshold int32 `json:"threshold,omitempty"`
	// OwnerIdentityPubkey holds the value of the "owner_identity_pubkey" field.
	OwnerIdentityPubkey keys.Public `json:"owner_identity_pubkey,omitempty"`
	// InvoiceString holds the value of the "invoice_string" field.
	InvoiceString string `json:"invoice_string,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PreimageShareQuery when eager-loading is set.
	Edges                            PreimageShareEdges `json:"edges"`
	preimage_request_preimage_shares *uuid.UUID
	selectValues                     sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*PreimageShare) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case preimageshare.FieldPaymentHash, preimageshare.FieldPreimageShare:
			values[i] = new([]byte)
		case preimageshare.FieldOwnerIdentityPubkey:
			values[i] = new(keys.Public)
		case preimageshare.FieldThreshold:
			values[i] = new(sql.NullInt64)
		case preimageshare.FieldInvoiceString:
			values[i] = new(sql.NullString)
		case preimageshare.FieldCreateTime, preimageshare.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case preimageshare.FieldID:
			values[i] = new(uuid.UUID)
		case preimageshare.ForeignKeys[0]: // preimage_request_preimage_shares
			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 PreimageShare fields.
func (ps *PreimageShare) 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 preimageshare.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				ps.ID = *value
			}
		case preimageshare.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 {
				ps.CreateTime = value.Time
			}
		case preimageshare.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 {
				ps.UpdateTime = value.Time
			}
		case preimageshare.FieldPaymentHash:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field payment_hash", values[i])
			} else if value != nil {
				ps.PaymentHash = *value
			}
		case preimageshare.FieldPreimageShare:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field preimage_share", values[i])
			} else if value != nil {
				ps.PreimageShare = *value
			}
		case preimageshare.FieldThreshold:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field threshold", values[i])
			} else if value.Valid {
				ps.Threshold = int32(value.Int64)
			}
		case preimageshare.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 {
				ps.OwnerIdentityPubkey = *value
			}
		case preimageshare.FieldInvoiceString:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field invoice_string", values[i])
			} else if value.Valid {
				ps.InvoiceString = value.String
			}
		case preimageshare.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field preimage_request_preimage_shares", values[i])
			} else if value.Valid {
				ps.preimage_request_preimage_shares = new(uuid.UUID)
				*ps.preimage_request_preimage_shares = *value.S.(*uuid.UUID)
			}
		default:
			ps.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryPreimageRequest queries the "preimage_request" edge of the PreimageShare entity.
func (ps *PreimageShare) QueryPreimageRequest() *PreimageRequestQuery {
	return NewPreimageShareClient(ps.config).QueryPreimageRequest(ps)
}

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

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

// String implements the fmt.Stringer.
func (ps *PreimageShare) String() string {
	var builder strings.Builder
	builder.WriteString("PreimageShare(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ps.ID))
	builder.WriteString("create_time=")
	builder.WriteString(ps.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(ps.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("payment_hash=")
	builder.WriteString(fmt.Sprintf("%v", ps.PaymentHash))
	builder.WriteString(", ")
	builder.WriteString("preimage_share=")
	builder.WriteString(fmt.Sprintf("%v", ps.PreimageShare))
	builder.WriteString(", ")
	builder.WriteString("threshold=")
	builder.WriteString(fmt.Sprintf("%v", ps.Threshold))
	builder.WriteString(", ")
	builder.WriteString("owner_identity_pubkey=")
	builder.WriteString(fmt.Sprintf("%v", ps.OwnerIdentityPubkey))
	builder.WriteString(", ")
	builder.WriteString("invoice_string=")
	builder.WriteString(ps.InvoiceString)
	builder.WriteByte(')')
	return builder.String()
}

// PreimageShares is a parsable slice of PreimageShare.
type PreimageShares []*PreimageShare
