// 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/Wei-Shaw/sub2api/ent/identityadoptiondecision"
	"github.com/Wei-Shaw/sub2api/ent/pendingauthsession"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// PendingAuthSession is the model entity for the PendingAuthSession schema.
type PendingAuthSession struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `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"`
	// SessionToken holds the value of the "session_token" field.
	SessionToken string `json:"session_token,omitempty"`
	// Intent holds the value of the "intent" field.
	Intent string `json:"intent,omitempty"`
	// ProviderType holds the value of the "provider_type" field.
	ProviderType string `json:"provider_type,omitempty"`
	// ProviderKey holds the value of the "provider_key" field.
	ProviderKey string `json:"provider_key,omitempty"`
	// ProviderSubject holds the value of the "provider_subject" field.
	ProviderSubject string `json:"provider_subject,omitempty"`
	// TargetUserID holds the value of the "target_user_id" field.
	TargetUserID *int64 `json:"target_user_id,omitempty"`
	// RedirectTo holds the value of the "redirect_to" field.
	RedirectTo string `json:"redirect_to,omitempty"`
	// ResolvedEmail holds the value of the "resolved_email" field.
	ResolvedEmail string `json:"resolved_email,omitempty"`
	// RegistrationPasswordHash holds the value of the "registration_password_hash" field.
	RegistrationPasswordHash string `json:"registration_password_hash,omitempty"`
	// UpstreamIdentityClaims holds the value of the "upstream_identity_claims" field.
	UpstreamIdentityClaims map[string]interface{} `json:"upstream_identity_claims,omitempty"`
	// LocalFlowState holds the value of the "local_flow_state" field.
	LocalFlowState map[string]interface{} `json:"local_flow_state,omitempty"`
	// BrowserSessionKey holds the value of the "browser_session_key" field.
	BrowserSessionKey string `json:"browser_session_key,omitempty"`
	// CompletionCodeHash holds the value of the "completion_code_hash" field.
	CompletionCodeHash string `json:"completion_code_hash,omitempty"`
	// CompletionCodeExpiresAt holds the value of the "completion_code_expires_at" field.
	CompletionCodeExpiresAt *time.Time `json:"completion_code_expires_at,omitempty"`
	// EmailVerifiedAt holds the value of the "email_verified_at" field.
	EmailVerifiedAt *time.Time `json:"email_verified_at,omitempty"`
	// PasswordVerifiedAt holds the value of the "password_verified_at" field.
	PasswordVerifiedAt *time.Time `json:"password_verified_at,omitempty"`
	// TotpVerifiedAt holds the value of the "totp_verified_at" field.
	TotpVerifiedAt *time.Time `json:"totp_verified_at,omitempty"`
	// ExpiresAt holds the value of the "expires_at" field.
	ExpiresAt time.Time `json:"expires_at,omitempty"`
	// ConsumedAt holds the value of the "consumed_at" field.
	ConsumedAt *time.Time `json:"consumed_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PendingAuthSessionQuery when eager-loading is set.
	Edges        PendingAuthSessionEdges `json:"edges"`
	selectValues sql.SelectValues
}

// PendingAuthSessionEdges holds the relations/edges for other nodes in the graph.
type PendingAuthSessionEdges struct {
	// TargetUser holds the value of the target_user edge.
	TargetUser *User `json:"target_user,omitempty"`
	// AdoptionDecision holds the value of the adoption_decision edge.
	AdoptionDecision *IdentityAdoptionDecision `json:"adoption_decision,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
}

// TargetUserOrErr returns the TargetUser value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e PendingAuthSessionEdges) TargetUserOrErr() (*User, error) {
	if e.TargetUser != nil {
		return e.TargetUser, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: user.Label}
	}
	return nil, &NotLoadedError{edge: "target_user"}
}

// AdoptionDecisionOrErr returns the AdoptionDecision value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e PendingAuthSessionEdges) AdoptionDecisionOrErr() (*IdentityAdoptionDecision, error) {
	if e.AdoptionDecision != nil {
		return e.AdoptionDecision, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: identityadoptiondecision.Label}
	}
	return nil, &NotLoadedError{edge: "adoption_decision"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*PendingAuthSession) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case pendingauthsession.FieldUpstreamIdentityClaims, pendingauthsession.FieldLocalFlowState:
			values[i] = new([]byte)
		case pendingauthsession.FieldID, pendingauthsession.FieldTargetUserID:
			values[i] = new(sql.NullInt64)
		case pendingauthsession.FieldSessionToken, pendingauthsession.FieldIntent, pendingauthsession.FieldProviderType, pendingauthsession.FieldProviderKey, pendingauthsession.FieldProviderSubject, pendingauthsession.FieldRedirectTo, pendingauthsession.FieldResolvedEmail, pendingauthsession.FieldRegistrationPasswordHash, pendingauthsession.FieldBrowserSessionKey, pendingauthsession.FieldCompletionCodeHash:
			values[i] = new(sql.NullString)
		case pendingauthsession.FieldCreatedAt, pendingauthsession.FieldUpdatedAt, pendingauthsession.FieldCompletionCodeExpiresAt, pendingauthsession.FieldEmailVerifiedAt, pendingauthsession.FieldPasswordVerifiedAt, pendingauthsession.FieldTotpVerifiedAt, pendingauthsession.FieldExpiresAt, pendingauthsession.FieldConsumedAt:
			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 PendingAuthSession fields.
func (_m *PendingAuthSession) 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 pendingauthsession.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int64(value.Int64)
		case pendingauthsession.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 {
				_m.CreatedAt = value.Time
			}
		case pendingauthsession.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 {
				_m.UpdatedAt = value.Time
			}
		case pendingauthsession.FieldSessionToken:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field session_token", values[i])
			} else if value.Valid {
				_m.SessionToken = value.String
			}
		case pendingauthsession.FieldIntent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field intent", values[i])
			} else if value.Valid {
				_m.Intent = value.String
			}
		case pendingauthsession.FieldProviderType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field provider_type", values[i])
			} else if value.Valid {
				_m.ProviderType = value.String
			}
		case pendingauthsession.FieldProviderKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field provider_key", values[i])
			} else if value.Valid {
				_m.ProviderKey = value.String
			}
		case pendingauthsession.FieldProviderSubject:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field provider_subject", values[i])
			} else if value.Valid {
				_m.ProviderSubject = value.String
			}
		case pendingauthsession.FieldTargetUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field target_user_id", values[i])
			} else if value.Valid {
				_m.TargetUserID = new(int64)
				*_m.TargetUserID = value.Int64
			}
		case pendingauthsession.FieldRedirectTo:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field redirect_to", values[i])
			} else if value.Valid {
				_m.RedirectTo = value.String
			}
		case pendingauthsession.FieldResolvedEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field resolved_email", values[i])
			} else if value.Valid {
				_m.ResolvedEmail = value.String
			}
		case pendingauthsession.FieldRegistrationPasswordHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field registration_password_hash", values[i])
			} else if value.Valid {
				_m.RegistrationPasswordHash = value.String
			}
		case pendingauthsession.FieldUpstreamIdentityClaims:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field upstream_identity_claims", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.UpstreamIdentityClaims); err != nil {
					return fmt.Errorf("unmarshal field upstream_identity_claims: %w", err)
				}
			}
		case pendingauthsession.FieldLocalFlowState:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field local_flow_state", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.LocalFlowState); err != nil {
					return fmt.Errorf("unmarshal field local_flow_state: %w", err)
				}
			}
		case pendingauthsession.FieldBrowserSessionKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field browser_session_key", values[i])
			} else if value.Valid {
				_m.BrowserSessionKey = value.String
			}
		case pendingauthsession.FieldCompletionCodeHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field completion_code_hash", values[i])
			} else if value.Valid {
				_m.CompletionCodeHash = value.String
			}
		case pendingauthsession.FieldCompletionCodeExpiresAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field completion_code_expires_at", values[i])
			} else if value.Valid {
				_m.CompletionCodeExpiresAt = new(time.Time)
				*_m.CompletionCodeExpiresAt = value.Time
			}
		case pendingauthsession.FieldEmailVerifiedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field email_verified_at", values[i])
			} else if value.Valid {
				_m.EmailVerifiedAt = new(time.Time)
				*_m.EmailVerifiedAt = value.Time
			}
		case pendingauthsession.FieldPasswordVerifiedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field password_verified_at", values[i])
			} else if value.Valid {
				_m.PasswordVerifiedAt = new(time.Time)
				*_m.PasswordVerifiedAt = value.Time
			}
		case pendingauthsession.FieldTotpVerifiedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field totp_verified_at", values[i])
			} else if value.Valid {
				_m.TotpVerifiedAt = new(time.Time)
				*_m.TotpVerifiedAt = value.Time
			}
		case pendingauthsession.FieldExpiresAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expires_at", values[i])
			} else if value.Valid {
				_m.ExpiresAt = value.Time
			}
		case pendingauthsession.FieldConsumedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field consumed_at", values[i])
			} else if value.Valid {
				_m.ConsumedAt = new(time.Time)
				*_m.ConsumedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTargetUser queries the "target_user" edge of the PendingAuthSession entity.
func (_m *PendingAuthSession) QueryTargetUser() *UserQuery {
	return NewPendingAuthSessionClient(_m.config).QueryTargetUser(_m)
}

// QueryAdoptionDecision queries the "adoption_decision" edge of the PendingAuthSession entity.
func (_m *PendingAuthSession) QueryAdoptionDecision() *IdentityAdoptionDecisionQuery {
	return NewPendingAuthSessionClient(_m.config).QueryAdoptionDecision(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *PendingAuthSession) String() string {
	var builder strings.Builder
	builder.WriteString("PendingAuthSession(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("session_token=")
	builder.WriteString(_m.SessionToken)
	builder.WriteString(", ")
	builder.WriteString("intent=")
	builder.WriteString(_m.Intent)
	builder.WriteString(", ")
	builder.WriteString("provider_type=")
	builder.WriteString(_m.ProviderType)
	builder.WriteString(", ")
	builder.WriteString("provider_key=")
	builder.WriteString(_m.ProviderKey)
	builder.WriteString(", ")
	builder.WriteString("provider_subject=")
	builder.WriteString(_m.ProviderSubject)
	builder.WriteString(", ")
	if v := _m.TargetUserID; v != nil {
		builder.WriteString("target_user_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("redirect_to=")
	builder.WriteString(_m.RedirectTo)
	builder.WriteString(", ")
	builder.WriteString("resolved_email=")
	builder.WriteString(_m.ResolvedEmail)
	builder.WriteString(", ")
	builder.WriteString("registration_password_hash=")
	builder.WriteString(_m.RegistrationPasswordHash)
	builder.WriteString(", ")
	builder.WriteString("upstream_identity_claims=")
	builder.WriteString(fmt.Sprintf("%v", _m.UpstreamIdentityClaims))
	builder.WriteString(", ")
	builder.WriteString("local_flow_state=")
	builder.WriteString(fmt.Sprintf("%v", _m.LocalFlowState))
	builder.WriteString(", ")
	builder.WriteString("browser_session_key=")
	builder.WriteString(_m.BrowserSessionKey)
	builder.WriteString(", ")
	builder.WriteString("completion_code_hash=")
	builder.WriteString(_m.CompletionCodeHash)
	builder.WriteString(", ")
	if v := _m.CompletionCodeExpiresAt; v != nil {
		builder.WriteString("completion_code_expires_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.EmailVerifiedAt; v != nil {
		builder.WriteString("email_verified_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.PasswordVerifiedAt; v != nil {
		builder.WriteString("password_verified_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.TotpVerifiedAt; v != nil {
		builder.WriteString("totp_verified_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("expires_at=")
	builder.WriteString(_m.ExpiresAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := _m.ConsumedAt; v != nil {
		builder.WriteString("consumed_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteByte(')')
	return builder.String()
}

// PendingAuthSessions is a parsable slice of PendingAuthSession.
type PendingAuthSessions []*PendingAuthSession
