// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/predicate"
	"PasswordManager/ent/user"
	"PasswordManager/ent/webauthnregisterchallenge"
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// WebAuthnRegisterChallengeUpdate is the builder for updating WebAuthnRegisterChallenge entities.
type WebAuthnRegisterChallengeUpdate struct {
	config
	hooks    []Hook
	mutation *WebAuthnRegisterChallengeMutation
}

// Where appends a list predicates to the WebAuthnRegisterChallengeUpdate builder.
func (warcu *WebAuthnRegisterChallengeUpdate) Where(ps ...predicate.WebAuthnRegisterChallenge) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.Where(ps...)
	return warcu
}

// SetSdChallenge sets the "sdChallenge" field.
func (warcu *WebAuthnRegisterChallengeUpdate) SetSdChallenge(s string) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetSdChallenge(s)
	return warcu
}

// SetNillableSdChallenge sets the "sdChallenge" field if the given value is not nil.
func (warcu *WebAuthnRegisterChallengeUpdate) SetNillableSdChallenge(s *string) *WebAuthnRegisterChallengeUpdate {
	if s != nil {
		warcu.SetSdChallenge(*s)
	}
	return warcu
}

// ClearSdChallenge clears the value of the "sdChallenge" field.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearSdChallenge() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearSdChallenge()
	return warcu
}

// SetUserId sets the "userId" field.
func (warcu *WebAuthnRegisterChallengeUpdate) SetUserId(b []byte) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetUserId(b)
	return warcu
}

// ClearUserId clears the value of the "userId" field.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearUserId() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearUserId()
	return warcu
}

// SetAllowedCredentialIds sets the "allowedCredentialIds" field.
func (warcu *WebAuthnRegisterChallengeUpdate) SetAllowedCredentialIds(u [][]uint8) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetAllowedCredentialIds(u)
	return warcu
}

// AppendAllowedCredentialIds appends u to the "allowedCredentialIds" field.
func (warcu *WebAuthnRegisterChallengeUpdate) AppendAllowedCredentialIds(u [][]uint8) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.AppendAllowedCredentialIds(u)
	return warcu
}

// ClearAllowedCredentialIds clears the value of the "allowedCredentialIds" field.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearAllowedCredentialIds() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearAllowedCredentialIds()
	return warcu
}

// SetUserVerification sets the "userVerification" field.
func (warcu *WebAuthnRegisterChallengeUpdate) SetUserVerification(s string) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetUserVerification(s)
	return warcu
}

// SetNillableUserVerification sets the "userVerification" field if the given value is not nil.
func (warcu *WebAuthnRegisterChallengeUpdate) SetNillableUserVerification(s *string) *WebAuthnRegisterChallengeUpdate {
	if s != nil {
		warcu.SetUserVerification(*s)
	}
	return warcu
}

// ClearUserVerification clears the value of the "userVerification" field.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearUserVerification() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearUserVerification()
	return warcu
}

// SetExtensions sets the "extensions" field.
func (warcu *WebAuthnRegisterChallengeUpdate) SetExtensions(m map[string]interface{}) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetExtensions(m)
	return warcu
}

// ClearExtensions clears the value of the "extensions" field.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearExtensions() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearExtensions()
	return warcu
}

// SetUserID sets the "user" edge to the User entity by ID.
func (warcu *WebAuthnRegisterChallengeUpdate) SetUserID(id uuid.UUID) *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.SetUserID(id)
	return warcu
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (warcu *WebAuthnRegisterChallengeUpdate) SetNillableUserID(id *uuid.UUID) *WebAuthnRegisterChallengeUpdate {
	if id != nil {
		warcu = warcu.SetUserID(*id)
	}
	return warcu
}

// SetUser sets the "user" edge to the User entity.
func (warcu *WebAuthnRegisterChallengeUpdate) SetUser(u *User) *WebAuthnRegisterChallengeUpdate {
	return warcu.SetUserID(u.ID)
}

// Mutation returns the WebAuthnRegisterChallengeMutation object of the builder.
func (warcu *WebAuthnRegisterChallengeUpdate) Mutation() *WebAuthnRegisterChallengeMutation {
	return warcu.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (warcu *WebAuthnRegisterChallengeUpdate) ClearUser() *WebAuthnRegisterChallengeUpdate {
	warcu.mutation.ClearUser()
	return warcu
}

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

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

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

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

func (warcu *WebAuthnRegisterChallengeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(webauthnregisterchallenge.Table, webauthnregisterchallenge.Columns, sqlgraph.NewFieldSpec(webauthnregisterchallenge.FieldID, field.TypeUUID))
	if ps := warcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := warcu.mutation.SdChallenge(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldSdChallenge, field.TypeString, value)
	}
	if warcu.mutation.SdChallengeCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldSdChallenge, field.TypeString)
	}
	if value, ok := warcu.mutation.UserId(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldUserId, field.TypeBytes, value)
	}
	if warcu.mutation.UserIdCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldUserId, field.TypeBytes)
	}
	if value, ok := warcu.mutation.AllowedCredentialIds(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldAllowedCredentialIds, field.TypeJSON, value)
	}
	if value, ok := warcu.mutation.AppendedAllowedCredentialIds(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, webauthnregisterchallenge.FieldAllowedCredentialIds, value)
		})
	}
	if warcu.mutation.AllowedCredentialIdsCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldAllowedCredentialIds, field.TypeJSON)
	}
	if value, ok := warcu.mutation.UserVerification(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldUserVerification, field.TypeString, value)
	}
	if warcu.mutation.UserVerificationCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldUserVerification, field.TypeString)
	}
	if value, ok := warcu.mutation.Extensions(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldExtensions, field.TypeJSON, value)
	}
	if warcu.mutation.ExtensionsCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldExtensions, field.TypeJSON)
	}
	if warcu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthnregisterchallenge.UserTable,
			Columns: []string{webauthnregisterchallenge.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := warcu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthnregisterchallenge.UserTable,
			Columns: []string{webauthnregisterchallenge.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, warcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{webauthnregisterchallenge.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	warcu.mutation.done = true
	return n, nil
}

// WebAuthnRegisterChallengeUpdateOne is the builder for updating a single WebAuthnRegisterChallenge entity.
type WebAuthnRegisterChallengeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WebAuthnRegisterChallengeMutation
}

// SetSdChallenge sets the "sdChallenge" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetSdChallenge(s string) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetSdChallenge(s)
	return warcuo
}

// SetNillableSdChallenge sets the "sdChallenge" field if the given value is not nil.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetNillableSdChallenge(s *string) *WebAuthnRegisterChallengeUpdateOne {
	if s != nil {
		warcuo.SetSdChallenge(*s)
	}
	return warcuo
}

// ClearSdChallenge clears the value of the "sdChallenge" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearSdChallenge() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearSdChallenge()
	return warcuo
}

// SetUserId sets the "userId" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetUserId(b []byte) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetUserId(b)
	return warcuo
}

// ClearUserId clears the value of the "userId" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearUserId() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearUserId()
	return warcuo
}

// SetAllowedCredentialIds sets the "allowedCredentialIds" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetAllowedCredentialIds(u [][]uint8) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetAllowedCredentialIds(u)
	return warcuo
}

// AppendAllowedCredentialIds appends u to the "allowedCredentialIds" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) AppendAllowedCredentialIds(u [][]uint8) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.AppendAllowedCredentialIds(u)
	return warcuo
}

// ClearAllowedCredentialIds clears the value of the "allowedCredentialIds" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearAllowedCredentialIds() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearAllowedCredentialIds()
	return warcuo
}

// SetUserVerification sets the "userVerification" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetUserVerification(s string) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetUserVerification(s)
	return warcuo
}

// SetNillableUserVerification sets the "userVerification" field if the given value is not nil.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetNillableUserVerification(s *string) *WebAuthnRegisterChallengeUpdateOne {
	if s != nil {
		warcuo.SetUserVerification(*s)
	}
	return warcuo
}

// ClearUserVerification clears the value of the "userVerification" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearUserVerification() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearUserVerification()
	return warcuo
}

// SetExtensions sets the "extensions" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetExtensions(m map[string]interface{}) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetExtensions(m)
	return warcuo
}

// ClearExtensions clears the value of the "extensions" field.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearExtensions() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearExtensions()
	return warcuo
}

// SetUserID sets the "user" edge to the User entity by ID.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetUserID(id uuid.UUID) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.SetUserID(id)
	return warcuo
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetNillableUserID(id *uuid.UUID) *WebAuthnRegisterChallengeUpdateOne {
	if id != nil {
		warcuo = warcuo.SetUserID(*id)
	}
	return warcuo
}

// SetUser sets the "user" edge to the User entity.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) SetUser(u *User) *WebAuthnRegisterChallengeUpdateOne {
	return warcuo.SetUserID(u.ID)
}

// Mutation returns the WebAuthnRegisterChallengeMutation object of the builder.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) Mutation() *WebAuthnRegisterChallengeMutation {
	return warcuo.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) ClearUser() *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.ClearUser()
	return warcuo
}

// Where appends a list predicates to the WebAuthnRegisterChallengeUpdate builder.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) Where(ps ...predicate.WebAuthnRegisterChallenge) *WebAuthnRegisterChallengeUpdateOne {
	warcuo.mutation.Where(ps...)
	return warcuo
}

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

// Save executes the query and returns the updated WebAuthnRegisterChallenge entity.
func (warcuo *WebAuthnRegisterChallengeUpdateOne) Save(ctx context.Context) (*WebAuthnRegisterChallenge, error) {
	return withHooks[*WebAuthnRegisterChallenge, WebAuthnRegisterChallengeMutation](ctx, warcuo.sqlSave, warcuo.mutation, warcuo.hooks)
}

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

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

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

func (warcuo *WebAuthnRegisterChallengeUpdateOne) sqlSave(ctx context.Context) (_node *WebAuthnRegisterChallenge, err error) {
	_spec := sqlgraph.NewUpdateSpec(webauthnregisterchallenge.Table, webauthnregisterchallenge.Columns, sqlgraph.NewFieldSpec(webauthnregisterchallenge.FieldID, field.TypeUUID))
	id, ok := warcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WebAuthnRegisterChallenge.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := warcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, webauthnregisterchallenge.FieldID)
		for _, f := range fields {
			if !webauthnregisterchallenge.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != webauthnregisterchallenge.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := warcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := warcuo.mutation.SdChallenge(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldSdChallenge, field.TypeString, value)
	}
	if warcuo.mutation.SdChallengeCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldSdChallenge, field.TypeString)
	}
	if value, ok := warcuo.mutation.UserId(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldUserId, field.TypeBytes, value)
	}
	if warcuo.mutation.UserIdCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldUserId, field.TypeBytes)
	}
	if value, ok := warcuo.mutation.AllowedCredentialIds(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldAllowedCredentialIds, field.TypeJSON, value)
	}
	if value, ok := warcuo.mutation.AppendedAllowedCredentialIds(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, webauthnregisterchallenge.FieldAllowedCredentialIds, value)
		})
	}
	if warcuo.mutation.AllowedCredentialIdsCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldAllowedCredentialIds, field.TypeJSON)
	}
	if value, ok := warcuo.mutation.UserVerification(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldUserVerification, field.TypeString, value)
	}
	if warcuo.mutation.UserVerificationCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldUserVerification, field.TypeString)
	}
	if value, ok := warcuo.mutation.Extensions(); ok {
		_spec.SetField(webauthnregisterchallenge.FieldExtensions, field.TypeJSON, value)
	}
	if warcuo.mutation.ExtensionsCleared() {
		_spec.ClearField(webauthnregisterchallenge.FieldExtensions, field.TypeJSON)
	}
	if warcuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthnregisterchallenge.UserTable,
			Columns: []string{webauthnregisterchallenge.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := warcuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthnregisterchallenge.UserTable,
			Columns: []string{webauthnregisterchallenge.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &WebAuthnRegisterChallenge{config: warcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, warcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{webauthnregisterchallenge.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	warcuo.mutation.done = true
	return _node, nil
}
