// Code generated by ent, DO NOT EDIT.

package models

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

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

// NotificationsUpdate is the builder for updating Notifications entities.
type NotificationsUpdate struct {
	config
	hooks    []Hook
	mutation *NotificationsMutation
}

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

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

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

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

// SetType sets the "type" field.
func (_u *NotificationsUpdate) SetType(v string) *NotificationsUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *NotificationsUpdate) SetNillableType(v *string) *NotificationsUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *NotificationsUpdate) SetTitle(v string) *NotificationsUpdate {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *NotificationsUpdate) SetNillableTitle(v *string) *NotificationsUpdate {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *NotificationsUpdate) SetContent(v string) *NotificationsUpdate {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *NotificationsUpdate) SetNillableContent(v *string) *NotificationsUpdate {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetRead sets the "read" field.
func (_u *NotificationsUpdate) SetRead(v bool) *NotificationsUpdate {
	_u.mutation.SetRead(v)
	return _u
}

// SetNillableRead sets the "read" field if the given value is not nil.
func (_u *NotificationsUpdate) SetNillableRead(v *bool) *NotificationsUpdate {
	if v != nil {
		_u.SetRead(*v)
	}
	return _u
}

// SetLink sets the "link" field.
func (_u *NotificationsUpdate) SetLink(v string) *NotificationsUpdate {
	_u.mutation.SetLink(v)
	return _u
}

// SetNillableLink sets the "link" field if the given value is not nil.
func (_u *NotificationsUpdate) SetNillableLink(v *string) *NotificationsUpdate {
	if v != nil {
		_u.SetLink(*v)
	}
	return _u
}

// ClearLink clears the value of the "link" field.
func (_u *NotificationsUpdate) ClearLink() *NotificationsUpdate {
	_u.mutation.ClearLink()
	return _u
}

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *NotificationsUpdate) 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 *NotificationsUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *NotificationsUpdate) 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 *NotificationsUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := notifications.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *NotificationsUpdate) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := notifications.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`models: validator failed for field "Notifications.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Title(); ok {
		if err := notifications.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Notifications.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Link(); ok {
		if err := notifications.LinkValidator(v); err != nil {
			return &ValidationError{Name: "link", err: fmt.Errorf(`models: validator failed for field "Notifications.link": %w`, err)}
		}
	}
	return nil
}

func (_u *NotificationsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(notifications.Table, notifications.Columns, sqlgraph.NewFieldSpec(notifications.FieldID, field.TypeInt64))
	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(notifications.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(notifications.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(notifications.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(notifications.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(notifications.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Read(); ok {
		_spec.SetField(notifications.FieldRead, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Link(); ok {
		_spec.SetField(notifications.FieldLink, field.TypeString, value)
	}
	if _u.mutation.LinkCleared() {
		_spec.ClearField(notifications.FieldLink, field.TypeString)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(notifications.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{notifications.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// NotificationsUpdateOne is the builder for updating a single Notifications entity.
type NotificationsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *NotificationsMutation
}

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

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

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

// SetType sets the "type" field.
func (_u *NotificationsUpdateOne) SetType(v string) *NotificationsUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *NotificationsUpdateOne) SetNillableType(v *string) *NotificationsUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *NotificationsUpdateOne) SetTitle(v string) *NotificationsUpdateOne {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *NotificationsUpdateOne) SetNillableTitle(v *string) *NotificationsUpdateOne {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *NotificationsUpdateOne) SetContent(v string) *NotificationsUpdateOne {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *NotificationsUpdateOne) SetNillableContent(v *string) *NotificationsUpdateOne {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetRead sets the "read" field.
func (_u *NotificationsUpdateOne) SetRead(v bool) *NotificationsUpdateOne {
	_u.mutation.SetRead(v)
	return _u
}

// SetNillableRead sets the "read" field if the given value is not nil.
func (_u *NotificationsUpdateOne) SetNillableRead(v *bool) *NotificationsUpdateOne {
	if v != nil {
		_u.SetRead(*v)
	}
	return _u
}

// SetLink sets the "link" field.
func (_u *NotificationsUpdateOne) SetLink(v string) *NotificationsUpdateOne {
	_u.mutation.SetLink(v)
	return _u
}

// SetNillableLink sets the "link" field if the given value is not nil.
func (_u *NotificationsUpdateOne) SetNillableLink(v *string) *NotificationsUpdateOne {
	if v != nil {
		_u.SetLink(*v)
	}
	return _u
}

// ClearLink clears the value of the "link" field.
func (_u *NotificationsUpdateOne) ClearLink() *NotificationsUpdateOne {
	_u.mutation.ClearLink()
	return _u
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *NotificationsUpdateOne) 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 *NotificationsUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := notifications.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *NotificationsUpdateOne) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := notifications.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`models: validator failed for field "Notifications.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Title(); ok {
		if err := notifications.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Notifications.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Link(); ok {
		if err := notifications.LinkValidator(v); err != nil {
			return &ValidationError{Name: "link", err: fmt.Errorf(`models: validator failed for field "Notifications.link": %w`, err)}
		}
	}
	return nil
}

func (_u *NotificationsUpdateOne) sqlSave(ctx context.Context) (_node *Notifications, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(notifications.Table, notifications.Columns, sqlgraph.NewFieldSpec(notifications.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Notifications.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, notifications.FieldID)
		for _, f := range fields {
			if !notifications.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != notifications.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(notifications.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(notifications.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(notifications.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(notifications.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(notifications.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Read(); ok {
		_spec.SetField(notifications.FieldRead, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Link(); ok {
		_spec.SetField(notifications.FieldLink, field.TypeString, value)
	}
	if _u.mutation.LinkCleared() {
		_spec.ClearField(notifications.FieldLink, field.TypeString)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(notifications.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &Notifications{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{notifications.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
