// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/cloudreve/Cloudreve/v4/ent/passkey"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/go-webauthn/webauthn/webauthn"
)

// Passkey is the model entity for the Passkey schema.
type Passkey struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	// UserID holds the value of the "user_id" field.
	UserID int `json:"user_id,omitempty"`
	// CredentialID holds the value of the "credential_id" field.
	CredentialID string `json:"credential_id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// Credential holds the value of the "credential" field.
	Credential *webauthn.Credential `json:"-"`
	// UsedAt holds the value of the "used_at" field.
	UsedAt *time.Time `json:"used_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PasskeyQuery when eager-loading is set.
	Edges        PasskeyEdges `json:"edges"`
	selectValues sql.SelectValues
}

// PasskeyEdges holds the relations/edges for other nodes in the graph.
type PasskeyEdges 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 PasskeyEdges) 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 (*Passkey) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case passkey.FieldCredential:
			values[i] = new([]byte)
		case passkey.FieldID, passkey.FieldUserID:
			values[i] = new(sql.NullInt64)
		case passkey.FieldCredentialID, passkey.FieldName:
			values[i] = new(sql.NullString)
		case passkey.FieldCreatedAt, passkey.FieldUpdatedAt, passkey.FieldDeletedAt, passkey.FieldUsedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Passkey fields.
func (pa *Passkey) 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 passkey.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			pa.ID = int(value.Int64)
		case passkey.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				pa.CreatedAt = value.Time
			}
		case passkey.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				pa.UpdatedAt = value.Time
			}
		case passkey.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				pa.DeletedAt = new(time.Time)
				*pa.DeletedAt = value.Time
			}
		case passkey.FieldUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				pa.UserID = int(value.Int64)
			}
		case passkey.FieldCredentialID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field credential_id", values[i])
			} else if value.Valid {
				pa.CredentialID = value.String
			}
		case passkey.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				pa.Name = value.String
			}
		case passkey.FieldCredential:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field credential", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &pa.Credential); err != nil {
					return fmt.Errorf("unmarshal field credential: %w", err)
				}
			}
		case passkey.FieldUsedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field used_at", values[i])
			} else if value.Valid {
				pa.UsedAt = new(time.Time)
				*pa.UsedAt = value.Time
			}
		default:
			pa.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Passkey.
// This includes values selected through modifiers, order, etc.
func (pa *Passkey) Value(name string) (ent.Value, error) {
	return pa.selectValues.Get(name)
}

// QueryUser queries the "user" edge of the Passkey entity.
func (pa *Passkey) QueryUser() *UserQuery {
	return NewPasskeyClient(pa.config).QueryUser(pa)
}

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

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

// String implements the fmt.Stringer.
func (pa *Passkey) String() string {
	var builder strings.Builder
	builder.WriteString("Passkey(")
	builder.WriteString(fmt.Sprintf("id=%v, ", pa.ID))
	builder.WriteString("created_at=")
	builder.WriteString(pa.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(pa.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := pa.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("user_id=")
	builder.WriteString(fmt.Sprintf("%v", pa.UserID))
	builder.WriteString(", ")
	builder.WriteString("credential_id=")
	builder.WriteString(pa.CredentialID)
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(pa.Name)
	builder.WriteString(", ")
	builder.WriteString("credential=<sensitive>")
	builder.WriteString(", ")
	if v := pa.UsedAt; v != nil {
		builder.WriteString("used_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteByte(')')
	return builder.String()
}

// SetUser manually set the edge as loaded state.
func (e *Passkey) SetUser(v *User) {
	e.Edges.User = v
	e.Edges.loadedTypes[0] = true
}

// Passkeys is a parsable slice of Passkey.
type Passkeys []*Passkey
