// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/challenge"
	"PasswordManager/ent/predicate"
	"PasswordManager/ent/totpcredential"
	"PasswordManager/ent/user"
	"context"
	"errors"
	"fmt"

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

// TotpCredentialUpdate is the builder for updating TotpCredential entities.
type TotpCredentialUpdate struct {
	config
	hooks    []Hook
	mutation *TotpCredentialMutation
}

// Where appends a list predicates to the TotpCredentialUpdate builder.
func (tcu *TotpCredentialUpdate) Where(ps ...predicate.TotpCredential) *TotpCredentialUpdate {
	tcu.mutation.Where(ps...)
	return tcu
}

// SetSecret sets the "secret" field.
func (tcu *TotpCredentialUpdate) SetSecret(s string) *TotpCredentialUpdate {
	tcu.mutation.SetSecret(s)
	return tcu
}

// SetValidated sets the "validated" field.
func (tcu *TotpCredentialUpdate) SetValidated(b bool) *TotpCredentialUpdate {
	tcu.mutation.SetValidated(b)
	return tcu
}

// SetNillableValidated sets the "validated" field if the given value is not nil.
func (tcu *TotpCredentialUpdate) SetNillableValidated(b *bool) *TotpCredentialUpdate {
	if b != nil {
		tcu.SetValidated(*b)
	}
	return tcu
}

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

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

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

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

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

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

// Mutation returns the TotpCredentialMutation object of the builder.
func (tcu *TotpCredentialUpdate) Mutation() *TotpCredentialMutation {
	return tcu.mutation
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tcu *TotpCredentialUpdate) check() error {
	if v, ok := tcu.mutation.Secret(); ok {
		if err := totpcredential.SecretValidator(v); err != nil {
			return &ValidationError{Name: "secret", err: fmt.Errorf(`ent: validator failed for field "TotpCredential.secret": %w`, err)}
		}
	}
	return nil
}

func (tcu *TotpCredentialUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tcu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(totpcredential.Table, totpcredential.Columns, sqlgraph.NewFieldSpec(totpcredential.FieldID, field.TypeUUID))
	if ps := tcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcu.mutation.Secret(); ok {
		_spec.SetField(totpcredential.FieldSecret, field.TypeString, value)
	}
	if value, ok := tcu.mutation.Validated(); ok {
		_spec.SetField(totpcredential.FieldValidated, field.TypeBool, value)
	}
	if tcu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.UserTable,
			Columns: []string{totpcredential.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.UserTable,
			Columns: []string{totpcredential.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 tcu.mutation.ChallengeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.ChallengeTable,
			Columns: []string{totpcredential.ChallengeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(challenge.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.ChallengeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.ChallengeTable,
			Columns: []string{totpcredential.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, tcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{totpcredential.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tcu.mutation.done = true
	return n, nil
}

// TotpCredentialUpdateOne is the builder for updating a single TotpCredential entity.
type TotpCredentialUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TotpCredentialMutation
}

// SetSecret sets the "secret" field.
func (tcuo *TotpCredentialUpdateOne) SetSecret(s string) *TotpCredentialUpdateOne {
	tcuo.mutation.SetSecret(s)
	return tcuo
}

// SetValidated sets the "validated" field.
func (tcuo *TotpCredentialUpdateOne) SetValidated(b bool) *TotpCredentialUpdateOne {
	tcuo.mutation.SetValidated(b)
	return tcuo
}

// SetNillableValidated sets the "validated" field if the given value is not nil.
func (tcuo *TotpCredentialUpdateOne) SetNillableValidated(b *bool) *TotpCredentialUpdateOne {
	if b != nil {
		tcuo.SetValidated(*b)
	}
	return tcuo
}

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

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

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

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

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

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

// Mutation returns the TotpCredentialMutation object of the builder.
func (tcuo *TotpCredentialUpdateOne) Mutation() *TotpCredentialMutation {
	return tcuo.mutation
}

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

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

// Where appends a list predicates to the TotpCredentialUpdate builder.
func (tcuo *TotpCredentialUpdateOne) Where(ps ...predicate.TotpCredential) *TotpCredentialUpdateOne {
	tcuo.mutation.Where(ps...)
	return tcuo
}

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

// Save executes the query and returns the updated TotpCredential entity.
func (tcuo *TotpCredentialUpdateOne) Save(ctx context.Context) (*TotpCredential, error) {
	return withHooks[*TotpCredential, TotpCredentialMutation](ctx, tcuo.sqlSave, tcuo.mutation, tcuo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tcuo *TotpCredentialUpdateOne) check() error {
	if v, ok := tcuo.mutation.Secret(); ok {
		if err := totpcredential.SecretValidator(v); err != nil {
			return &ValidationError{Name: "secret", err: fmt.Errorf(`ent: validator failed for field "TotpCredential.secret": %w`, err)}
		}
	}
	return nil
}

func (tcuo *TotpCredentialUpdateOne) sqlSave(ctx context.Context) (_node *TotpCredential, err error) {
	if err := tcuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(totpcredential.Table, totpcredential.Columns, sqlgraph.NewFieldSpec(totpcredential.FieldID, field.TypeUUID))
	id, ok := tcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TotpCredential.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, totpcredential.FieldID)
		for _, f := range fields {
			if !totpcredential.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != totpcredential.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcuo.mutation.Secret(); ok {
		_spec.SetField(totpcredential.FieldSecret, field.TypeString, value)
	}
	if value, ok := tcuo.mutation.Validated(); ok {
		_spec.SetField(totpcredential.FieldValidated, field.TypeBool, value)
	}
	if tcuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.UserTable,
			Columns: []string{totpcredential.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.UserTable,
			Columns: []string{totpcredential.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 tcuo.mutation.ChallengeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.ChallengeTable,
			Columns: []string{totpcredential.ChallengeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(challenge.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.ChallengeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   totpcredential.ChallengeTable,
			Columns: []string{totpcredential.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 = &TotpCredential{config: tcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{totpcredential.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tcuo.mutation.done = true
	return _node, nil
}
