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

// PaymentProviderInstanceUpdate is the builder for updating PaymentProviderInstance entities.
type PaymentProviderInstanceUpdate struct {
	config
	hooks    []Hook
	mutation *PaymentProviderInstanceMutation
}

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

// SetProviderKey sets the "provider_key" field.
func (_u *PaymentProviderInstanceUpdate) SetProviderKey(v string) *PaymentProviderInstanceUpdate {
	_u.mutation.SetProviderKey(v)
	return _u
}

// SetNillableProviderKey sets the "provider_key" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableProviderKey(v *string) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetProviderKey(*v)
	}
	return _u
}

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

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

// SetConfig sets the "config" field.
func (_u *PaymentProviderInstanceUpdate) SetConfig(v string) *PaymentProviderInstanceUpdate {
	_u.mutation.SetConfig(v)
	return _u
}

// SetNillableConfig sets the "config" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableConfig(v *string) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetConfig(*v)
	}
	return _u
}

// SetSupportedTypes sets the "supported_types" field.
func (_u *PaymentProviderInstanceUpdate) SetSupportedTypes(v string) *PaymentProviderInstanceUpdate {
	_u.mutation.SetSupportedTypes(v)
	return _u
}

// SetNillableSupportedTypes sets the "supported_types" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableSupportedTypes(v *string) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetSupportedTypes(*v)
	}
	return _u
}

// SetEnabled sets the "enabled" field.
func (_u *PaymentProviderInstanceUpdate) SetEnabled(v bool) *PaymentProviderInstanceUpdate {
	_u.mutation.SetEnabled(v)
	return _u
}

// SetNillableEnabled sets the "enabled" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableEnabled(v *bool) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetEnabled(*v)
	}
	return _u
}

// SetPaymentMode sets the "payment_mode" field.
func (_u *PaymentProviderInstanceUpdate) SetPaymentMode(v string) *PaymentProviderInstanceUpdate {
	_u.mutation.SetPaymentMode(v)
	return _u
}

// SetNillablePaymentMode sets the "payment_mode" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillablePaymentMode(v *string) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetPaymentMode(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *PaymentProviderInstanceUpdate) SetSortOrder(v int) *PaymentProviderInstanceUpdate {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableSortOrder(v *int) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *PaymentProviderInstanceUpdate) AddSortOrder(v int) *PaymentProviderInstanceUpdate {
	_u.mutation.AddSortOrder(v)
	return _u
}

// SetLimits sets the "limits" field.
func (_u *PaymentProviderInstanceUpdate) SetLimits(v string) *PaymentProviderInstanceUpdate {
	_u.mutation.SetLimits(v)
	return _u
}

// SetNillableLimits sets the "limits" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableLimits(v *string) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetLimits(*v)
	}
	return _u
}

// SetRefundEnabled sets the "refund_enabled" field.
func (_u *PaymentProviderInstanceUpdate) SetRefundEnabled(v bool) *PaymentProviderInstanceUpdate {
	_u.mutation.SetRefundEnabled(v)
	return _u
}

// SetNillableRefundEnabled sets the "refund_enabled" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdate) SetNillableRefundEnabled(v *bool) *PaymentProviderInstanceUpdate {
	if v != nil {
		_u.SetRefundEnabled(*v)
	}
	return _u
}

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *PaymentProviderInstanceUpdate) 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 *PaymentProviderInstanceUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *PaymentProviderInstanceUpdate) 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 *PaymentProviderInstanceUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := paymentproviderinstance.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *PaymentProviderInstanceUpdate) check() error {
	if v, ok := _u.mutation.ProviderKey(); ok {
		if err := paymentproviderinstance.ProviderKeyValidator(v); err != nil {
			return &ValidationError{Name: "provider_key", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.provider_key": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := paymentproviderinstance.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SupportedTypes(); ok {
		if err := paymentproviderinstance.SupportedTypesValidator(v); err != nil {
			return &ValidationError{Name: "supported_types", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.supported_types": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PaymentMode(); ok {
		if err := paymentproviderinstance.PaymentModeValidator(v); err != nil {
			return &ValidationError{Name: "payment_mode", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.payment_mode": %w`, err)}
		}
	}
	return nil
}

func (_u *PaymentProviderInstanceUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(paymentproviderinstance.Table, paymentproviderinstance.Columns, sqlgraph.NewFieldSpec(paymentproviderinstance.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.ProviderKey(); ok {
		_spec.SetField(paymentproviderinstance.FieldProviderKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(paymentproviderinstance.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Config(); ok {
		_spec.SetField(paymentproviderinstance.FieldConfig, field.TypeString, value)
	}
	if value, ok := _u.mutation.SupportedTypes(); ok {
		_spec.SetField(paymentproviderinstance.FieldSupportedTypes, field.TypeString, value)
	}
	if value, ok := _u.mutation.Enabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.PaymentMode(); ok {
		_spec.SetField(paymentproviderinstance.FieldPaymentMode, field.TypeString, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(paymentproviderinstance.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(paymentproviderinstance.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Limits(); ok {
		_spec.SetField(paymentproviderinstance.FieldLimits, field.TypeString, value)
	}
	if value, ok := _u.mutation.RefundEnabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldRefundEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(paymentproviderinstance.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{paymentproviderinstance.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// PaymentProviderInstanceUpdateOne is the builder for updating a single PaymentProviderInstance entity.
type PaymentProviderInstanceUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PaymentProviderInstanceMutation
}

// SetProviderKey sets the "provider_key" field.
func (_u *PaymentProviderInstanceUpdateOne) SetProviderKey(v string) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetProviderKey(v)
	return _u
}

// SetNillableProviderKey sets the "provider_key" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableProviderKey(v *string) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetProviderKey(*v)
	}
	return _u
}

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

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

// SetConfig sets the "config" field.
func (_u *PaymentProviderInstanceUpdateOne) SetConfig(v string) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetConfig(v)
	return _u
}

// SetNillableConfig sets the "config" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableConfig(v *string) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetConfig(*v)
	}
	return _u
}

// SetSupportedTypes sets the "supported_types" field.
func (_u *PaymentProviderInstanceUpdateOne) SetSupportedTypes(v string) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetSupportedTypes(v)
	return _u
}

// SetNillableSupportedTypes sets the "supported_types" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableSupportedTypes(v *string) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetSupportedTypes(*v)
	}
	return _u
}

// SetEnabled sets the "enabled" field.
func (_u *PaymentProviderInstanceUpdateOne) SetEnabled(v bool) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetEnabled(v)
	return _u
}

// SetNillableEnabled sets the "enabled" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableEnabled(v *bool) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetEnabled(*v)
	}
	return _u
}

// SetPaymentMode sets the "payment_mode" field.
func (_u *PaymentProviderInstanceUpdateOne) SetPaymentMode(v string) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetPaymentMode(v)
	return _u
}

// SetNillablePaymentMode sets the "payment_mode" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillablePaymentMode(v *string) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetPaymentMode(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *PaymentProviderInstanceUpdateOne) SetSortOrder(v int) *PaymentProviderInstanceUpdateOne {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableSortOrder(v *int) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *PaymentProviderInstanceUpdateOne) AddSortOrder(v int) *PaymentProviderInstanceUpdateOne {
	_u.mutation.AddSortOrder(v)
	return _u
}

// SetLimits sets the "limits" field.
func (_u *PaymentProviderInstanceUpdateOne) SetLimits(v string) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetLimits(v)
	return _u
}

// SetNillableLimits sets the "limits" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableLimits(v *string) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetLimits(*v)
	}
	return _u
}

// SetRefundEnabled sets the "refund_enabled" field.
func (_u *PaymentProviderInstanceUpdateOne) SetRefundEnabled(v bool) *PaymentProviderInstanceUpdateOne {
	_u.mutation.SetRefundEnabled(v)
	return _u
}

// SetNillableRefundEnabled sets the "refund_enabled" field if the given value is not nil.
func (_u *PaymentProviderInstanceUpdateOne) SetNillableRefundEnabled(v *bool) *PaymentProviderInstanceUpdateOne {
	if v != nil {
		_u.SetRefundEnabled(*v)
	}
	return _u
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *PaymentProviderInstanceUpdateOne) 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 *PaymentProviderInstanceUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := paymentproviderinstance.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *PaymentProviderInstanceUpdateOne) check() error {
	if v, ok := _u.mutation.ProviderKey(); ok {
		if err := paymentproviderinstance.ProviderKeyValidator(v); err != nil {
			return &ValidationError{Name: "provider_key", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.provider_key": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := paymentproviderinstance.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SupportedTypes(); ok {
		if err := paymentproviderinstance.SupportedTypesValidator(v); err != nil {
			return &ValidationError{Name: "supported_types", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.supported_types": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PaymentMode(); ok {
		if err := paymentproviderinstance.PaymentModeValidator(v); err != nil {
			return &ValidationError{Name: "payment_mode", err: fmt.Errorf(`ent: validator failed for field "PaymentProviderInstance.payment_mode": %w`, err)}
		}
	}
	return nil
}

func (_u *PaymentProviderInstanceUpdateOne) sqlSave(ctx context.Context) (_node *PaymentProviderInstance, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(paymentproviderinstance.Table, paymentproviderinstance.Columns, sqlgraph.NewFieldSpec(paymentproviderinstance.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PaymentProviderInstance.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, paymentproviderinstance.FieldID)
		for _, f := range fields {
			if !paymentproviderinstance.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != paymentproviderinstance.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.ProviderKey(); ok {
		_spec.SetField(paymentproviderinstance.FieldProviderKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(paymentproviderinstance.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Config(); ok {
		_spec.SetField(paymentproviderinstance.FieldConfig, field.TypeString, value)
	}
	if value, ok := _u.mutation.SupportedTypes(); ok {
		_spec.SetField(paymentproviderinstance.FieldSupportedTypes, field.TypeString, value)
	}
	if value, ok := _u.mutation.Enabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.PaymentMode(); ok {
		_spec.SetField(paymentproviderinstance.FieldPaymentMode, field.TypeString, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(paymentproviderinstance.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(paymentproviderinstance.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Limits(); ok {
		_spec.SetField(paymentproviderinstance.FieldLimits, field.TypeString, value)
	}
	if value, ok := _u.mutation.RefundEnabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldRefundEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(paymentproviderinstance.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &PaymentProviderInstance{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{paymentproviderinstance.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
