// 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/tokenoutput"
	"github.com/lightsparkdev/spark/so/ent/tokenpartialrevocationsecretshare"
)

// Holds the revealed revocation secret shares for a token output from the peer operators. DO NOT WRITE an operator's own secret share in this table. This already exists in the TokenOutput table.
type TokenPartialRevocationSecretShare 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"`
	// OperatorIdentityPublicKey holds the value of the "operator_identity_public_key" field.
	OperatorIdentityPublicKey keys.Public `json:"operator_identity_public_key,omitempty"`
	// SecretShare holds the value of the "secret_share" field.
	SecretShare keys.Private `json:"secret_share,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TokenPartialRevocationSecretShareQuery when eager-loading is set.
	Edges                                               TokenPartialRevocationSecretShareEdges `json:"edges"`
	token_output_token_partial_revocation_secret_shares *uuid.UUID
	selectValues                                        sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*TokenPartialRevocationSecretShare) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case tokenpartialrevocationsecretshare.FieldSecretShare:
			values[i] = new(keys.Private)
		case tokenpartialrevocationsecretshare.FieldOperatorIdentityPublicKey:
			values[i] = new(keys.Public)
		case tokenpartialrevocationsecretshare.FieldCreateTime, tokenpartialrevocationsecretshare.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case tokenpartialrevocationsecretshare.FieldID:
			values[i] = new(uuid.UUID)
		case tokenpartialrevocationsecretshare.ForeignKeys[0]: // token_output_token_partial_revocation_secret_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 TokenPartialRevocationSecretShare fields.
func (tprss *TokenPartialRevocationSecretShare) 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 tokenpartialrevocationsecretshare.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				tprss.ID = *value
			}
		case tokenpartialrevocationsecretshare.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 {
				tprss.CreateTime = value.Time
			}
		case tokenpartialrevocationsecretshare.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 {
				tprss.UpdateTime = value.Time
			}
		case tokenpartialrevocationsecretshare.FieldOperatorIdentityPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field operator_identity_public_key", values[i])
			} else if value != nil {
				tprss.OperatorIdentityPublicKey = *value
			}
		case tokenpartialrevocationsecretshare.FieldSecretShare:
			if value, ok := values[i].(*keys.Private); !ok {
				return fmt.Errorf("unexpected type %T for field secret_share", values[i])
			} else if value != nil {
				tprss.SecretShare = *value
			}
		case tokenpartialrevocationsecretshare.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field token_output_token_partial_revocation_secret_shares", values[i])
			} else if value.Valid {
				tprss.token_output_token_partial_revocation_secret_shares = new(uuid.UUID)
				*tprss.token_output_token_partial_revocation_secret_shares = *value.S.(*uuid.UUID)
			}
		default:
			tprss.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTokenOutput queries the "token_output" edge of the TokenPartialRevocationSecretShare entity.
func (tprss *TokenPartialRevocationSecretShare) QueryTokenOutput() *TokenOutputQuery {
	return NewTokenPartialRevocationSecretShareClient(tprss.config).QueryTokenOutput(tprss)
}

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

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

// String implements the fmt.Stringer.
func (tprss *TokenPartialRevocationSecretShare) String() string {
	var builder strings.Builder
	builder.WriteString("TokenPartialRevocationSecretShare(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tprss.ID))
	builder.WriteString("create_time=")
	builder.WriteString(tprss.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(tprss.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("operator_identity_public_key=")
	builder.WriteString(fmt.Sprintf("%v", tprss.OperatorIdentityPublicKey))
	builder.WriteString(", ")
	builder.WriteString("secret_share=")
	builder.WriteString(fmt.Sprintf("%v", tprss.SecretShare))
	builder.WriteByte(')')
	return builder.String()
}

// TokenPartialRevocationSecretShares is a parsable slice of TokenPartialRevocationSecretShare.
type TokenPartialRevocationSecretShares []*TokenPartialRevocationSecretShare
