// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/emailchallenge"
	"PasswordManager/ent/totpcredential"
	"PasswordManager/ent/user"
	"PasswordManager/ent/webauthnchallenge"
	"fmt"
	"strings"
	"time"

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

// Challenge is the model entity for the Challenge schema.
type Challenge struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Expiry holds the value of the "expiry" field.
	Expiry time.Time `json:"expiry,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ChallengeQuery when eager-loading is set.
	Edges           ChallengeEdges `json:"edges"`
	user_challenges *uuid.UUID
}

// ChallengeEdges holds the relations/edges for other nodes in the graph.
type ChallengeEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// EmailChallenge holds the value of the emailChallenge edge.
	EmailChallenge *EmailChallenge `json:"emailChallenge,omitempty"`
	// WebauthnChallenge holds the value of the webauthnChallenge edge.
	WebauthnChallenge *WebAuthnChallenge `json:"webauthnChallenge,omitempty"`
	// TotpCredential holds the value of the totpCredential edge.
	TotpCredential *TotpCredential `json:"totpCredential,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]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 ChallengeEdges) 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"}
}

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

// WebauthnChallengeOrErr returns the WebauthnChallenge value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ChallengeEdges) WebauthnChallengeOrErr() (*WebAuthnChallenge, error) {
	if e.loadedTypes[2] {
		if e.WebauthnChallenge == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: webauthnchallenge.Label}
		}
		return e.WebauthnChallenge, nil
	}
	return nil, &NotLoadedError{edge: "webauthnChallenge"}
}

// 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 ChallengeEdges) TotpCredentialOrErr() (*TotpCredential, error) {
	if e.loadedTypes[3] {
		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"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Challenge) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case challenge.FieldExpiry:
			values[i] = new(sql.NullTime)
		case challenge.FieldID:
			values[i] = new(uuid.UUID)
		case challenge.ForeignKeys[0]: // user_challenges
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			return nil, fmt.Errorf("unexpected column %q for type Challenge", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Challenge fields.
func (c *Challenge) 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 challenge.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				c.ID = *value
			}
		case challenge.FieldExpiry:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expiry", values[i])
			} else if value.Valid {
				c.Expiry = value.Time
			}
		case challenge.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_challenges", values[i])
			} else if value.Valid {
				c.user_challenges = new(uuid.UUID)
				*c.user_challenges = *value.S.(*uuid.UUID)
			}
		}
	}
	return nil
}

// QueryUser queries the "user" edge of the Challenge entity.
func (c *Challenge) QueryUser() *UserQuery {
	return NewChallengeClient(c.config).QueryUser(c)
}

// QueryEmailChallenge queries the "emailChallenge" edge of the Challenge entity.
func (c *Challenge) QueryEmailChallenge() *EmailChallengeQuery {
	return NewChallengeClient(c.config).QueryEmailChallenge(c)
}

// QueryWebauthnChallenge queries the "webauthnChallenge" edge of the Challenge entity.
func (c *Challenge) QueryWebauthnChallenge() *WebAuthnChallengeQuery {
	return NewChallengeClient(c.config).QueryWebauthnChallenge(c)
}

// QueryTotpCredential queries the "totpCredential" edge of the Challenge entity.
func (c *Challenge) QueryTotpCredential() *TotpCredentialQuery {
	return NewChallengeClient(c.config).QueryTotpCredential(c)
}

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

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

// String implements the fmt.Stringer.
func (c *Challenge) String() string {
	var builder strings.Builder
	builder.WriteString("Challenge(")
	builder.WriteString(fmt.Sprintf("id=%v, ", c.ID))
	builder.WriteString("expiry=")
	builder.WriteString(c.Expiry.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Challenges is a parsable slice of Challenge.
type Challenges []*Challenge
