// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/webauthnchallenge"
	"encoding/json"
	"fmt"
	"strings"

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

// WebAuthnChallenge is the model entity for the WebAuthnChallenge schema.
type WebAuthnChallenge struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// SdChallenge holds the value of the "sdChallenge" field.
	SdChallenge string `json:"sdChallenge,omitempty"`
	// UserId holds the value of the "userId" field.
	UserId []byte `json:"userId,omitempty"`
	// AllowedCredentialIds holds the value of the "allowedCredentialIds" field.
	AllowedCredentialIds [][]uint8 `json:"allowedCredentialIds,omitempty"`
	// UserVerification holds the value of the "userVerification" field.
	UserVerification string `json:"userVerification,omitempty"`
	// Extensions holds the value of the "extensions" field.
	Extensions map[string]interface{} `json:"extensions,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the WebAuthnChallengeQuery when eager-loading is set.
	Edges                        WebAuthnChallengeEdges `json:"edges"`
	challenge_webauthn_challenge *uuid.UUID
}

// WebAuthnChallengeEdges holds the relations/edges for other nodes in the graph.
type WebAuthnChallengeEdges struct {
	// 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 [1]bool
}

// 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 WebAuthnChallengeEdges) ChallengeOrErr() (*Challenge, error) {
	if e.loadedTypes[0] {
		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 (*WebAuthnChallenge) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case webauthnchallenge.FieldUserId, webauthnchallenge.FieldAllowedCredentialIds, webauthnchallenge.FieldExtensions:
			values[i] = new([]byte)
		case webauthnchallenge.FieldSdChallenge, webauthnchallenge.FieldUserVerification:
			values[i] = new(sql.NullString)
		case webauthnchallenge.FieldID:
			values[i] = new(uuid.UUID)
		case webauthnchallenge.ForeignKeys[0]: // challenge_webauthn_challenge
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			return nil, fmt.Errorf("unexpected column %q for type WebAuthnChallenge", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the WebAuthnChallenge fields.
func (wac *WebAuthnChallenge) 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 webauthnchallenge.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				wac.ID = *value
			}
		case webauthnchallenge.FieldSdChallenge:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field sdChallenge", values[i])
			} else if value.Valid {
				wac.SdChallenge = value.String
			}
		case webauthnchallenge.FieldUserId:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field userId", values[i])
			} else if value != nil {
				wac.UserId = *value
			}
		case webauthnchallenge.FieldAllowedCredentialIds:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field allowedCredentialIds", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &wac.AllowedCredentialIds); err != nil {
					return fmt.Errorf("unmarshal field allowedCredentialIds: %w", err)
				}
			}
		case webauthnchallenge.FieldUserVerification:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field userVerification", values[i])
			} else if value.Valid {
				wac.UserVerification = value.String
			}
		case webauthnchallenge.FieldExtensions:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field extensions", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &wac.Extensions); err != nil {
					return fmt.Errorf("unmarshal field extensions: %w", err)
				}
			}
		case webauthnchallenge.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field challenge_webauthn_challenge", values[i])
			} else if value.Valid {
				wac.challenge_webauthn_challenge = new(uuid.UUID)
				*wac.challenge_webauthn_challenge = *value.S.(*uuid.UUID)
			}
		}
	}
	return nil
}

// QueryChallenge queries the "challenge" edge of the WebAuthnChallenge entity.
func (wac *WebAuthnChallenge) QueryChallenge() *ChallengeQuery {
	return NewWebAuthnChallengeClient(wac.config).QueryChallenge(wac)
}

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

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

// String implements the fmt.Stringer.
func (wac *WebAuthnChallenge) String() string {
	var builder strings.Builder
	builder.WriteString("WebAuthnChallenge(")
	builder.WriteString(fmt.Sprintf("id=%v, ", wac.ID))
	builder.WriteString("sdChallenge=")
	builder.WriteString(wac.SdChallenge)
	builder.WriteString(", ")
	builder.WriteString("userId=")
	builder.WriteString(fmt.Sprintf("%v", wac.UserId))
	builder.WriteString(", ")
	builder.WriteString("allowedCredentialIds=")
	builder.WriteString(fmt.Sprintf("%v", wac.AllowedCredentialIds))
	builder.WriteString(", ")
	builder.WriteString("userVerification=")
	builder.WriteString(wac.UserVerification)
	builder.WriteString(", ")
	builder.WriteString("extensions=")
	builder.WriteString(fmt.Sprintf("%v", wac.Extensions))
	builder.WriteByte(')')
	return builder.String()
}

// WebAuthnChallenges is a parsable slice of WebAuthnChallenge.
type WebAuthnChallenges []*WebAuthnChallenge
