// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/AvengeMedia/dankcalendar/core/ent/account"
	"github.com/AvengeMedia/dankcalendar/core/ent/predicate"
	"github.com/AvengeMedia/dankcalendar/core/ent/secret"
)

// SecretUpdate is the builder for updating Secret entities.
type SecretUpdate struct {
	config
	hooks     []Hook
	mutation  *SecretMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

// SetKey sets the "key" field.
func (_u *SecretUpdate) SetKey(v string) *SecretUpdate {
	_u.mutation.SetKey(v)
	return _u
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (_u *SecretUpdate) SetNillableKey(v *string) *SecretUpdate {
	if v != nil {
		_u.SetKey(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *SecretUpdate) SetValue(v []byte) *SecretUpdate {
	_u.mutation.SetValue(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *SecretUpdate) SetUpdatedAt(v time.Time) *SecretUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *SecretUpdate) SetAccountID(id string) *SecretUpdate {
	_u.mutation.SetAccountID(id)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *SecretUpdate) SetAccount(v *Account) *SecretUpdate {
	return _u.SetAccountID(v.ID)
}

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *SecretUpdate) ClearAccount() *SecretUpdate {
	_u.mutation.ClearAccount()
	return _u
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *SecretUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := secret.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *SecretUpdate) check() error {
	if v, ok := _u.mutation.Key(); ok {
		if err := secret.KeyValidator(v); err != nil {
			return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Secret.key": %w`, err)}
		}
	}
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Secret.account"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *SecretUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *SecretUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *SecretUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(secret.Table, secret.Columns, sqlgraph.NewFieldSpec(secret.FieldID, field.TypeString))
	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.Key(); ok {
		_spec.SetField(secret.FieldKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(secret.FieldValue, field.TypeBytes, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(secret.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   secret.AccountTable,
			Columns: []string{secret.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   secret.AccountTable,
			Columns: []string{secret.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{secret.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// SecretUpdateOne is the builder for updating a single Secret entity.
type SecretUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *SecretMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetKey sets the "key" field.
func (_u *SecretUpdateOne) SetKey(v string) *SecretUpdateOne {
	_u.mutation.SetKey(v)
	return _u
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (_u *SecretUpdateOne) SetNillableKey(v *string) *SecretUpdateOne {
	if v != nil {
		_u.SetKey(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *SecretUpdateOne) SetValue(v []byte) *SecretUpdateOne {
	_u.mutation.SetValue(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *SecretUpdateOne) SetUpdatedAt(v time.Time) *SecretUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *SecretUpdateOne) SetAccountID(id string) *SecretUpdateOne {
	_u.mutation.SetAccountID(id)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *SecretUpdateOne) SetAccount(v *Account) *SecretUpdateOne {
	return _u.SetAccountID(v.ID)
}

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *SecretUpdateOne) ClearAccount() *SecretUpdateOne {
	_u.mutation.ClearAccount()
	return _u
}

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *SecretUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := secret.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *SecretUpdateOne) check() error {
	if v, ok := _u.mutation.Key(); ok {
		if err := secret.KeyValidator(v); err != nil {
			return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Secret.key": %w`, err)}
		}
	}
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Secret.account"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *SecretUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *SecretUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *SecretUpdateOne) sqlSave(ctx context.Context) (_node *Secret, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(secret.Table, secret.Columns, sqlgraph.NewFieldSpec(secret.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Secret.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, secret.FieldID)
		for _, f := range fields {
			if !secret.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != secret.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.Key(); ok {
		_spec.SetField(secret.FieldKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(secret.FieldValue, field.TypeBytes, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(secret.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   secret.AccountTable,
			Columns: []string{secret.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   secret.AccountTable,
			Columns: []string{secret.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &Secret{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{secret.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
