// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/user_verifications/rpc/internal/models/predicate"
	"juwan-backend/app/user_verifications/rpc/internal/models/schema"
	"juwan-backend/app/user_verifications/rpc/internal/models/userverifications"
	"time"

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

// UserVerificationsUpdate is the builder for updating UserVerifications entities.
type UserVerificationsUpdate struct {
	config
	hooks    []Hook
	mutation *UserVerificationsMutation
}

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

// SetRole sets the "role" field.
func (_u *UserVerificationsUpdate) SetRole(v string) *UserVerificationsUpdate {
	_u.mutation.SetRole(v)
	return _u
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableRole(v *string) *UserVerificationsUpdate {
	if v != nil {
		_u.SetRole(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *UserVerificationsUpdate) SetStatus(v string) *UserVerificationsUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableStatus(v *string) *UserVerificationsUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetMaterials sets the "materials" field.
func (_u *UserVerificationsUpdate) SetMaterials(v schema.MaterialStruct) *UserVerificationsUpdate {
	_u.mutation.SetMaterials(v)
	return _u
}

// SetNillableMaterials sets the "materials" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableMaterials(v *schema.MaterialStruct) *UserVerificationsUpdate {
	if v != nil {
		_u.SetMaterials(*v)
	}
	return _u
}

// SetRejectReason sets the "reject_reason" field.
func (_u *UserVerificationsUpdate) SetRejectReason(v string) *UserVerificationsUpdate {
	_u.mutation.SetRejectReason(v)
	return _u
}

// SetNillableRejectReason sets the "reject_reason" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableRejectReason(v *string) *UserVerificationsUpdate {
	if v != nil {
		_u.SetRejectReason(*v)
	}
	return _u
}

// ClearRejectReason clears the value of the "reject_reason" field.
func (_u *UserVerificationsUpdate) ClearRejectReason() *UserVerificationsUpdate {
	_u.mutation.ClearRejectReason()
	return _u
}

// SetReviewedBy sets the "reviewed_by" field.
func (_u *UserVerificationsUpdate) SetReviewedBy(v int64) *UserVerificationsUpdate {
	_u.mutation.ResetReviewedBy()
	_u.mutation.SetReviewedBy(v)
	return _u
}

// SetNillableReviewedBy sets the "reviewed_by" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableReviewedBy(v *int64) *UserVerificationsUpdate {
	if v != nil {
		_u.SetReviewedBy(*v)
	}
	return _u
}

// AddReviewedBy adds value to the "reviewed_by" field.
func (_u *UserVerificationsUpdate) AddReviewedBy(v int64) *UserVerificationsUpdate {
	_u.mutation.AddReviewedBy(v)
	return _u
}

// ClearReviewedBy clears the value of the "reviewed_by" field.
func (_u *UserVerificationsUpdate) ClearReviewedBy() *UserVerificationsUpdate {
	_u.mutation.ClearReviewedBy()
	return _u
}

// SetReviewedAt sets the "reviewed_at" field.
func (_u *UserVerificationsUpdate) SetReviewedAt(v time.Time) *UserVerificationsUpdate {
	_u.mutation.SetReviewedAt(v)
	return _u
}

// SetNillableReviewedAt sets the "reviewed_at" field if the given value is not nil.
func (_u *UserVerificationsUpdate) SetNillableReviewedAt(v *time.Time) *UserVerificationsUpdate {
	if v != nil {
		_u.SetReviewedAt(*v)
	}
	return _u
}

// ClearReviewedAt clears the value of the "reviewed_at" field.
func (_u *UserVerificationsUpdate) ClearReviewedAt() *UserVerificationsUpdate {
	_u.mutation.ClearReviewedAt()
	return _u
}

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

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

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

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

func (_u *UserVerificationsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(userverifications.Table, userverifications.Columns, sqlgraph.NewFieldSpec(userverifications.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.Role(); ok {
		_spec.SetField(userverifications.FieldRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(userverifications.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Materials(); ok {
		_spec.SetField(userverifications.FieldMaterials, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.RejectReason(); ok {
		_spec.SetField(userverifications.FieldRejectReason, field.TypeString, value)
	}
	if _u.mutation.RejectReasonCleared() {
		_spec.ClearField(userverifications.FieldRejectReason, field.TypeString)
	}
	if value, ok := _u.mutation.ReviewedBy(); ok {
		_spec.SetField(userverifications.FieldReviewedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedReviewedBy(); ok {
		_spec.AddField(userverifications.FieldReviewedBy, field.TypeInt64, value)
	}
	if _u.mutation.ReviewedByCleared() {
		_spec.ClearField(userverifications.FieldReviewedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.ReviewedAt(); ok {
		_spec.SetField(userverifications.FieldReviewedAt, field.TypeTime, value)
	}
	if _u.mutation.ReviewedAtCleared() {
		_spec.ClearField(userverifications.FieldReviewedAt, field.TypeTime)
	}
	if _u.mutation.CreatedAtCleared() {
		_spec.ClearField(userverifications.FieldCreatedAt, field.TypeTime)
	}
	if _u.mutation.UpdatedAtCleared() {
		_spec.ClearField(userverifications.FieldUpdatedAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{userverifications.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserVerificationsUpdateOne is the builder for updating a single UserVerifications entity.
type UserVerificationsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserVerificationsMutation
}

// SetRole sets the "role" field.
func (_u *UserVerificationsUpdateOne) SetRole(v string) *UserVerificationsUpdateOne {
	_u.mutation.SetRole(v)
	return _u
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableRole(v *string) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetRole(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *UserVerificationsUpdateOne) SetStatus(v string) *UserVerificationsUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableStatus(v *string) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetMaterials sets the "materials" field.
func (_u *UserVerificationsUpdateOne) SetMaterials(v schema.MaterialStruct) *UserVerificationsUpdateOne {
	_u.mutation.SetMaterials(v)
	return _u
}

// SetNillableMaterials sets the "materials" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableMaterials(v *schema.MaterialStruct) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetMaterials(*v)
	}
	return _u
}

// SetRejectReason sets the "reject_reason" field.
func (_u *UserVerificationsUpdateOne) SetRejectReason(v string) *UserVerificationsUpdateOne {
	_u.mutation.SetRejectReason(v)
	return _u
}

// SetNillableRejectReason sets the "reject_reason" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableRejectReason(v *string) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetRejectReason(*v)
	}
	return _u
}

// ClearRejectReason clears the value of the "reject_reason" field.
func (_u *UserVerificationsUpdateOne) ClearRejectReason() *UserVerificationsUpdateOne {
	_u.mutation.ClearRejectReason()
	return _u
}

// SetReviewedBy sets the "reviewed_by" field.
func (_u *UserVerificationsUpdateOne) SetReviewedBy(v int64) *UserVerificationsUpdateOne {
	_u.mutation.ResetReviewedBy()
	_u.mutation.SetReviewedBy(v)
	return _u
}

// SetNillableReviewedBy sets the "reviewed_by" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableReviewedBy(v *int64) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetReviewedBy(*v)
	}
	return _u
}

// AddReviewedBy adds value to the "reviewed_by" field.
func (_u *UserVerificationsUpdateOne) AddReviewedBy(v int64) *UserVerificationsUpdateOne {
	_u.mutation.AddReviewedBy(v)
	return _u
}

// ClearReviewedBy clears the value of the "reviewed_by" field.
func (_u *UserVerificationsUpdateOne) ClearReviewedBy() *UserVerificationsUpdateOne {
	_u.mutation.ClearReviewedBy()
	return _u
}

// SetReviewedAt sets the "reviewed_at" field.
func (_u *UserVerificationsUpdateOne) SetReviewedAt(v time.Time) *UserVerificationsUpdateOne {
	_u.mutation.SetReviewedAt(v)
	return _u
}

// SetNillableReviewedAt sets the "reviewed_at" field if the given value is not nil.
func (_u *UserVerificationsUpdateOne) SetNillableReviewedAt(v *time.Time) *UserVerificationsUpdateOne {
	if v != nil {
		_u.SetReviewedAt(*v)
	}
	return _u
}

// ClearReviewedAt clears the value of the "reviewed_at" field.
func (_u *UserVerificationsUpdateOne) ClearReviewedAt() *UserVerificationsUpdateOne {
	_u.mutation.ClearReviewedAt()
	return _u
}

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

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

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

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

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

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

func (_u *UserVerificationsUpdateOne) sqlSave(ctx context.Context) (_node *UserVerifications, err error) {
	_spec := sqlgraph.NewUpdateSpec(userverifications.Table, userverifications.Columns, sqlgraph.NewFieldSpec(userverifications.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "UserVerifications.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, userverifications.FieldID)
		for _, f := range fields {
			if !userverifications.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != userverifications.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.Role(); ok {
		_spec.SetField(userverifications.FieldRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(userverifications.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Materials(); ok {
		_spec.SetField(userverifications.FieldMaterials, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.RejectReason(); ok {
		_spec.SetField(userverifications.FieldRejectReason, field.TypeString, value)
	}
	if _u.mutation.RejectReasonCleared() {
		_spec.ClearField(userverifications.FieldRejectReason, field.TypeString)
	}
	if value, ok := _u.mutation.ReviewedBy(); ok {
		_spec.SetField(userverifications.FieldReviewedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedReviewedBy(); ok {
		_spec.AddField(userverifications.FieldReviewedBy, field.TypeInt64, value)
	}
	if _u.mutation.ReviewedByCleared() {
		_spec.ClearField(userverifications.FieldReviewedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.ReviewedAt(); ok {
		_spec.SetField(userverifications.FieldReviewedAt, field.TypeTime, value)
	}
	if _u.mutation.ReviewedAtCleared() {
		_spec.ClearField(userverifications.FieldReviewedAt, field.TypeTime)
	}
	if _u.mutation.CreatedAtCleared() {
		_spec.ClearField(userverifications.FieldCreatedAt, field.TypeTime)
	}
	if _u.mutation.UpdatedAtCleared() {
		_spec.ClearField(userverifications.FieldUpdatedAt, field.TypeTime)
	}
	_node = &UserVerifications{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{userverifications.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
