// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/emailchallenge"
	"fmt"
	"strings"

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

// EmailChallenge is the model entity for the EmailChallenge schema.
type EmailChallenge struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Code holds the value of the "code" field.
	Code []byte `json:"code,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the EmailChallengeQuery when eager-loading is set.
	Edges                     EmailChallengeEdges `json:"edges"`
	challenge_email_challenge *uuid.UUID
}

// EmailChallengeEdges holds the relations/edges for other nodes in the graph.
type EmailChallengeEdges 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 EmailChallengeEdges) 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 (*EmailChallenge) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case emailchallenge.FieldCode:
			values[i] = new([]byte)
		case emailchallenge.FieldID:
			values[i] = new(uuid.UUID)
		case emailchallenge.ForeignKeys[0]: // challenge_email_challenge
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			return nil, fmt.Errorf("unexpected column %q for type EmailChallenge", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the EmailChallenge fields.
func (ec *EmailChallenge) 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 emailchallenge.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				ec.ID = *value
			}
		case emailchallenge.FieldCode:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field code", values[i])
			} else if value != nil {
				ec.Code = *value
			}
		case emailchallenge.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field challenge_email_challenge", values[i])
			} else if value.Valid {
				ec.challenge_email_challenge = new(uuid.UUID)
				*ec.challenge_email_challenge = *value.S.(*uuid.UUID)
			}
		}
	}
	return nil
}

// QueryChallenge queries the "challenge" edge of the EmailChallenge entity.
func (ec *EmailChallenge) QueryChallenge() *ChallengeQuery {
	return NewEmailChallengeClient(ec.config).QueryChallenge(ec)
}

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

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

// String implements the fmt.Stringer.
func (ec *EmailChallenge) String() string {
	var builder strings.Builder
	builder.WriteString("EmailChallenge(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ec.ID))
	builder.WriteString("code=")
	builder.WriteString(fmt.Sprintf("%v", ec.Code))
	builder.WriteByte(')')
	return builder.String()
}

// EmailChallenges is a parsable slice of EmailChallenge.
type EmailChallenges []*EmailChallenge
