// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/anzhiyu-c/anheyu-app/ent/user"
	"github.com/anzhiyu-c/anheyu-app/ent/usergroup"
)

// User is the model entity for the User schema.
type User struct {
	config `json:"-"`
	// ID of the ent.
	ID uint `json:"id,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,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"`
	// 用户账号
	Username string `json:"username,omitempty"`
	// PasswordHash holds the value of the "password_hash" field.
	PasswordHash string `json:"-"`
	// 用户昵称
	Nickname string `json:"nickname,omitempty"`
	// 用户头像URL
	Avatar string `json:"avatar,omitempty"`
	// 用户邮箱
	Email string `json:"email,omitempty"`
	// 用户个人网站
	Website string `json:"website,omitempty"`
	// LastLoginAt holds the value of the "last_login_at" field.
	LastLoginAt *time.Time `json:"last_login_at,omitempty"`
	// 用户状态 1:正常 2:未激活 3:已封禁
	Status int `json:"status,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"`
	user_group_id *uint
	selectValues  sql.SelectValues
}

// UserEdges holds the relations/edges for other nodes in the graph.
type UserEdges struct {
	// UserGroup holds the value of the user_group edge.
	UserGroup *UserGroup `json:"user_group,omitempty"`
	// Files holds the value of the files edge.
	Files []*File `json:"files,omitempty"`
	// Comments holds the value of the comments edge.
	Comments []*Comment `json:"comments,omitempty"`
	// InstalledThemes holds the value of the installed_themes edge.
	InstalledThemes []*UserInstalledTheme `json:"installed_themes,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
}

// UserGroupOrErr returns the UserGroup value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) UserGroupOrErr() (*UserGroup, error) {
	if e.UserGroup != nil {
		return e.UserGroup, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: usergroup.Label}
	}
	return nil, &NotLoadedError{edge: "user_group"}
}

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

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

// InstalledThemesOrErr returns the InstalledThemes value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) InstalledThemesOrErr() ([]*UserInstalledTheme, error) {
	if e.loadedTypes[3] {
		return e.InstalledThemes, nil
	}
	return nil, &NotLoadedError{edge: "installed_themes"}
}

// 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.FieldID, user.FieldStatus:
			values[i] = new(sql.NullInt64)
		case user.FieldUsername, user.FieldPasswordHash, user.FieldNickname, user.FieldAvatar, user.FieldEmail, user.FieldWebsite:
			values[i] = new(sql.NullString)
		case user.FieldDeletedAt, user.FieldCreatedAt, user.FieldUpdatedAt, user.FieldLastLoginAt:
			values[i] = new(sql.NullTime)
		case user.ForeignKeys[0]: // user_group_id
			values[i] = new(sql.NullInt64)
		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 (_m *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)
			}
			_m.ID = uint(value.Int64)
		case user.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 {
				_m.DeletedAt = new(time.Time)
				*_m.DeletedAt = value.Time
			}
		case user.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 user.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 user.FieldUsername:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field username", values[i])
			} else if value.Valid {
				_m.Username = value.String
			}
		case user.FieldPasswordHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field password_hash", values[i])
			} else if value.Valid {
				_m.PasswordHash = value.String
			}
		case user.FieldNickname:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nickname", values[i])
			} else if value.Valid {
				_m.Nickname = value.String
			}
		case user.FieldAvatar:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field avatar", values[i])
			} else if value.Valid {
				_m.Avatar = value.String
			}
		case user.FieldEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email", values[i])
			} else if value.Valid {
				_m.Email = value.String
			}
		case user.FieldWebsite:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field website", values[i])
			} else if value.Valid {
				_m.Website = value.String
			}
		case user.FieldLastLoginAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_login_at", values[i])
			} else if value.Valid {
				_m.LastLoginAt = new(time.Time)
				*_m.LastLoginAt = value.Time
			}
		case user.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = int(value.Int64)
			}
		case user.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field user_group_id", value)
			} else if value.Valid {
				_m.user_group_id = new(uint)
				*_m.user_group_id = uint(value.Int64)
			}
		default:
			_m.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 (_m *User) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryUserGroup queries the "user_group" edge of the User entity.
func (_m *User) QueryUserGroup() *UserGroupQuery {
	return NewUserClient(_m.config).QueryUserGroup(_m)
}

// QueryFiles queries the "files" edge of the User entity.
func (_m *User) QueryFiles() *FileQuery {
	return NewUserClient(_m.config).QueryFiles(_m)
}

// QueryComments queries the "comments" edge of the User entity.
func (_m *User) QueryComments() *CommentQuery {
	return NewUserClient(_m.config).QueryComments(_m)
}

// QueryInstalledThemes queries the "installed_themes" edge of the User entity.
func (_m *User) QueryInstalledThemes() *UserInstalledThemeQuery {
	return NewUserClient(_m.config).QueryInstalledThemes(_m)
}

// 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 (_m *User) Update() *UserUpdateOne {
	return NewUserClient(_m.config).UpdateOne(_m)
}

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

// String implements the fmt.Stringer.
func (_m *User) String() string {
	var builder strings.Builder
	builder.WriteString("User(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	if v := _m.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	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("username=")
	builder.WriteString(_m.Username)
	builder.WriteString(", ")
	builder.WriteString("password_hash=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("nickname=")
	builder.WriteString(_m.Nickname)
	builder.WriteString(", ")
	builder.WriteString("avatar=")
	builder.WriteString(_m.Avatar)
	builder.WriteString(", ")
	builder.WriteString("email=")
	builder.WriteString(_m.Email)
	builder.WriteString(", ")
	builder.WriteString("website=")
	builder.WriteString(_m.Website)
	builder.WriteString(", ")
	if v := _m.LastLoginAt; v != nil {
		builder.WriteString("last_login_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteByte(')')
	return builder.String()
}

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