// 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/cloudreve/Cloudreve/v4/ent/group"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
)

// User is the model entity for the User schema.
type User struct {
	config `json:"-"`
	// ID of the ent.
	ID int `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"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	// Email holds the value of the "email" field.
	Email string `json:"email,omitempty"`
	// Nick holds the value of the "nick" field.
	Nick string `json:"nick,omitempty"`
	// Password holds the value of the "password" field.
	Password string `json:"-"`
	// Status holds the value of the "status" field.
	Status user.Status `json:"status,omitempty"`
	// Storage holds the value of the "storage" field.
	Storage int64 `json:"storage,omitempty"`
	// TwoFactorSecret holds the value of the "two_factor_secret" field.
	TwoFactorSecret string `json:"-"`
	// Avatar holds the value of the "avatar" field.
	Avatar string `json:"avatar,omitempty"`
	// Settings holds the value of the "settings" field.
	Settings *types.UserSetting `json:"settings,omitempty"`
	// GroupUsers holds the value of the "group_users" field.
	GroupUsers int `json:"group_users,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 {
	// Group holds the value of the group edge.
	Group *Group `json:"group,omitempty"`
	// Files holds the value of the files edge.
	Files []*File `json:"files,omitempty"`
	// DavAccounts holds the value of the dav_accounts edge.
	DavAccounts []*DavAccount `json:"dav_accounts,omitempty"`
	// Shares holds the value of the shares edge.
	Shares []*Share `json:"shares,omitempty"`
	// Passkey holds the value of the passkey edge.
	Passkey []*Passkey `json:"passkey,omitempty"`
	// Tasks holds the value of the tasks edge.
	Tasks []*Task `json:"tasks,omitempty"`
	// Entities holds the value of the entities edge.
	Entities []*Entity `json:"entities,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [7]bool
}

// GroupOrErr returns the Group value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) GroupOrErr() (*Group, error) {
	if e.loadedTypes[0] {
		if e.Group == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: group.Label}
		}
		return e.Group, nil
	}
	return nil, &NotLoadedError{edge: "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"}
}

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

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

// PasskeyOrErr returns the Passkey value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) PasskeyOrErr() ([]*Passkey, error) {
	if e.loadedTypes[4] {
		return e.Passkey, nil
	}
	return nil, &NotLoadedError{edge: "passkey"}
}

// TasksOrErr returns the Tasks value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) TasksOrErr() ([]*Task, error) {
	if e.loadedTypes[5] {
		return e.Tasks, nil
	}
	return nil, &NotLoadedError{edge: "tasks"}
}

// EntitiesOrErr returns the Entities value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) EntitiesOrErr() ([]*Entity, error) {
	if e.loadedTypes[6] {
		return e.Entities, nil
	}
	return nil, &NotLoadedError{edge: "entities"}
}

// 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.FieldSettings:
			values[i] = new([]byte)
		case user.FieldID, user.FieldStorage, user.FieldGroupUsers:
			values[i] = new(sql.NullInt64)
		case user.FieldEmail, user.FieldNick, user.FieldPassword, user.FieldStatus, user.FieldTwoFactorSecret, user.FieldAvatar:
			values[i] = new(sql.NullString)
		case user.FieldCreatedAt, user.FieldUpdatedAt, user.FieldDeletedAt:
			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 = int(value.Int64)
		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 {
				u.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 {
				u.UpdatedAt = value.Time
			}
		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 {
				u.DeletedAt = new(time.Time)
				*u.DeletedAt = value.Time
			}
		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 {
				u.Email = value.String
			}
		case user.FieldNick:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nick", values[i])
			} else if value.Valid {
				u.Nick = value.String
			}
		case user.FieldPassword:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field password", values[i])
			} else if value.Valid {
				u.Password = value.String
			}
		case user.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				u.Status = user.Status(value.String)
			}
		case user.FieldStorage:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field storage", values[i])
			} else if value.Valid {
				u.Storage = value.Int64
			}
		case user.FieldTwoFactorSecret:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field two_factor_secret", values[i])
			} else if value.Valid {
				u.TwoFactorSecret = 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 {
				u.Avatar = value.String
			}
		case user.FieldSettings:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field settings", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &u.Settings); err != nil {
					return fmt.Errorf("unmarshal field settings: %w", err)
				}
			}
		case user.FieldGroupUsers:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field group_users", values[i])
			} else if value.Valid {
				u.GroupUsers = int(value.Int64)
			}
		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)
}

// QueryGroup queries the "group" edge of the User entity.
func (u *User) QueryGroup() *GroupQuery {
	return NewUserClient(u.config).QueryGroup(u)
}

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

// QueryDavAccounts queries the "dav_accounts" edge of the User entity.
func (u *User) QueryDavAccounts() *DavAccountQuery {
	return NewUserClient(u.config).QueryDavAccounts(u)
}

// QueryShares queries the "shares" edge of the User entity.
func (u *User) QueryShares() *ShareQuery {
	return NewUserClient(u.config).QueryShares(u)
}

// QueryPasskey queries the "passkey" edge of the User entity.
func (u *User) QueryPasskey() *PasskeyQuery {
	return NewUserClient(u.config).QueryPasskey(u)
}

// QueryTasks queries the "tasks" edge of the User entity.
func (u *User) QueryTasks() *TaskQuery {
	return NewUserClient(u.config).QueryTasks(u)
}

// QueryEntities queries the "entities" edge of the User entity.
func (u *User) QueryEntities() *EntityQuery {
	return NewUserClient(u.config).QueryEntities(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("created_at=")
	builder.WriteString(u.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(u.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := u.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("email=")
	builder.WriteString(u.Email)
	builder.WriteString(", ")
	builder.WriteString("nick=")
	builder.WriteString(u.Nick)
	builder.WriteString(", ")
	builder.WriteString("password=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", u.Status))
	builder.WriteString(", ")
	builder.WriteString("storage=")
	builder.WriteString(fmt.Sprintf("%v", u.Storage))
	builder.WriteString(", ")
	builder.WriteString("two_factor_secret=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("avatar=")
	builder.WriteString(u.Avatar)
	builder.WriteString(", ")
	builder.WriteString("settings=")
	builder.WriteString(fmt.Sprintf("%v", u.Settings))
	builder.WriteString(", ")
	builder.WriteString("group_users=")
	builder.WriteString(fmt.Sprintf("%v", u.GroupUsers))
	builder.WriteByte(')')
	return builder.String()
}

// SetGroup manually set the edge as loaded state.
func (e *User) SetGroup(v *Group) {
	e.Edges.Group = v
	e.Edges.loadedTypes[0] = true
}

// SetFiles manually set the edge as loaded state.
func (e *User) SetFiles(v []*File) {
	e.Edges.Files = v
	e.Edges.loadedTypes[1] = true
}

// SetDavAccounts manually set the edge as loaded state.
func (e *User) SetDavAccounts(v []*DavAccount) {
	e.Edges.DavAccounts = v
	e.Edges.loadedTypes[2] = true
}

// SetShares manually set the edge as loaded state.
func (e *User) SetShares(v []*Share) {
	e.Edges.Shares = v
	e.Edges.loadedTypes[3] = true
}

// SetPasskey manually set the edge as loaded state.
func (e *User) SetPasskey(v []*Passkey) {
	e.Edges.Passkey = v
	e.Edges.loadedTypes[4] = true
}

// SetTasks manually set the edge as loaded state.
func (e *User) SetTasks(v []*Task) {
	e.Edges.Tasks = v
	e.Edges.loadedTypes[5] = true
}

// SetEntities manually set the edge as loaded state.
func (e *User) SetEntities(v []*Entity) {
	e.Edges.Entities = v
	e.Edges.loadedTypes[6] = true
}

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