// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/totpcredential"
	"PasswordManager/ent/user"
	"fmt"
	"strings"

	"entgo.io/ent/dialect/sql"
	"github.com/google/uuid"
)

// TotpCredential is the model entity for the TotpCredential schema.
type TotpCredential struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Secret holds the value of the "secret" field.
	Secret string `json:"secret,omitempty"`
	// Validated holds the value of the "validated" field.
	Validated bool `json:"validated,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TotpCredentialQuery when eager-loading is set.
	Edges                     TotpCredentialEdges `json:"edges"`
	challenge_totp_credential *uuid.UUID
	user_totp_credential      *uuid.UUID
}

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

// UserOrErr returns the User value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TotpCredentialEdges) UserOrErr() (*User, error) {
	if e.loadedTypes[0] {
		if e.User == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: user.Label}
		}
		return e.User, nil
	}
	return nil, &NotLoadedError{edge: "user"}
}

// ChallengeOrErr returns the Challenge value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TotpCredentialEdges) ChallengeOrErr() (*Challenge, error) {
	if e.loadedTypes[1] {
		if e.Challenge == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: challenge.Label}
		}
		return e.Challenge, nil
	}
	return nil, &NotLoadedError{edge: "challenge"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TotpCredential) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case totpcredential.FieldValidated:
			values[i] = new(sql.NullBool)
		case totpcredential.FieldSecret:
			values[i] = new(sql.NullString)
		case totpcredential.FieldID:
			values[i] = new(uuid.UUID)
		case totpcredential.ForeignKeys[0]: // challenge_totp_credential
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case totpcredential.ForeignKeys[1]: // user_totp_credential
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			return nil, fmt.Errorf("unexpected column %q for type TotpCredential", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the TotpCredential fields.
func (tc *TotpCredential) 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 totpcredential.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				tc.ID = *value
			}
		case totpcredential.FieldSecret:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field secret", values[i])
			} else if value.Valid {
				tc.Secret = value.String
			}
		case totpcredential.FieldValidated:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field validated", values[i])
			} else if value.Valid {
				tc.Validated = value.Bool
			}
		case totpcredential.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field challenge_totp_credential", values[i])
			} else if value.Valid {
				tc.challenge_totp_credential = new(uuid.UUID)
				*tc.challenge_totp_credential = *value.S.(*uuid.UUID)
			}
		case totpcredential.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_totp_credential", values[i])
			} else if value.Valid {
				tc.user_totp_credential = new(uuid.UUID)
				*tc.user_totp_credential = *value.S.(*uuid.UUID)
			}
		}
	}
	return nil
}

// QueryUser queries the "user" edge of the TotpCredential entity.
func (tc *TotpCredential) QueryUser() *UserQuery {
	return NewTotpCredentialClient(tc.config).QueryUser(tc)
}

// QueryChallenge queries the "challenge" edge of the TotpCredential entity.
func (tc *TotpCredential) QueryChallenge() *ChallengeQuery {
	return NewTotpCredentialClient(tc.config).QueryChallenge(tc)
}

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

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

// String implements the fmt.Stringer.
func (tc *TotpCredential) String() string {
	var builder strings.Builder
	builder.WriteString("TotpCredential(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tc.ID))
	builder.WriteString("secret=")
	builder.WriteString(tc.Secret)
	builder.WriteString(", ")
	builder.WriteString("validated=")
	builder.WriteString(fmt.Sprintf("%v", tc.Validated))
	builder.WriteByte(')')
	return builder.String()
}

// TotpCredentials is a parsable slice of TotpCredential.
type TotpCredentials []*TotpCredential
