// Code generated by ent, DO NOT EDIT.

package ent

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

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

// User is the model entity for the User schema.
type User struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Email holds the value of the "email" field.
	Email string `json:"email,omitempty"`
	// StrengthenedMasterHash holds the value of the "strengthenedMasterHash" field.
	StrengthenedMasterHash []byte `json:"-"`
	// StrengthenedMasterHashSalt holds the value of the "strengthenedMasterHashSalt" field.
	StrengthenedMasterHashSalt []byte `json:"-"`
	// ProtectedDatabaseKey holds the value of the "protectedDatabaseKey" field.
	ProtectedDatabaseKey []byte `json:"-"`
	// ProtectedDatabaseKeyIv holds the value of the "protectedDatabaseKeyIv" field.
	ProtectedDatabaseKeyIv []byte `json:"-"`
	// WebauthnEnabled holds the value of the "webauthnEnabled" field.
	WebauthnEnabled bool `json:"webauthnEnabled,omitempty"`
	// TotpEnabled holds the value of the "totpEnabled" field.
	TotpEnabled bool `json:"totpEnabled,omitempty"`
	// Verified holds the value of the "verified" field.
	Verified bool `json:"verified,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserQuery when eager-loading is set.
	Edges UserEdges `json:"edges"`
}

// UserEdges holds the relations/edges for other nodes in the graph.
type UserEdges struct {
	// TotpCredential holds the value of the totpCredential edge.
	TotpCredential *TotpCredential `json:"totpCredential,omitempty"`
	// WebauthnCredentials holds the value of the webauthnCredentials edge.
	WebauthnCredentials []*WebAuthnCredential `json:"webauthnCredentials,omitempty"`
	// WebauthnRegisterChallenges holds the value of the webauthnRegisterChallenges edge.
	WebauthnRegisterChallenges []*WebAuthnRegisterChallenge `json:"webauthnRegisterChallenges,omitempty"`
	// Vaults holds the value of the vaults edge.
	Vaults []*Vault `json:"vaults,omitempty"`
	// Sessions holds the value of the sessions edge.
	Sessions []*Session `json:"sessions,omitempty"`
	// Challenges holds the value of the challenges edge.
	Challenges []*Challenge `json:"challenges,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [6]bool
}

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

// WebauthnCredentialsOrErr returns the WebauthnCredentials value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) WebauthnCredentialsOrErr() ([]*WebAuthnCredential, error) {
	if e.loadedTypes[1] {
		return e.WebauthnCredentials, nil
	}
	return nil, &NotLoadedError{edge: "webauthnCredentials"}
}

// WebauthnRegisterChallengesOrErr returns the WebauthnRegisterChallenges value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) WebauthnRegisterChallengesOrErr() ([]*WebAuthnRegisterChallenge, error) {
	if e.loadedTypes[2] {
		return e.WebauthnRegisterChallenges, nil
	}
	return nil, &NotLoadedError{edge: "webauthnRegisterChallenges"}
}

// VaultsOrErr returns the Vaults value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) VaultsOrErr() ([]*Vault, error) {
	if e.loadedTypes[3] {
		return e.Vaults, nil
	}
	return nil, &NotLoadedError{edge: "vaults"}
}

// SessionsOrErr returns the Sessions value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) SessionsOrErr() ([]*Session, error) {
	if e.loadedTypes[4] {
		return e.Sessions, nil
	}
	return nil, &NotLoadedError{edge: "sessions"}
}

// ChallengesOrErr returns the Challenges value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) ChallengesOrErr() ([]*Challenge, error) {
	if e.loadedTypes[5] {
		return e.Challenges, nil
	}
	return nil, &NotLoadedError{edge: "challenges"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*User) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case user.FieldStrengthenedMasterHash, user.FieldStrengthenedMasterHashSalt, user.FieldProtectedDatabaseKey, user.FieldProtectedDatabaseKeyIv:
			values[i] = new([]byte)
		case user.FieldWebauthnEnabled, user.FieldTotpEnabled, user.FieldVerified:
			values[i] = new(sql.NullBool)
		case user.FieldEmail:
			values[i] = new(sql.NullString)
		case user.FieldID:
			values[i] = new(uuid.UUID)
		default:
			return nil, fmt.Errorf("unexpected column %q for type User", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the User fields.
func (u *User) 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 user.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				u.ID = *value
			}
		case user.FieldEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email", values[i])
			} else if value.Valid {
				u.Email = value.String
			}
		case user.FieldStrengthenedMasterHash:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field strengthenedMasterHash", values[i])
			} else if value != nil {
				u.StrengthenedMasterHash = *value
			}
		case user.FieldStrengthenedMasterHashSalt:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field strengthenedMasterHashSalt", values[i])
			} else if value != nil {
				u.StrengthenedMasterHashSalt = *value
			}
		case user.FieldProtectedDatabaseKey:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field protectedDatabaseKey", values[i])
			} else if value != nil {
				u.ProtectedDatabaseKey = *value
			}
		case user.FieldProtectedDatabaseKeyIv:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field protectedDatabaseKeyIv", values[i])
			} else if value != nil {
				u.ProtectedDatabaseKeyIv = *value
			}
		case user.FieldWebauthnEnabled:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field webauthnEnabled", values[i])
			} else if value.Valid {
				u.WebauthnEnabled = value.Bool
			}
		case user.FieldTotpEnabled:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field totpEnabled", values[i])
			} else if value.Valid {
				u.TotpEnabled = value.Bool
			}
		case user.FieldVerified:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field verified", values[i])
			} else if value.Valid {
				u.Verified = value.Bool
			}
		}
	}
	return nil
}

// QueryTotpCredential queries the "totpCredential" edge of the User entity.
func (u *User) QueryTotpCredential() *TotpCredentialQuery {
	return NewUserClient(u.config).QueryTotpCredential(u)
}

// QueryWebauthnCredentials queries the "webauthnCredentials" edge of the User entity.
func (u *User) QueryWebauthnCredentials() *WebAuthnCredentialQuery {
	return NewUserClient(u.config).QueryWebauthnCredentials(u)
}

// QueryWebauthnRegisterChallenges queries the "webauthnRegisterChallenges" edge of the User entity.
func (u *User) QueryWebauthnRegisterChallenges() *WebAuthnRegisterChallengeQuery {
	return NewUserClient(u.config).QueryWebauthnRegisterChallenges(u)
}

// QueryVaults queries the "vaults" edge of the User entity.
func (u *User) QueryVaults() *VaultQuery {
	return NewUserClient(u.config).QueryVaults(u)
}

// QuerySessions queries the "sessions" edge of the User entity.
func (u *User) QuerySessions() *SessionQuery {
	return NewUserClient(u.config).QuerySessions(u)
}

// QueryChallenges queries the "challenges" edge of the User entity.
func (u *User) QueryChallenges() *ChallengeQuery {
	return NewUserClient(u.config).QueryChallenges(u)
}

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

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

// String implements the fmt.Stringer.
func (u *User) String() string {
	var builder strings.Builder
	builder.WriteString("User(")
	builder.WriteString(fmt.Sprintf("id=%v, ", u.ID))
	builder.WriteString("email=")
	builder.WriteString(u.Email)
	builder.WriteString(", ")
	builder.WriteString("strengthenedMasterHash=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("strengthenedMasterHashSalt=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("protectedDatabaseKey=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("protectedDatabaseKeyIv=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("webauthnEnabled=")
	builder.WriteString(fmt.Sprintf("%v", u.WebauthnEnabled))
	builder.WriteString(", ")
	builder.WriteString("totpEnabled=")
	builder.WriteString(fmt.Sprintf("%v", u.TotpEnabled))
	builder.WriteString(", ")
	builder.WriteString("verified=")
	builder.WriteString(fmt.Sprintf("%v", u.Verified))
	builder.WriteByte(')')
	return builder.String()
}

// Users is a parsable slice of User.
type Users []*User
