// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/user"
	"PasswordManager/ent/webauthncredential"
	"encoding/json"
	"fmt"
	"strings"
	"time"

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

// WebAuthnCredential is the model entity for the WebAuthnCredential schema.
type WebAuthnCredential struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// CreatedAt holds the value of the "createdAt" field.
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// CredentialId holds the value of the "credentialId" field.
	CredentialId []byte `json:"credentialId,omitempty"`
	// PublicKey holds the value of the "publicKey" field.
	PublicKey []byte `json:"publicKey,omitempty"`
	// AttestationType holds the value of the "attestationType" field.
	AttestationType string `json:"attestationType,omitempty"`
	// Transport holds the value of the "transport" field.
	Transport []string `json:"transport,omitempty"`
	// Aaguid holds the value of the "aaguid" field.
	Aaguid []byte `json:"aaguid,omitempty"`
	// SignCount holds the value of the "signCount" field.
	SignCount uint32 `json:"signCount,omitempty"`
	// CloneWarning holds the value of the "cloneWarning" field.
	CloneWarning bool `json:"cloneWarning,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the WebAuthnCredentialQuery when eager-loading is set.
	Edges                     WebAuthnCredentialEdges `json:"edges"`
	user_webauthn_credentials *uuid.UUID
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*WebAuthnCredential) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case webauthncredential.FieldCredentialId, webauthncredential.FieldPublicKey, webauthncredential.FieldTransport, webauthncredential.FieldAaguid:
			values[i] = new([]byte)
		case webauthncredential.FieldCloneWarning:
			values[i] = new(sql.NullBool)
		case webauthncredential.FieldSignCount:
			values[i] = new(sql.NullInt64)
		case webauthncredential.FieldName, webauthncredential.FieldAttestationType:
			values[i] = new(sql.NullString)
		case webauthncredential.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		case webauthncredential.FieldID:
			values[i] = new(uuid.UUID)
		case webauthncredential.ForeignKeys[0]: // user_webauthn_credentials
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			return nil, fmt.Errorf("unexpected column %q for type WebAuthnCredential", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the WebAuthnCredential fields.
func (wac *WebAuthnCredential) 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 webauthncredential.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 webauthncredential.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				wac.Name = value.String
			}
		case webauthncredential.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field createdAt", values[i])
			} else if value.Valid {
				wac.CreatedAt = value.Time
			}
		case webauthncredential.FieldCredentialId:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field credentialId", values[i])
			} else if value != nil {
				wac.CredentialId = *value
			}
		case webauthncredential.FieldPublicKey:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field publicKey", values[i])
			} else if value != nil {
				wac.PublicKey = *value
			}
		case webauthncredential.FieldAttestationType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field attestationType", values[i])
			} else if value.Valid {
				wac.AttestationType = value.String
			}
		case webauthncredential.FieldTransport:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field transport", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &wac.Transport); err != nil {
					return fmt.Errorf("unmarshal field transport: %w", err)
				}
			}
		case webauthncredential.FieldAaguid:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field aaguid", values[i])
			} else if value != nil {
				wac.Aaguid = *value
			}
		case webauthncredential.FieldSignCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field signCount", values[i])
			} else if value.Valid {
				wac.SignCount = uint32(value.Int64)
			}
		case webauthncredential.FieldCloneWarning:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field cloneWarning", values[i])
			} else if value.Valid {
				wac.CloneWarning = value.Bool
			}
		case webauthncredential.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_webauthn_credentials", values[i])
			} else if value.Valid {
				wac.user_webauthn_credentials = new(uuid.UUID)
				*wac.user_webauthn_credentials = *value.S.(*uuid.UUID)
			}
		}
	}
	return nil
}

// QueryUser queries the "user" edge of the WebAuthnCredential entity.
func (wac *WebAuthnCredential) QueryUser() *UserQuery {
	return NewWebAuthnCredentialClient(wac.config).QueryUser(wac)
}

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

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

// String implements the fmt.Stringer.
func (wac *WebAuthnCredential) String() string {
	var builder strings.Builder
	builder.WriteString("WebAuthnCredential(")
	builder.WriteString(fmt.Sprintf("id=%v, ", wac.ID))
	builder.WriteString("name=")
	builder.WriteString(wac.Name)
	builder.WriteString(", ")
	builder.WriteString("createdAt=")
	builder.WriteString(wac.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("credentialId=")
	builder.WriteString(fmt.Sprintf("%v", wac.CredentialId))
	builder.WriteString(", ")
	builder.WriteString("publicKey=")
	builder.WriteString(fmt.Sprintf("%v", wac.PublicKey))
	builder.WriteString(", ")
	builder.WriteString("attestationType=")
	builder.WriteString(wac.AttestationType)
	builder.WriteString(", ")
	builder.WriteString("transport=")
	builder.WriteString(fmt.Sprintf("%v", wac.Transport))
	builder.WriteString(", ")
	builder.WriteString("aaguid=")
	builder.WriteString(fmt.Sprintf("%v", wac.Aaguid))
	builder.WriteString(", ")
	builder.WriteString("signCount=")
	builder.WriteString(fmt.Sprintf("%v", wac.SignCount))
	builder.WriteString(", ")
	builder.WriteString("cloneWarning=")
	builder.WriteString(fmt.Sprintf("%v", wac.CloneWarning))
	builder.WriteByte(')')
	return builder.String()
}

// WebAuthnCredentials is a parsable slice of WebAuthnCredential.
type WebAuthnCredentials []*WebAuthnCredential
