// 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/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/proxy"
)

// ProxyUpdate is the builder for updating Proxy entities.
type ProxyUpdate struct {
	config
	hooks    []Hook
	mutation *ProxyMutation
}

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

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *ProxyUpdate) SetDeletedAt(v time.Time) *ProxyUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableDeletedAt(v *time.Time) *ProxyUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *ProxyUpdate) ClearDeletedAt() *ProxyUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetName sets the "name" field.
func (_u *ProxyUpdate) SetName(v string) *ProxyUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableName(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetProtocol sets the "protocol" field.
func (_u *ProxyUpdate) SetProtocol(v string) *ProxyUpdate {
	_u.mutation.SetProtocol(v)
	return _u
}

// SetNillableProtocol sets the "protocol" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableProtocol(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetProtocol(*v)
	}
	return _u
}

// SetHost sets the "host" field.
func (_u *ProxyUpdate) SetHost(v string) *ProxyUpdate {
	_u.mutation.SetHost(v)
	return _u
}

// SetNillableHost sets the "host" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableHost(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetHost(*v)
	}
	return _u
}

// SetPort sets the "port" field.
func (_u *ProxyUpdate) SetPort(v int) *ProxyUpdate {
	_u.mutation.ResetPort()
	_u.mutation.SetPort(v)
	return _u
}

// SetNillablePort sets the "port" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillablePort(v *int) *ProxyUpdate {
	if v != nil {
		_u.SetPort(*v)
	}
	return _u
}

// AddPort adds value to the "port" field.
func (_u *ProxyUpdate) AddPort(v int) *ProxyUpdate {
	_u.mutation.AddPort(v)
	return _u
}

// SetUsername sets the "username" field.
func (_u *ProxyUpdate) SetUsername(v string) *ProxyUpdate {
	_u.mutation.SetUsername(v)
	return _u
}

// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableUsername(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetUsername(*v)
	}
	return _u
}

// ClearUsername clears the value of the "username" field.
func (_u *ProxyUpdate) ClearUsername() *ProxyUpdate {
	_u.mutation.ClearUsername()
	return _u
}

// SetPassword sets the "password" field.
func (_u *ProxyUpdate) SetPassword(v string) *ProxyUpdate {
	_u.mutation.SetPassword(v)
	return _u
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillablePassword(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetPassword(*v)
	}
	return _u
}

// ClearPassword clears the value of the "password" field.
func (_u *ProxyUpdate) ClearPassword() *ProxyUpdate {
	_u.mutation.ClearPassword()
	return _u
}

// SetStatus sets the "status" field.
func (_u *ProxyUpdate) SetStatus(v string) *ProxyUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ProxyUpdate) SetNillableStatus(v *string) *ProxyUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *ProxyUpdate) Save(ctx context.Context) (int, error) {
	if err := _u.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ProxyUpdate) 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 *ProxyUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if proxy.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized proxy.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := proxy.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *ProxyUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := proxy.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Proxy.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Protocol(); ok {
		if err := proxy.ProtocolValidator(v); err != nil {
			return &ValidationError{Name: "protocol", err: fmt.Errorf(`ent: validator failed for field "Proxy.protocol": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Host(); ok {
		if err := proxy.HostValidator(v); err != nil {
			return &ValidationError{Name: "host", err: fmt.Errorf(`ent: validator failed for field "Proxy.host": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Username(); ok {
		if err := proxy.UsernameValidator(v); err != nil {
			return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "Proxy.username": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Password(); ok {
		if err := proxy.PasswordValidator(v); err != nil {
			return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "Proxy.password": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := proxy.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Proxy.status": %w`, err)}
		}
	}
	return nil
}

func (_u *ProxyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(proxy.Table, proxy.Columns, sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64))
	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.UpdatedAt(); ok {
		_spec.SetField(proxy.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(proxy.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(proxy.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(proxy.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Protocol(); ok {
		_spec.SetField(proxy.FieldProtocol, field.TypeString, value)
	}
	if value, ok := _u.mutation.Host(); ok {
		_spec.SetField(proxy.FieldHost, field.TypeString, value)
	}
	if value, ok := _u.mutation.Port(); ok {
		_spec.SetField(proxy.FieldPort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPort(); ok {
		_spec.AddField(proxy.FieldPort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Username(); ok {
		_spec.SetField(proxy.FieldUsername, field.TypeString, value)
	}
	if _u.mutation.UsernameCleared() {
		_spec.ClearField(proxy.FieldUsername, field.TypeString)
	}
	if value, ok := _u.mutation.Password(); ok {
		_spec.SetField(proxy.FieldPassword, field.TypeString, value)
	}
	if _u.mutation.PasswordCleared() {
		_spec.ClearField(proxy.FieldPassword, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(proxy.FieldStatus, field.TypeString, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{proxy.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ProxyUpdateOne is the builder for updating a single Proxy entity.
type ProxyUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ProxyMutation
}

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *ProxyUpdateOne) SetDeletedAt(v time.Time) *ProxyUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableDeletedAt(v *time.Time) *ProxyUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *ProxyUpdateOne) ClearDeletedAt() *ProxyUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetName sets the "name" field.
func (_u *ProxyUpdateOne) SetName(v string) *ProxyUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableName(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetProtocol sets the "protocol" field.
func (_u *ProxyUpdateOne) SetProtocol(v string) *ProxyUpdateOne {
	_u.mutation.SetProtocol(v)
	return _u
}

// SetNillableProtocol sets the "protocol" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableProtocol(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetProtocol(*v)
	}
	return _u
}

// SetHost sets the "host" field.
func (_u *ProxyUpdateOne) SetHost(v string) *ProxyUpdateOne {
	_u.mutation.SetHost(v)
	return _u
}

// SetNillableHost sets the "host" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableHost(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetHost(*v)
	}
	return _u
}

// SetPort sets the "port" field.
func (_u *ProxyUpdateOne) SetPort(v int) *ProxyUpdateOne {
	_u.mutation.ResetPort()
	_u.mutation.SetPort(v)
	return _u
}

// SetNillablePort sets the "port" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillablePort(v *int) *ProxyUpdateOne {
	if v != nil {
		_u.SetPort(*v)
	}
	return _u
}

// AddPort adds value to the "port" field.
func (_u *ProxyUpdateOne) AddPort(v int) *ProxyUpdateOne {
	_u.mutation.AddPort(v)
	return _u
}

// SetUsername sets the "username" field.
func (_u *ProxyUpdateOne) SetUsername(v string) *ProxyUpdateOne {
	_u.mutation.SetUsername(v)
	return _u
}

// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableUsername(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetUsername(*v)
	}
	return _u
}

// ClearUsername clears the value of the "username" field.
func (_u *ProxyUpdateOne) ClearUsername() *ProxyUpdateOne {
	_u.mutation.ClearUsername()
	return _u
}

// SetPassword sets the "password" field.
func (_u *ProxyUpdateOne) SetPassword(v string) *ProxyUpdateOne {
	_u.mutation.SetPassword(v)
	return _u
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillablePassword(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetPassword(*v)
	}
	return _u
}

// ClearPassword clears the value of the "password" field.
func (_u *ProxyUpdateOne) ClearPassword() *ProxyUpdateOne {
	_u.mutation.ClearPassword()
	return _u
}

// SetStatus sets the "status" field.
func (_u *ProxyUpdateOne) SetStatus(v string) *ProxyUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ProxyUpdateOne) SetNillableStatus(v *string) *ProxyUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

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

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

// Save executes the query and returns the updated Proxy entity.
func (_u *ProxyUpdateOne) Save(ctx context.Context) (*Proxy, error) {
	if err := _u.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ProxyUpdateOne) 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 *ProxyUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if proxy.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized proxy.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := proxy.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *ProxyUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := proxy.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Proxy.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Protocol(); ok {
		if err := proxy.ProtocolValidator(v); err != nil {
			return &ValidationError{Name: "protocol", err: fmt.Errorf(`ent: validator failed for field "Proxy.protocol": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Host(); ok {
		if err := proxy.HostValidator(v); err != nil {
			return &ValidationError{Name: "host", err: fmt.Errorf(`ent: validator failed for field "Proxy.host": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Username(); ok {
		if err := proxy.UsernameValidator(v); err != nil {
			return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "Proxy.username": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Password(); ok {
		if err := proxy.PasswordValidator(v); err != nil {
			return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "Proxy.password": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := proxy.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Proxy.status": %w`, err)}
		}
	}
	return nil
}

func (_u *ProxyUpdateOne) sqlSave(ctx context.Context) (_node *Proxy, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(proxy.Table, proxy.Columns, sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Proxy.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, proxy.FieldID)
		for _, f := range fields {
			if !proxy.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != proxy.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.UpdatedAt(); ok {
		_spec.SetField(proxy.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(proxy.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(proxy.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(proxy.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Protocol(); ok {
		_spec.SetField(proxy.FieldProtocol, field.TypeString, value)
	}
	if value, ok := _u.mutation.Host(); ok {
		_spec.SetField(proxy.FieldHost, field.TypeString, value)
	}
	if value, ok := _u.mutation.Port(); ok {
		_spec.SetField(proxy.FieldPort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPort(); ok {
		_spec.AddField(proxy.FieldPort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Username(); ok {
		_spec.SetField(proxy.FieldUsername, field.TypeString, value)
	}
	if _u.mutation.UsernameCleared() {
		_spec.ClearField(proxy.FieldUsername, field.TypeString)
	}
	if value, ok := _u.mutation.Password(); ok {
		_spec.SetField(proxy.FieldPassword, field.TypeString, value)
	}
	if _u.mutation.PasswordCleared() {
		_spec.ClearField(proxy.FieldPassword, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(proxy.FieldStatus, field.TypeString, value)
	}
	_node = &Proxy{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{proxy.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
