// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/users/rpc/internal/models/predicate"
	"juwan-backend/app/users/rpc/internal/models/userpreferences"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// UserPreferencesUpdate is the builder for updating UserPreferences entities.
type UserPreferencesUpdate struct {
	config
	hooks    []Hook
	mutation *UserPreferencesMutation
}

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

// SetUserID sets the "user_id" field.
func (_u *UserPreferencesUpdate) SetUserID(v int64) *UserPreferencesUpdate {
	_u.mutation.ResetUserID()
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableUserID(v *int64) *UserPreferencesUpdate {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// AddUserID adds value to the "user_id" field.
func (_u *UserPreferencesUpdate) AddUserID(v int64) *UserPreferencesUpdate {
	_u.mutation.AddUserID(v)
	return _u
}

// SetNotificationOrder sets the "notification_order" field.
func (_u *UserPreferencesUpdate) SetNotificationOrder(v bool) *UserPreferencesUpdate {
	_u.mutation.SetNotificationOrder(v)
	return _u
}

// SetNillableNotificationOrder sets the "notification_order" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableNotificationOrder(v *bool) *UserPreferencesUpdate {
	if v != nil {
		_u.SetNotificationOrder(*v)
	}
	return _u
}

// SetNotificationCommunity sets the "notification_community" field.
func (_u *UserPreferencesUpdate) SetNotificationCommunity(v bool) *UserPreferencesUpdate {
	_u.mutation.SetNotificationCommunity(v)
	return _u
}

// SetNillableNotificationCommunity sets the "notification_community" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableNotificationCommunity(v *bool) *UserPreferencesUpdate {
	if v != nil {
		_u.SetNotificationCommunity(*v)
	}
	return _u
}

// SetNotificationSystem sets the "notification_system" field.
func (_u *UserPreferencesUpdate) SetNotificationSystem(v bool) *UserPreferencesUpdate {
	_u.mutation.SetNotificationSystem(v)
	return _u
}

// SetNillableNotificationSystem sets the "notification_system" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableNotificationSystem(v *bool) *UserPreferencesUpdate {
	if v != nil {
		_u.SetNotificationSystem(*v)
	}
	return _u
}

// SetTheme sets the "theme" field.
func (_u *UserPreferencesUpdate) SetTheme(v string) *UserPreferencesUpdate {
	_u.mutation.SetTheme(v)
	return _u
}

// SetNillableTheme sets the "theme" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableTheme(v *string) *UserPreferencesUpdate {
	if v != nil {
		_u.SetTheme(*v)
	}
	return _u
}

// SetLanguage sets the "language" field.
func (_u *UserPreferencesUpdate) SetLanguage(v string) *UserPreferencesUpdate {
	_u.mutation.SetLanguage(v)
	return _u
}

// SetNillableLanguage sets the "language" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableLanguage(v *string) *UserPreferencesUpdate {
	if v != nil {
		_u.SetLanguage(*v)
	}
	return _u
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_u *UserPreferencesUpdate) SetNillableUpdatedAt(v *time.Time) *UserPreferencesUpdate {
	if v != nil {
		_u.SetUpdatedAt(*v)
	}
	return _u
}

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

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

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

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

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

func (_u *UserPreferencesUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(userpreferences.Table, userpreferences.Columns, sqlgraph.NewFieldSpec(userpreferences.FieldID, field.TypeInt))
	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.UserID(); ok {
		_spec.SetField(userpreferences.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(userpreferences.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.NotificationOrder(); ok {
		_spec.SetField(userpreferences.FieldNotificationOrder, field.TypeBool, value)
	}
	if value, ok := _u.mutation.NotificationCommunity(); ok {
		_spec.SetField(userpreferences.FieldNotificationCommunity, field.TypeBool, value)
	}
	if value, ok := _u.mutation.NotificationSystem(); ok {
		_spec.SetField(userpreferences.FieldNotificationSystem, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Theme(); ok {
		_spec.SetField(userpreferences.FieldTheme, field.TypeString, value)
	}
	if value, ok := _u.mutation.Language(); ok {
		_spec.SetField(userpreferences.FieldLanguage, field.TypeString, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(userpreferences.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{userpreferences.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserPreferencesUpdateOne is the builder for updating a single UserPreferences entity.
type UserPreferencesUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserPreferencesMutation
}

// SetUserID sets the "user_id" field.
func (_u *UserPreferencesUpdateOne) SetUserID(v int64) *UserPreferencesUpdateOne {
	_u.mutation.ResetUserID()
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableUserID(v *int64) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// AddUserID adds value to the "user_id" field.
func (_u *UserPreferencesUpdateOne) AddUserID(v int64) *UserPreferencesUpdateOne {
	_u.mutation.AddUserID(v)
	return _u
}

// SetNotificationOrder sets the "notification_order" field.
func (_u *UserPreferencesUpdateOne) SetNotificationOrder(v bool) *UserPreferencesUpdateOne {
	_u.mutation.SetNotificationOrder(v)
	return _u
}

// SetNillableNotificationOrder sets the "notification_order" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableNotificationOrder(v *bool) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetNotificationOrder(*v)
	}
	return _u
}

// SetNotificationCommunity sets the "notification_community" field.
func (_u *UserPreferencesUpdateOne) SetNotificationCommunity(v bool) *UserPreferencesUpdateOne {
	_u.mutation.SetNotificationCommunity(v)
	return _u
}

// SetNillableNotificationCommunity sets the "notification_community" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableNotificationCommunity(v *bool) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetNotificationCommunity(*v)
	}
	return _u
}

// SetNotificationSystem sets the "notification_system" field.
func (_u *UserPreferencesUpdateOne) SetNotificationSystem(v bool) *UserPreferencesUpdateOne {
	_u.mutation.SetNotificationSystem(v)
	return _u
}

// SetNillableNotificationSystem sets the "notification_system" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableNotificationSystem(v *bool) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetNotificationSystem(*v)
	}
	return _u
}

// SetTheme sets the "theme" field.
func (_u *UserPreferencesUpdateOne) SetTheme(v string) *UserPreferencesUpdateOne {
	_u.mutation.SetTheme(v)
	return _u
}

// SetNillableTheme sets the "theme" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableTheme(v *string) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetTheme(*v)
	}
	return _u
}

// SetLanguage sets the "language" field.
func (_u *UserPreferencesUpdateOne) SetLanguage(v string) *UserPreferencesUpdateOne {
	_u.mutation.SetLanguage(v)
	return _u
}

// SetNillableLanguage sets the "language" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableLanguage(v *string) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetLanguage(*v)
	}
	return _u
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_u *UserPreferencesUpdateOne) SetNillableUpdatedAt(v *time.Time) *UserPreferencesUpdateOne {
	if v != nil {
		_u.SetUpdatedAt(*v)
	}
	return _u
}

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

// Where appends a list predicates to the UserPreferencesUpdate builder.
func (_u *UserPreferencesUpdateOne) Where(ps ...predicate.UserPreferences) *UserPreferencesUpdateOne {
	_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 *UserPreferencesUpdateOne) Select(field string, fields ...string) *UserPreferencesUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

func (_u *UserPreferencesUpdateOne) sqlSave(ctx context.Context) (_node *UserPreferences, err error) {
	_spec := sqlgraph.NewUpdateSpec(userpreferences.Table, userpreferences.Columns, sqlgraph.NewFieldSpec(userpreferences.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "UserPreferences.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, userpreferences.FieldID)
		for _, f := range fields {
			if !userpreferences.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != userpreferences.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.UserID(); ok {
		_spec.SetField(userpreferences.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(userpreferences.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.NotificationOrder(); ok {
		_spec.SetField(userpreferences.FieldNotificationOrder, field.TypeBool, value)
	}
	if value, ok := _u.mutation.NotificationCommunity(); ok {
		_spec.SetField(userpreferences.FieldNotificationCommunity, field.TypeBool, value)
	}
	if value, ok := _u.mutation.NotificationSystem(); ok {
		_spec.SetField(userpreferences.FieldNotificationSystem, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Theme(); ok {
		_spec.SetField(userpreferences.FieldTheme, field.TypeString, value)
	}
	if value, ok := _u.mutation.Language(); ok {
		_spec.SetField(userpreferences.FieldLanguage, field.TypeString, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(userpreferences.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &UserPreferences{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{userpreferences.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
