// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/predicate"
	"PasswordManager/ent/user"
	"PasswordManager/ent/webauthncredential"
	"context"
	"errors"
	"fmt"
	"time"

	"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"
)

// WebAuthnCredentialUpdate is the builder for updating WebAuthnCredential entities.
type WebAuthnCredentialUpdate struct {
	config
	hooks    []Hook
	mutation *WebAuthnCredentialMutation
}

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

// SetName sets the "name" field.
func (wacu *WebAuthnCredentialUpdate) SetName(s string) *WebAuthnCredentialUpdate {
	wacu.mutation.SetName(s)
	return wacu
}

// SetCreatedAt sets the "createdAt" field.
func (wacu *WebAuthnCredentialUpdate) SetCreatedAt(t time.Time) *WebAuthnCredentialUpdate {
	wacu.mutation.SetCreatedAt(t)
	return wacu
}

// SetNillableCreatedAt sets the "createdAt" field if the given value is not nil.
func (wacu *WebAuthnCredentialUpdate) SetNillableCreatedAt(t *time.Time) *WebAuthnCredentialUpdate {
	if t != nil {
		wacu.SetCreatedAt(*t)
	}
	return wacu
}

// SetCredentialId sets the "credentialId" field.
func (wacu *WebAuthnCredentialUpdate) SetCredentialId(b []byte) *WebAuthnCredentialUpdate {
	wacu.mutation.SetCredentialId(b)
	return wacu
}

// SetPublicKey sets the "publicKey" field.
func (wacu *WebAuthnCredentialUpdate) SetPublicKey(b []byte) *WebAuthnCredentialUpdate {
	wacu.mutation.SetPublicKey(b)
	return wacu
}

// SetAttestationType sets the "attestationType" field.
func (wacu *WebAuthnCredentialUpdate) SetAttestationType(s string) *WebAuthnCredentialUpdate {
	wacu.mutation.SetAttestationType(s)
	return wacu
}

// SetTransport sets the "transport" field.
func (wacu *WebAuthnCredentialUpdate) SetTransport(s []string) *WebAuthnCredentialUpdate {
	wacu.mutation.SetTransport(s)
	return wacu
}

// AppendTransport appends s to the "transport" field.
func (wacu *WebAuthnCredentialUpdate) AppendTransport(s []string) *WebAuthnCredentialUpdate {
	wacu.mutation.AppendTransport(s)
	return wacu
}

// SetAaguid sets the "aaguid" field.
func (wacu *WebAuthnCredentialUpdate) SetAaguid(b []byte) *WebAuthnCredentialUpdate {
	wacu.mutation.SetAaguid(b)
	return wacu
}

// SetSignCount sets the "signCount" field.
func (wacu *WebAuthnCredentialUpdate) SetSignCount(u uint32) *WebAuthnCredentialUpdate {
	wacu.mutation.ResetSignCount()
	wacu.mutation.SetSignCount(u)
	return wacu
}

// AddSignCount adds u to the "signCount" field.
func (wacu *WebAuthnCredentialUpdate) AddSignCount(u int32) *WebAuthnCredentialUpdate {
	wacu.mutation.AddSignCount(u)
	return wacu
}

// SetCloneWarning sets the "cloneWarning" field.
func (wacu *WebAuthnCredentialUpdate) SetCloneWarning(b bool) *WebAuthnCredentialUpdate {
	wacu.mutation.SetCloneWarning(b)
	return wacu
}

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (wacu *WebAuthnCredentialUpdate) check() error {
	if _, ok := wacu.mutation.UserID(); wacu.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "WebAuthnCredential.user"`)
	}
	return nil
}

func (wacu *WebAuthnCredentialUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := wacu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(webauthncredential.Table, webauthncredential.Columns, sqlgraph.NewFieldSpec(webauthncredential.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.Name(); ok {
		_spec.SetField(webauthncredential.FieldName, field.TypeString, value)
	}
	if value, ok := wacu.mutation.CreatedAt(); ok {
		_spec.SetField(webauthncredential.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := wacu.mutation.CredentialId(); ok {
		_spec.SetField(webauthncredential.FieldCredentialId, field.TypeBytes, value)
	}
	if value, ok := wacu.mutation.PublicKey(); ok {
		_spec.SetField(webauthncredential.FieldPublicKey, field.TypeBytes, value)
	}
	if value, ok := wacu.mutation.AttestationType(); ok {
		_spec.SetField(webauthncredential.FieldAttestationType, field.TypeString, value)
	}
	if value, ok := wacu.mutation.Transport(); ok {
		_spec.SetField(webauthncredential.FieldTransport, field.TypeJSON, value)
	}
	if value, ok := wacu.mutation.AppendedTransport(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, webauthncredential.FieldTransport, value)
		})
	}
	if value, ok := wacu.mutation.Aaguid(); ok {
		_spec.SetField(webauthncredential.FieldAaguid, field.TypeBytes, value)
	}
	if value, ok := wacu.mutation.SignCount(); ok {
		_spec.SetField(webauthncredential.FieldSignCount, field.TypeUint32, value)
	}
	if value, ok := wacu.mutation.AddedSignCount(); ok {
		_spec.AddField(webauthncredential.FieldSignCount, field.TypeUint32, value)
	}
	if value, ok := wacu.mutation.CloneWarning(); ok {
		_spec.SetField(webauthncredential.FieldCloneWarning, field.TypeBool, value)
	}
	if wacu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthncredential.UserTable,
			Columns: []string{webauthncredential.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wacu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthncredential.UserTable,
			Columns: []string{webauthncredential.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, wacu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{webauthncredential.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wacu.mutation.done = true
	return n, nil
}

// WebAuthnCredentialUpdateOne is the builder for updating a single WebAuthnCredential entity.
type WebAuthnCredentialUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WebAuthnCredentialMutation
}

// SetName sets the "name" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetName(s string) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetName(s)
	return wacuo
}

// SetCreatedAt sets the "createdAt" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetCreatedAt(t time.Time) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetCreatedAt(t)
	return wacuo
}

// SetNillableCreatedAt sets the "createdAt" field if the given value is not nil.
func (wacuo *WebAuthnCredentialUpdateOne) SetNillableCreatedAt(t *time.Time) *WebAuthnCredentialUpdateOne {
	if t != nil {
		wacuo.SetCreatedAt(*t)
	}
	return wacuo
}

// SetCredentialId sets the "credentialId" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetCredentialId(b []byte) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetCredentialId(b)
	return wacuo
}

// SetPublicKey sets the "publicKey" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetPublicKey(b []byte) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetPublicKey(b)
	return wacuo
}

// SetAttestationType sets the "attestationType" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetAttestationType(s string) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetAttestationType(s)
	return wacuo
}

// SetTransport sets the "transport" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetTransport(s []string) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetTransport(s)
	return wacuo
}

// AppendTransport appends s to the "transport" field.
func (wacuo *WebAuthnCredentialUpdateOne) AppendTransport(s []string) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.AppendTransport(s)
	return wacuo
}

// SetAaguid sets the "aaguid" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetAaguid(b []byte) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetAaguid(b)
	return wacuo
}

// SetSignCount sets the "signCount" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetSignCount(u uint32) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.ResetSignCount()
	wacuo.mutation.SetSignCount(u)
	return wacuo
}

// AddSignCount adds u to the "signCount" field.
func (wacuo *WebAuthnCredentialUpdateOne) AddSignCount(u int32) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.AddSignCount(u)
	return wacuo
}

// SetCloneWarning sets the "cloneWarning" field.
func (wacuo *WebAuthnCredentialUpdateOne) SetCloneWarning(b bool) *WebAuthnCredentialUpdateOne {
	wacuo.mutation.SetCloneWarning(b)
	return wacuo
}

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (wacuo *WebAuthnCredentialUpdateOne) check() error {
	if _, ok := wacuo.mutation.UserID(); wacuo.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "WebAuthnCredential.user"`)
	}
	return nil
}

func (wacuo *WebAuthnCredentialUpdateOne) sqlSave(ctx context.Context) (_node *WebAuthnCredential, err error) {
	if err := wacuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(webauthncredential.Table, webauthncredential.Columns, sqlgraph.NewFieldSpec(webauthncredential.FieldID, field.TypeUUID))
	id, ok := wacuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WebAuthnCredential.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, webauthncredential.FieldID)
		for _, f := range fields {
			if !webauthncredential.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != webauthncredential.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.Name(); ok {
		_spec.SetField(webauthncredential.FieldName, field.TypeString, value)
	}
	if value, ok := wacuo.mutation.CreatedAt(); ok {
		_spec.SetField(webauthncredential.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := wacuo.mutation.CredentialId(); ok {
		_spec.SetField(webauthncredential.FieldCredentialId, field.TypeBytes, value)
	}
	if value, ok := wacuo.mutation.PublicKey(); ok {
		_spec.SetField(webauthncredential.FieldPublicKey, field.TypeBytes, value)
	}
	if value, ok := wacuo.mutation.AttestationType(); ok {
		_spec.SetField(webauthncredential.FieldAttestationType, field.TypeString, value)
	}
	if value, ok := wacuo.mutation.Transport(); ok {
		_spec.SetField(webauthncredential.FieldTransport, field.TypeJSON, value)
	}
	if value, ok := wacuo.mutation.AppendedTransport(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, webauthncredential.FieldTransport, value)
		})
	}
	if value, ok := wacuo.mutation.Aaguid(); ok {
		_spec.SetField(webauthncredential.FieldAaguid, field.TypeBytes, value)
	}
	if value, ok := wacuo.mutation.SignCount(); ok {
		_spec.SetField(webauthncredential.FieldSignCount, field.TypeUint32, value)
	}
	if value, ok := wacuo.mutation.AddedSignCount(); ok {
		_spec.AddField(webauthncredential.FieldSignCount, field.TypeUint32, value)
	}
	if value, ok := wacuo.mutation.CloneWarning(); ok {
		_spec.SetField(webauthncredential.FieldCloneWarning, field.TypeBool, value)
	}
	if wacuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthncredential.UserTable,
			Columns: []string{webauthncredential.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wacuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   webauthncredential.UserTable,
			Columns: []string{webauthncredential.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 = &WebAuthnCredential{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{webauthncredential.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	wacuo.mutation.done = true
	return _node, nil
}
