// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/predicate"
	"PasswordManager/ent/webauthnchallenge"
	"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"
)

// WebAuthnChallengeUpdate is the builder for updating WebAuthnChallenge entities.
type WebAuthnChallengeUpdate struct {
	config
	hooks    []Hook
	mutation *WebAuthnChallengeMutation
}

// Where appends a list predicates to the WebAuthnChallengeUpdate builder.
func (wacu *WebAuthnChallengeUpdate) Where(ps ...predicate.WebAuthnChallenge) *WebAuthnChallengeUpdate {
	wacu.mutation.Where(ps...)
	return wacu
}

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

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

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

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

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

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

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

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

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

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

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

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

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

// SetChallengeID sets the "challenge" edge to the Challenge entity by ID.
func (wacu *WebAuthnChallengeUpdate) SetChallengeID(id uuid.UUID) *WebAuthnChallengeUpdate {
	wacu.mutation.SetChallengeID(id)
	return wacu
}

// SetNillableChallengeID sets the "challenge" edge to the Challenge entity by ID if the given value is not nil.
func (wacu *WebAuthnChallengeUpdate) SetNillableChallengeID(id *uuid.UUID) *WebAuthnChallengeUpdate {
	if id != nil {
		wacu = wacu.SetChallengeID(*id)
	}
	return wacu
}

// SetChallenge sets the "challenge" edge to the Challenge entity.
func (wacu *WebAuthnChallengeUpdate) SetChallenge(c *Challenge) *WebAuthnChallengeUpdate {
	return wacu.SetChallengeID(c.ID)
}

// Mutation returns the WebAuthnChallengeMutation object of the builder.
func (wacu *WebAuthnChallengeUpdate) Mutation() *WebAuthnChallengeMutation {
	return wacu.mutation
}

// ClearChallenge clears the "challenge" edge to the Challenge entity.
func (wacu *WebAuthnChallengeUpdate) ClearChallenge() *WebAuthnChallengeUpdate {
	wacu.mutation.ClearChallenge()
	return wacu
}

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

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

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

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

func (wacu *WebAuthnChallengeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(webauthnchallenge.Table, webauthnchallenge.Columns, sqlgraph.NewFieldSpec(webauthnchallenge.FieldID, field.TypeUUID))
	if ps := wacu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wacu.mutation.SdChallenge(); ok {
		_spec.SetField(webauthnchallenge.FieldSdChallenge, field.TypeString, value)
	}
	if wacu.mutation.SdChallengeCleared() {
		_spec.ClearField(webauthnchallenge.FieldSdChallenge, field.TypeString)
	}
	if value, ok := wacu.mutation.UserId(); ok {
		_spec.SetField(webauthnchallenge.FieldUserId, field.TypeBytes, value)
	}
	if wacu.mutation.UserIdCleared() {
		_spec.ClearField(webauthnchallenge.FieldUserId, field.TypeBytes)
	}
	if value, ok := wacu.mutation.AllowedCredentialIds(); ok {
		_spec.SetField(webauthnchallenge.FieldAllowedCredentialIds, field.TypeJSON, value)
	}
	if value, ok := wacu.mutation.AppendedAllowedCredentialIds(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, webauthnchallenge.FieldAllowedCredentialIds, value)
		})
	}
	if wacu.mutation.AllowedCredentialIdsCleared() {
		_spec.ClearField(webauthnchallenge.FieldAllowedCredentialIds, field.TypeJSON)
	}
	if value, ok := wacu.mutation.UserVerification(); ok {
		_spec.SetField(webauthnchallenge.FieldUserVerification, field.TypeString, value)
	}
	if wacu.mutation.UserVerificationCleared() {
		_spec.ClearField(webauthnchallenge.FieldUserVerification, field.TypeString)
	}
	if value, ok := wacu.mutation.Extensions(); ok {
		_spec.SetField(webauthnchallenge.FieldExtensions, field.TypeJSON, value)
	}
	if wacu.mutation.ExtensionsCleared() {
		_spec.ClearField(webauthnchallenge.FieldExtensions, field.TypeJSON)
	}
	if wacu.mutation.ChallengeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   webauthnchallenge.ChallengeTable,
			Columns: []string{webauthnchallenge.ChallengeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(challenge.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wacu.mutation.ChallengeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   webauthnchallenge.ChallengeTable,
			Columns: []string{webauthnchallenge.ChallengeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(challenge.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, wacu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{webauthnchallenge.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wacu.mutation.done = true
	return n, nil
}

// WebAuthnChallengeUpdateOne is the builder for updating a single WebAuthnChallenge entity.
type WebAuthnChallengeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WebAuthnChallengeMutation
}

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

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

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

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

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

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

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

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

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

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

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

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

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

// SetChallengeID sets the "challenge" edge to the Challenge entity by ID.
func (wacuo *WebAuthnChallengeUpdateOne) SetChallengeID(id uuid.UUID) *WebAuthnChallengeUpdateOne {
	wacuo.mutation.SetChallengeID(id)
	return wacuo
}

// SetNillableChallengeID sets the "challenge" edge to the Challenge entity by ID if the given value is not nil.
func (wacuo *WebAuthnChallengeUpdateOne) SetNillableChallengeID(id *uuid.UUID) *WebAuthnChallengeUpdateOne {
	if id != nil {
		wacuo = wacuo.SetChallengeID(*id)
	}
	return wacuo
}

// SetChallenge sets the "challenge" edge to the Challenge entity.
func (wacuo *WebAuthnChallengeUpdateOne) SetChallenge(c *Challenge) *WebAuthnChallengeUpdateOne {
	return wacuo.SetChallengeID(c.ID)
}

// Mutation returns the WebAuthnChallengeMutation object of the builder.
func (wacuo *WebAuthnChallengeUpdateOne) Mutation() *WebAuthnChallengeMutation {
	return wacuo.mutation
}

// ClearChallenge clears the "challenge" edge to the Challenge entity.
func (wacuo *WebAuthnChallengeUpdateOne) ClearChallenge() *WebAuthnChallengeUpdateOne {
	wacuo.mutation.ClearChallenge()
	return wacuo
}

// Where appends a list predicates to the WebAuthnChallengeUpdate builder.
func (wacuo *WebAuthnChallengeUpdateOne) Where(ps ...predicate.WebAuthnChallenge) *WebAuthnChallengeUpdateOne {
	wacuo.mutation.Where(ps...)
	return wacuo
}

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

// Save executes the query and returns the updated WebAuthnChallenge entity.
func (wacuo *WebAuthnChallengeUpdateOne) Save(ctx context.Context) (*WebAuthnChallenge, error) {
	return withHooks[*WebAuthnChallenge, WebAuthnChallengeMutation](ctx, wacuo.sqlSave, wacuo.mutation, wacuo.hooks)
}

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

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

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

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