// 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/member-gentei/member-gentei/gentei/ent/user"
	"golang.org/x/oauth2"
)

// User is the model entity for the User schema.
type User struct {
	config `json:"-"`
	// ID of the ent.
	// Discord user snowflake
	ID uint64 `json:"id,omitempty"`
	// Username + discriminator
	FullName string `json:"full_name,omitempty"`
	// AvatarHash holds the value of the "avatar_hash" field.
	AvatarHash string `json:"avatar_hash,omitempty"`
	// Timestamp of last membership check
	LastCheck time.Time `json:"last_check,omitempty"`
	// user's YouTube channel ID
	YoutubeID *string `json:"youtube_id,omitempty"`
	// YoutubeToken holds the value of the "youtube_token" field.
	YoutubeToken *oauth2.Token `json:"youtube_token,omitempty"`
	// DiscordToken holds the value of the "discord_token" field.
	DiscordToken *oauth2.Token `json:"discord_token,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserQuery when eager-loading is set.
	Edges        UserEdges `json:"edges"`
	selectValues sql.SelectValues
}

// UserEdges holds the relations/edges for other nodes in the graph.
type UserEdges struct {
	// Guild that this user has joined
	Guilds []*Guild `json:"guilds,omitempty"`
	// GuildsAdmin holds the value of the guilds_admin edge.
	GuildsAdmin []*Guild `json:"guilds_admin,omitempty"`
	// Memberships holds the value of the memberships edge.
	Memberships []*UserMembership `json:"memberships,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// GuildsOrErr returns the Guilds value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) GuildsOrErr() ([]*Guild, error) {
	if e.loadedTypes[0] {
		return e.Guilds, nil
	}
	return nil, &NotLoadedError{edge: "guilds"}
}

// GuildsAdminOrErr returns the GuildsAdmin value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) GuildsAdminOrErr() ([]*Guild, error) {
	if e.loadedTypes[1] {
		return e.GuildsAdmin, nil
	}
	return nil, &NotLoadedError{edge: "guilds_admin"}
}

// MembershipsOrErr returns the Memberships value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) MembershipsOrErr() ([]*UserMembership, error) {
	if e.loadedTypes[2] {
		return e.Memberships, nil
	}
	return nil, &NotLoadedError{edge: "memberships"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*User) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case user.FieldYoutubeToken, user.FieldDiscordToken:
			values[i] = new([]byte)
		case user.FieldID:
			values[i] = new(sql.NullInt64)
		case user.FieldFullName, user.FieldAvatarHash, user.FieldYoutubeID:
			values[i] = new(sql.NullString)
		case user.FieldLastCheck:
			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 User fields.
func (u *User) 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 user.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			u.ID = uint64(value.Int64)
		case user.FieldFullName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field full_name", values[i])
			} else if value.Valid {
				u.FullName = value.String
			}
		case user.FieldAvatarHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field avatar_hash", values[i])
			} else if value.Valid {
				u.AvatarHash = value.String
			}
		case user.FieldLastCheck:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_check", values[i])
			} else if value.Valid {
				u.LastCheck = value.Time
			}
		case user.FieldYoutubeID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field youtube_id", values[i])
			} else if value.Valid {
				u.YoutubeID = new(string)
				*u.YoutubeID = value.String
			}
		case user.FieldYoutubeToken:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field youtube_token", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &u.YoutubeToken); err != nil {
					return fmt.Errorf("unmarshal field youtube_token: %w", err)
				}
			}
		case user.FieldDiscordToken:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field discord_token", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &u.DiscordToken); err != nil {
					return fmt.Errorf("unmarshal field discord_token: %w", err)
				}
			}
		default:
			u.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryGuilds queries the "guilds" edge of the User entity.
func (u *User) QueryGuilds() *GuildQuery {
	return NewUserClient(u.config).QueryGuilds(u)
}

// QueryGuildsAdmin queries the "guilds_admin" edge of the User entity.
func (u *User) QueryGuildsAdmin() *GuildQuery {
	return NewUserClient(u.config).QueryGuildsAdmin(u)
}

// QueryMemberships queries the "memberships" edge of the User entity.
func (u *User) QueryMemberships() *UserMembershipQuery {
	return NewUserClient(u.config).QueryMemberships(u)
}

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

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

// String implements the fmt.Stringer.
func (u *User) String() string {
	var builder strings.Builder
	builder.WriteString("User(")
	builder.WriteString(fmt.Sprintf("id=%v, ", u.ID))
	builder.WriteString("full_name=")
	builder.WriteString(u.FullName)
	builder.WriteString(", ")
	builder.WriteString("avatar_hash=")
	builder.WriteString(u.AvatarHash)
	builder.WriteString(", ")
	builder.WriteString("last_check=")
	builder.WriteString(u.LastCheck.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := u.YoutubeID; v != nil {
		builder.WriteString("youtube_id=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("youtube_token=")
	builder.WriteString(fmt.Sprintf("%v", u.YoutubeToken))
	builder.WriteString(", ")
	builder.WriteString("discord_token=")
	builder.WriteString(fmt.Sprintf("%v", u.DiscordToken))
	builder.WriteByte(')')
	return builder.String()
}

// Users is a parsable slice of User.
type Users []*User
