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

// AuthIdentity is the model entity for the AuthIdentity schema.
type AuthIdentity 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"`
	// UserID holds the value of the "user_id" field.
	UserID int64 `json:"user_id,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"`
	// VerifiedAt holds the value of the "verified_at" field.
	VerifiedAt *time.Time `json:"verified_at,omitempty"`
	// Issuer holds the value of the "issuer" field.
	Issuer *string `json:"issuer,omitempty"`
	// Metadata holds the value of the "metadata" field.
	Metadata map[string]interface{} `json:"metadata,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the AuthIdentityQuery when eager-loading is set.
	Edges        AuthIdentityEdges `json:"edges"`
	selectValues sql.SelectValues
}

// AuthIdentityEdges holds the relations/edges for other nodes in the graph.
type AuthIdentityEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// Channels holds the value of the channels edge.
	Channels []*AuthIdentityChannel `json:"channels,omitempty"`
	// AdoptionDecisions holds the value of the adoption_decisions edge.
	AdoptionDecisions []*IdentityAdoptionDecision `json:"adoption_decisions,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]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 AuthIdentityEdges) UserOrErr() (*User, error) {
	if e.User != nil {
		return e.User, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: user.Label}
	}
	return nil, &NotLoadedError{edge: "user"}
}

// ChannelsOrErr returns the Channels value or an error if the edge
// was not loaded in eager-loading.
func (e AuthIdentityEdges) ChannelsOrErr() ([]*AuthIdentityChannel, error) {
	if e.loadedTypes[1] {
		return e.Channels, nil
	}
	return nil, &NotLoadedError{edge: "channels"}
}

// AdoptionDecisionsOrErr returns the AdoptionDecisions value or an error if the edge
// was not loaded in eager-loading.
func (e AuthIdentityEdges) AdoptionDecisionsOrErr() ([]*IdentityAdoptionDecision, error) {
	if e.loadedTypes[2] {
		return e.AdoptionDecisions, nil
	}
	return nil, &NotLoadedError{edge: "adoption_decisions"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*AuthIdentity) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case authidentity.FieldMetadata:
			values[i] = new([]byte)
		case authidentity.FieldID, authidentity.FieldUserID:
			values[i] = new(sql.NullInt64)
		case authidentity.FieldProviderType, authidentity.FieldProviderKey, authidentity.FieldProviderSubject, authidentity.FieldIssuer:
			values[i] = new(sql.NullString)
		case authidentity.FieldCreatedAt, authidentity.FieldUpdatedAt, authidentity.FieldVerifiedAt:
			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 AuthIdentity fields.
func (_m *AuthIdentity) 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 authidentity.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 authidentity.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 authidentity.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 authidentity.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 {
				_m.UserID = value.Int64
			}
		case authidentity.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 authidentity.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 authidentity.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 authidentity.FieldVerifiedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field verified_at", values[i])
			} else if value.Valid {
				_m.VerifiedAt = new(time.Time)
				*_m.VerifiedAt = value.Time
			}
		case authidentity.FieldIssuer:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field issuer", values[i])
			} else if value.Valid {
				_m.Issuer = new(string)
				*_m.Issuer = value.String
			}
		case authidentity.FieldMetadata:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field metadata", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Metadata); err != nil {
					return fmt.Errorf("unmarshal field metadata: %w", err)
				}
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the AuthIdentity entity.
func (_m *AuthIdentity) QueryUser() *UserQuery {
	return NewAuthIdentityClient(_m.config).QueryUser(_m)
}

// QueryChannels queries the "channels" edge of the AuthIdentity entity.
func (_m *AuthIdentity) QueryChannels() *AuthIdentityChannelQuery {
	return NewAuthIdentityClient(_m.config).QueryChannels(_m)
}

// QueryAdoptionDecisions queries the "adoption_decisions" edge of the AuthIdentity entity.
func (_m *AuthIdentity) QueryAdoptionDecisions() *IdentityAdoptionDecisionQuery {
	return NewAuthIdentityClient(_m.config).QueryAdoptionDecisions(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *AuthIdentity) String() string {
	var builder strings.Builder
	builder.WriteString("AuthIdentity(")
	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("user_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.UserID))
	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.VerifiedAt; v != nil {
		builder.WriteString("verified_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Issuer; v != nil {
		builder.WriteString("issuer=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("metadata=")
	builder.WriteString(fmt.Sprintf("%v", _m.Metadata))
	builder.WriteByte(')')
	return builder.String()
}

// AuthIdentities is a parsable slice of AuthIdentity.
type AuthIdentities []*AuthIdentity
