// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.dcentral.systems/toolz/goplt/internal/ent/predicate"
	"git.dcentral.systems/toolz/goplt/internal/ent/user"
	"git.dcentral.systems/toolz/goplt/internal/ent/userrole"
)

// UserUpdate is the builder for updating User entities.
type UserUpdate struct {
	config
	hooks    []Hook
	mutation *UserMutation
}

// Where appends a list predicates to the UserUpdate builder.
func (_u *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetEmail sets the "email" field.
func (_u *UserUpdate) SetEmail(v string) *UserUpdate {
	_u.mutation.SetEmail(v)
	return _u
}

// SetNillableEmail sets the "email" field if the given value is not nil.
func (_u *UserUpdate) SetNillableEmail(v *string) *UserUpdate {
	if v != nil {
		_u.SetEmail(*v)
	}
	return _u
}

// SetUsername sets the "username" field.
func (_u *UserUpdate) SetUsername(v string) *UserUpdate {
	_u.mutation.SetUsername(v)
	return _u
}

// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *UserUpdate) SetNillableUsername(v *string) *UserUpdate {
	if v != nil {
		_u.SetUsername(*v)
	}
	return _u
}

// ClearUsername clears the value of the "username" field.
func (_u *UserUpdate) ClearUsername() *UserUpdate {
	_u.mutation.ClearUsername()
	return _u
}

// SetFirstName sets the "first_name" field.
func (_u *UserUpdate) SetFirstName(v string) *UserUpdate {
	_u.mutation.SetFirstName(v)
	return _u
}

// SetNillableFirstName sets the "first_name" field if the given value is not nil.
func (_u *UserUpdate) SetNillableFirstName(v *string) *UserUpdate {
	if v != nil {
		_u.SetFirstName(*v)
	}
	return _u
}

// ClearFirstName clears the value of the "first_name" field.
func (_u *UserUpdate) ClearFirstName() *UserUpdate {
	_u.mutation.ClearFirstName()
	return _u
}

// SetLastName sets the "last_name" field.
func (_u *UserUpdate) SetLastName(v string) *UserUpdate {
	_u.mutation.SetLastName(v)
	return _u
}

// SetNillableLastName sets the "last_name" field if the given value is not nil.
func (_u *UserUpdate) SetNillableLastName(v *string) *UserUpdate {
	if v != nil {
		_u.SetLastName(*v)
	}
	return _u
}

// ClearLastName clears the value of the "last_name" field.
func (_u *UserUpdate) ClearLastName() *UserUpdate {
	_u.mutation.ClearLastName()
	return _u
}

// SetPasswordHash sets the "password_hash" field.
func (_u *UserUpdate) SetPasswordHash(v string) *UserUpdate {
	_u.mutation.SetPasswordHash(v)
	return _u
}

// SetNillablePasswordHash sets the "password_hash" field if the given value is not nil.
func (_u *UserUpdate) SetNillablePasswordHash(v *string) *UserUpdate {
	if v != nil {
		_u.SetPasswordHash(*v)
	}
	return _u
}

// SetVerified sets the "verified" field.
func (_u *UserUpdate) SetVerified(v bool) *UserUpdate {
	_u.mutation.SetVerified(v)
	return _u
}

// SetNillableVerified sets the "verified" field if the given value is not nil.
func (_u *UserUpdate) SetNillableVerified(v *bool) *UserUpdate {
	if v != nil {
		_u.SetVerified(*v)
	}
	return _u
}

// SetEmailVerificationToken sets the "email_verification_token" field.
func (_u *UserUpdate) SetEmailVerificationToken(v string) *UserUpdate {
	_u.mutation.SetEmailVerificationToken(v)
	return _u
}

// SetNillableEmailVerificationToken sets the "email_verification_token" field if the given value is not nil.
func (_u *UserUpdate) SetNillableEmailVerificationToken(v *string) *UserUpdate {
	if v != nil {
		_u.SetEmailVerificationToken(*v)
	}
	return _u
}

// ClearEmailVerificationToken clears the value of the "email_verification_token" field.
func (_u *UserUpdate) ClearEmailVerificationToken() *UserUpdate {
	_u.mutation.ClearEmailVerificationToken()
	return _u
}

// SetPasswordResetToken sets the "password_reset_token" field.
func (_u *UserUpdate) SetPasswordResetToken(v string) *UserUpdate {
	_u.mutation.SetPasswordResetToken(v)
	return _u
}

// SetNillablePasswordResetToken sets the "password_reset_token" field if the given value is not nil.
func (_u *UserUpdate) SetNillablePasswordResetToken(v *string) *UserUpdate {
	if v != nil {
		_u.SetPasswordResetToken(*v)
	}
	return _u
}

// ClearPasswordResetToken clears the value of the "password_reset_token" field.
func (_u *UserUpdate) ClearPasswordResetToken() *UserUpdate {
	_u.mutation.ClearPasswordResetToken()
	return _u
}

// SetPasswordResetExpiresAt sets the "password_reset_expires_at" field.
func (_u *UserUpdate) SetPasswordResetExpiresAt(v time.Time) *UserUpdate {
	_u.mutation.SetPasswordResetExpiresAt(v)
	return _u
}

// SetNillablePasswordResetExpiresAt sets the "password_reset_expires_at" field if the given value is not nil.
func (_u *UserUpdate) SetNillablePasswordResetExpiresAt(v *time.Time) *UserUpdate {
	if v != nil {
		_u.SetPasswordResetExpiresAt(*v)
	}
	return _u
}

// ClearPasswordResetExpiresAt clears the value of the "password_reset_expires_at" field.
func (_u *UserUpdate) ClearPasswordResetExpiresAt() *UserUpdate {
	_u.mutation.ClearPasswordResetExpiresAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *UserUpdate) SetUpdatedAt(v time.Time) *UserUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs.
func (_u *UserUpdate) AddUserRoleIDs(ids ...int) *UserUpdate {
	_u.mutation.AddUserRoleIDs(ids...)
	return _u
}

// AddUserRoles adds the "user_roles" edges to the UserRole entity.
func (_u *UserUpdate) AddUserRoles(v ...*UserRole) *UserUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUserRoleIDs(ids...)
}

// Mutation returns the UserMutation object of the builder.
func (_u *UserUpdate) Mutation() *UserMutation {
	return _u.mutation
}

// ClearUserRoles clears all "user_roles" edges to the UserRole entity.
func (_u *UserUpdate) ClearUserRoles() *UserUpdate {
	_u.mutation.ClearUserRoles()
	return _u
}

// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs.
func (_u *UserUpdate) RemoveUserRoleIDs(ids ...int) *UserUpdate {
	_u.mutation.RemoveUserRoleIDs(ids...)
	return _u
}

// RemoveUserRoles removes "user_roles" edges to UserRole entities.
func (_u *UserUpdate) RemoveUserRoles(v ...*UserRole) *UserUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUserRoleIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *UserUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *UserUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *UserUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *UserUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *UserUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := user.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserUpdate) check() error {
	if v, ok := _u.mutation.Email(); ok {
		if err := user.EmailValidator(v); err != nil {
			return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PasswordHash(); ok {
		if err := user.PasswordHashValidator(v); err != nil {
			return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)}
		}
	}
	return nil
}

func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Email(); ok {
		_spec.SetField(user.FieldEmail, field.TypeString, value)
	}
	if value, ok := _u.mutation.Username(); ok {
		_spec.SetField(user.FieldUsername, field.TypeString, value)
	}
	if _u.mutation.UsernameCleared() {
		_spec.ClearField(user.FieldUsername, field.TypeString)
	}
	if value, ok := _u.mutation.FirstName(); ok {
		_spec.SetField(user.FieldFirstName, field.TypeString, value)
	}
	if _u.mutation.FirstNameCleared() {
		_spec.ClearField(user.FieldFirstName, field.TypeString)
	}
	if value, ok := _u.mutation.LastName(); ok {
		_spec.SetField(user.FieldLastName, field.TypeString, value)
	}
	if _u.mutation.LastNameCleared() {
		_spec.ClearField(user.FieldLastName, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordHash(); ok {
		_spec.SetField(user.FieldPasswordHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.Verified(); ok {
		_spec.SetField(user.FieldVerified, field.TypeBool, value)
	}
	if value, ok := _u.mutation.EmailVerificationToken(); ok {
		_spec.SetField(user.FieldEmailVerificationToken, field.TypeString, value)
	}
	if _u.mutation.EmailVerificationTokenCleared() {
		_spec.ClearField(user.FieldEmailVerificationToken, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordResetToken(); ok {
		_spec.SetField(user.FieldPasswordResetToken, field.TypeString, value)
	}
	if _u.mutation.PasswordResetTokenCleared() {
		_spec.ClearField(user.FieldPasswordResetToken, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordResetExpiresAt(); ok {
		_spec.SetField(user.FieldPasswordResetExpiresAt, field.TypeTime, value)
	}
	if _u.mutation.PasswordResetExpiresAtCleared() {
		_spec.ClearField(user.FieldPasswordResetExpiresAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.UserRolesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{user.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserUpdateOne is the builder for updating a single User entity.
type UserUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserMutation
}

// SetEmail sets the "email" field.
func (_u *UserUpdateOne) SetEmail(v string) *UserUpdateOne {
	_u.mutation.SetEmail(v)
	return _u
}

// SetNillableEmail sets the "email" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableEmail(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetEmail(*v)
	}
	return _u
}

// SetUsername sets the "username" field.
func (_u *UserUpdateOne) SetUsername(v string) *UserUpdateOne {
	_u.mutation.SetUsername(v)
	return _u
}

// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableUsername(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetUsername(*v)
	}
	return _u
}

// ClearUsername clears the value of the "username" field.
func (_u *UserUpdateOne) ClearUsername() *UserUpdateOne {
	_u.mutation.ClearUsername()
	return _u
}

// SetFirstName sets the "first_name" field.
func (_u *UserUpdateOne) SetFirstName(v string) *UserUpdateOne {
	_u.mutation.SetFirstName(v)
	return _u
}

// SetNillableFirstName sets the "first_name" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableFirstName(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetFirstName(*v)
	}
	return _u
}

// ClearFirstName clears the value of the "first_name" field.
func (_u *UserUpdateOne) ClearFirstName() *UserUpdateOne {
	_u.mutation.ClearFirstName()
	return _u
}

// SetLastName sets the "last_name" field.
func (_u *UserUpdateOne) SetLastName(v string) *UserUpdateOne {
	_u.mutation.SetLastName(v)
	return _u
}

// SetNillableLastName sets the "last_name" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableLastName(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetLastName(*v)
	}
	return _u
}

// ClearLastName clears the value of the "last_name" field.
func (_u *UserUpdateOne) ClearLastName() *UserUpdateOne {
	_u.mutation.ClearLastName()
	return _u
}

// SetPasswordHash sets the "password_hash" field.
func (_u *UserUpdateOne) SetPasswordHash(v string) *UserUpdateOne {
	_u.mutation.SetPasswordHash(v)
	return _u
}

// SetNillablePasswordHash sets the "password_hash" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillablePasswordHash(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetPasswordHash(*v)
	}
	return _u
}

// SetVerified sets the "verified" field.
func (_u *UserUpdateOne) SetVerified(v bool) *UserUpdateOne {
	_u.mutation.SetVerified(v)
	return _u
}

// SetNillableVerified sets the "verified" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableVerified(v *bool) *UserUpdateOne {
	if v != nil {
		_u.SetVerified(*v)
	}
	return _u
}

// SetEmailVerificationToken sets the "email_verification_token" field.
func (_u *UserUpdateOne) SetEmailVerificationToken(v string) *UserUpdateOne {
	_u.mutation.SetEmailVerificationToken(v)
	return _u
}

// SetNillableEmailVerificationToken sets the "email_verification_token" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableEmailVerificationToken(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetEmailVerificationToken(*v)
	}
	return _u
}

// ClearEmailVerificationToken clears the value of the "email_verification_token" field.
func (_u *UserUpdateOne) ClearEmailVerificationToken() *UserUpdateOne {
	_u.mutation.ClearEmailVerificationToken()
	return _u
}

// SetPasswordResetToken sets the "password_reset_token" field.
func (_u *UserUpdateOne) SetPasswordResetToken(v string) *UserUpdateOne {
	_u.mutation.SetPasswordResetToken(v)
	return _u
}

// SetNillablePasswordResetToken sets the "password_reset_token" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillablePasswordResetToken(v *string) *UserUpdateOne {
	if v != nil {
		_u.SetPasswordResetToken(*v)
	}
	return _u
}

// ClearPasswordResetToken clears the value of the "password_reset_token" field.
func (_u *UserUpdateOne) ClearPasswordResetToken() *UserUpdateOne {
	_u.mutation.ClearPasswordResetToken()
	return _u
}

// SetPasswordResetExpiresAt sets the "password_reset_expires_at" field.
func (_u *UserUpdateOne) SetPasswordResetExpiresAt(v time.Time) *UserUpdateOne {
	_u.mutation.SetPasswordResetExpiresAt(v)
	return _u
}

// SetNillablePasswordResetExpiresAt sets the "password_reset_expires_at" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillablePasswordResetExpiresAt(v *time.Time) *UserUpdateOne {
	if v != nil {
		_u.SetPasswordResetExpiresAt(*v)
	}
	return _u
}

// ClearPasswordResetExpiresAt clears the value of the "password_reset_expires_at" field.
func (_u *UserUpdateOne) ClearPasswordResetExpiresAt() *UserUpdateOne {
	_u.mutation.ClearPasswordResetExpiresAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *UserUpdateOne) SetUpdatedAt(v time.Time) *UserUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// AddUserRoleIDs adds the "user_roles" edge to the UserRole entity by IDs.
func (_u *UserUpdateOne) AddUserRoleIDs(ids ...int) *UserUpdateOne {
	_u.mutation.AddUserRoleIDs(ids...)
	return _u
}

// AddUserRoles adds the "user_roles" edges to the UserRole entity.
func (_u *UserUpdateOne) AddUserRoles(v ...*UserRole) *UserUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUserRoleIDs(ids...)
}

// Mutation returns the UserMutation object of the builder.
func (_u *UserUpdateOne) Mutation() *UserMutation {
	return _u.mutation
}

// ClearUserRoles clears all "user_roles" edges to the UserRole entity.
func (_u *UserUpdateOne) ClearUserRoles() *UserUpdateOne {
	_u.mutation.ClearUserRoles()
	return _u
}

// RemoveUserRoleIDs removes the "user_roles" edge to UserRole entities by IDs.
func (_u *UserUpdateOne) RemoveUserRoleIDs(ids ...int) *UserUpdateOne {
	_u.mutation.RemoveUserRoleIDs(ids...)
	return _u
}

// RemoveUserRoles removes "user_roles" edges to UserRole entities.
func (_u *UserUpdateOne) RemoveUserRoles(v ...*UserRole) *UserUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUserRoleIDs(ids...)
}

// Where appends a list predicates to the UserUpdate builder.
func (_u *UserUpdateOne) Where(ps ...predicate.User) *UserUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated User entity.
func (_u *UserUpdateOne) Save(ctx context.Context) (*User, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *UserUpdateOne) SaveX(ctx context.Context) *User {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *UserUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *UserUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *UserUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := user.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserUpdateOne) check() error {
	if v, ok := _u.mutation.Email(); ok {
		if err := user.EmailValidator(v); err != nil {
			return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PasswordHash(); ok {
		if err := user.PasswordHashValidator(v); err != nil {
			return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)}
		}
	}
	return nil
}

func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "User.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
		for _, f := range fields {
			if !user.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != user.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Email(); ok {
		_spec.SetField(user.FieldEmail, field.TypeString, value)
	}
	if value, ok := _u.mutation.Username(); ok {
		_spec.SetField(user.FieldUsername, field.TypeString, value)
	}
	if _u.mutation.UsernameCleared() {
		_spec.ClearField(user.FieldUsername, field.TypeString)
	}
	if value, ok := _u.mutation.FirstName(); ok {
		_spec.SetField(user.FieldFirstName, field.TypeString, value)
	}
	if _u.mutation.FirstNameCleared() {
		_spec.ClearField(user.FieldFirstName, field.TypeString)
	}
	if value, ok := _u.mutation.LastName(); ok {
		_spec.SetField(user.FieldLastName, field.TypeString, value)
	}
	if _u.mutation.LastNameCleared() {
		_spec.ClearField(user.FieldLastName, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordHash(); ok {
		_spec.SetField(user.FieldPasswordHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.Verified(); ok {
		_spec.SetField(user.FieldVerified, field.TypeBool, value)
	}
	if value, ok := _u.mutation.EmailVerificationToken(); ok {
		_spec.SetField(user.FieldEmailVerificationToken, field.TypeString, value)
	}
	if _u.mutation.EmailVerificationTokenCleared() {
		_spec.ClearField(user.FieldEmailVerificationToken, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordResetToken(); ok {
		_spec.SetField(user.FieldPasswordResetToken, field.TypeString, value)
	}
	if _u.mutation.PasswordResetTokenCleared() {
		_spec.ClearField(user.FieldPasswordResetToken, field.TypeString)
	}
	if value, ok := _u.mutation.PasswordResetExpiresAt(); ok {
		_spec.SetField(user.FieldPasswordResetExpiresAt, field.TypeTime, value)
	}
	if _u.mutation.PasswordResetExpiresAtCleared() {
		_spec.ClearField(user.FieldPasswordResetExpiresAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.UserRolesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUserRolesIDs(); len(nodes) > 0 && !_u.mutation.UserRolesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserRolesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   user.UserRolesTable,
			Columns: []string{user.UserRolesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(userrole.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &User{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{user.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
