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

// PaymentProviderInstanceCreate is the builder for creating a PaymentProviderInstance entity.
type PaymentProviderInstanceCreate struct {
	config
	mutation *PaymentProviderInstanceMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

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

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

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

// SetSortOrder sets the "sort_order" field.
func (_c *PaymentProviderInstanceCreate) SetSortOrder(v int) *PaymentProviderInstanceCreate {
	_c.mutation.SetSortOrder(v)
	return _c
}

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

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

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

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

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

// SetCreatedAt sets the "created_at" field.
func (_c *PaymentProviderInstanceCreate) SetCreatedAt(v time.Time) *PaymentProviderInstanceCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *PaymentProviderInstanceCreate) SetNillableCreatedAt(v *time.Time) *PaymentProviderInstanceCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *PaymentProviderInstanceCreate) SetNillableUpdatedAt(v *time.Time) *PaymentProviderInstanceCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

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

// Save creates the PaymentProviderInstance in the database.
func (_c *PaymentProviderInstanceCreate) Save(ctx context.Context) (*PaymentProviderInstance, error) {
	_c.defaults()
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (_c *PaymentProviderInstanceCreate) SaveX(ctx context.Context) *PaymentProviderInstance {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (_c *PaymentProviderInstanceCreate) defaults() {
	if _, ok := _c.mutation.Name(); !ok {
		v := paymentproviderinstance.DefaultName
		_c.mutation.SetName(v)
	}
	if _, ok := _c.mutation.SupportedTypes(); !ok {
		v := paymentproviderinstance.DefaultSupportedTypes
		_c.mutation.SetSupportedTypes(v)
	}
	if _, ok := _c.mutation.Enabled(); !ok {
		v := paymentproviderinstance.DefaultEnabled
		_c.mutation.SetEnabled(v)
	}
	if _, ok := _c.mutation.PaymentMode(); !ok {
		v := paymentproviderinstance.DefaultPaymentMode
		_c.mutation.SetPaymentMode(v)
	}
	if _, ok := _c.mutation.SortOrder(); !ok {
		v := paymentproviderinstance.DefaultSortOrder
		_c.mutation.SetSortOrder(v)
	}
	if _, ok := _c.mutation.Limits(); !ok {
		v := paymentproviderinstance.DefaultLimits
		_c.mutation.SetLimits(v)
	}
	if _, ok := _c.mutation.RefundEnabled(); !ok {
		v := paymentproviderinstance.DefaultRefundEnabled
		_c.mutation.SetRefundEnabled(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := paymentproviderinstance.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := paymentproviderinstance.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *PaymentProviderInstanceCreate) check() error {
	if _, ok := _c.mutation.ProviderKey(); !ok {
		return &ValidationError{Name: "provider_key", err: errors.New(`ent: missing required field "PaymentProviderInstance.provider_key"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "PaymentProviderInstance.name"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Config(); !ok {
		return &ValidationError{Name: "config", err: errors.New(`ent: missing required field "PaymentProviderInstance.config"`)}
	}
	if _, ok := _c.mutation.SupportedTypes(); !ok {
		return &ValidationError{Name: "supported_types", err: errors.New(`ent: missing required field "PaymentProviderInstance.supported_types"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Enabled(); !ok {
		return &ValidationError{Name: "enabled", err: errors.New(`ent: missing required field "PaymentProviderInstance.enabled"`)}
	}
	if _, ok := _c.mutation.PaymentMode(); !ok {
		return &ValidationError{Name: "payment_mode", err: errors.New(`ent: missing required field "PaymentProviderInstance.payment_mode"`)}
	}
	if v, ok := _c.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)}
		}
	}
	if _, ok := _c.mutation.SortOrder(); !ok {
		return &ValidationError{Name: "sort_order", err: errors.New(`ent: missing required field "PaymentProviderInstance.sort_order"`)}
	}
	if _, ok := _c.mutation.Limits(); !ok {
		return &ValidationError{Name: "limits", err: errors.New(`ent: missing required field "PaymentProviderInstance.limits"`)}
	}
	if _, ok := _c.mutation.RefundEnabled(); !ok {
		return &ValidationError{Name: "refund_enabled", err: errors.New(`ent: missing required field "PaymentProviderInstance.refund_enabled"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PaymentProviderInstance.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PaymentProviderInstance.updated_at"`)}
	}
	return nil
}

func (_c *PaymentProviderInstanceCreate) sqlSave(ctx context.Context) (*PaymentProviderInstance, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int64(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *PaymentProviderInstanceCreate) createSpec() (*PaymentProviderInstance, *sqlgraph.CreateSpec) {
	var (
		_node = &PaymentProviderInstance{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(paymentproviderinstance.Table, sqlgraph.NewFieldSpec(paymentproviderinstance.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.ProviderKey(); ok {
		_spec.SetField(paymentproviderinstance.FieldProviderKey, field.TypeString, value)
		_node.ProviderKey = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(paymentproviderinstance.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Config(); ok {
		_spec.SetField(paymentproviderinstance.FieldConfig, field.TypeString, value)
		_node.Config = value
	}
	if value, ok := _c.mutation.SupportedTypes(); ok {
		_spec.SetField(paymentproviderinstance.FieldSupportedTypes, field.TypeString, value)
		_node.SupportedTypes = value
	}
	if value, ok := _c.mutation.Enabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldEnabled, field.TypeBool, value)
		_node.Enabled = value
	}
	if value, ok := _c.mutation.PaymentMode(); ok {
		_spec.SetField(paymentproviderinstance.FieldPaymentMode, field.TypeString, value)
		_node.PaymentMode = value
	}
	if value, ok := _c.mutation.SortOrder(); ok {
		_spec.SetField(paymentproviderinstance.FieldSortOrder, field.TypeInt, value)
		_node.SortOrder = value
	}
	if value, ok := _c.mutation.Limits(); ok {
		_spec.SetField(paymentproviderinstance.FieldLimits, field.TypeString, value)
		_node.Limits = value
	}
	if value, ok := _c.mutation.RefundEnabled(); ok {
		_spec.SetField(paymentproviderinstance.FieldRefundEnabled, field.TypeBool, value)
		_node.RefundEnabled = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(paymentproviderinstance.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(paymentproviderinstance.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PaymentProviderInstance.Create().
//		SetProviderKey(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PaymentProviderInstanceUpsert) {
//			SetProviderKey(v+v).
//		}).
//		Exec(ctx)
func (_c *PaymentProviderInstanceCreate) OnConflict(opts ...sql.ConflictOption) *PaymentProviderInstanceUpsertOne {
	_c.conflict = opts
	return &PaymentProviderInstanceUpsertOne{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *PaymentProviderInstanceCreate) OnConflictColumns(columns ...string) *PaymentProviderInstanceUpsertOne {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &PaymentProviderInstanceUpsertOne{
		create: _c,
	}
}

type (
	// PaymentProviderInstanceUpsertOne is the builder for "upsert"-ing
	//  one PaymentProviderInstance node.
	PaymentProviderInstanceUpsertOne struct {
		create *PaymentProviderInstanceCreate
	}

	// PaymentProviderInstanceUpsert is the "OnConflict" setter.
	PaymentProviderInstanceUpsert struct {
		*sql.UpdateSet
	}
)

// SetProviderKey sets the "provider_key" field.
func (u *PaymentProviderInstanceUpsert) SetProviderKey(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldProviderKey, v)
	return u
}

// UpdateProviderKey sets the "provider_key" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateProviderKey() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldProviderKey)
	return u
}

// SetName sets the "name" field.
func (u *PaymentProviderInstanceUpsert) SetName(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateName() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldName)
	return u
}

// SetConfig sets the "config" field.
func (u *PaymentProviderInstanceUpsert) SetConfig(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldConfig, v)
	return u
}

// UpdateConfig sets the "config" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateConfig() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldConfig)
	return u
}

// SetSupportedTypes sets the "supported_types" field.
func (u *PaymentProviderInstanceUpsert) SetSupportedTypes(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldSupportedTypes, v)
	return u
}

// UpdateSupportedTypes sets the "supported_types" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateSupportedTypes() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldSupportedTypes)
	return u
}

// SetEnabled sets the "enabled" field.
func (u *PaymentProviderInstanceUpsert) SetEnabled(v bool) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldEnabled, v)
	return u
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateEnabled() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldEnabled)
	return u
}

// SetPaymentMode sets the "payment_mode" field.
func (u *PaymentProviderInstanceUpsert) SetPaymentMode(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldPaymentMode, v)
	return u
}

// UpdatePaymentMode sets the "payment_mode" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdatePaymentMode() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldPaymentMode)
	return u
}

// SetSortOrder sets the "sort_order" field.
func (u *PaymentProviderInstanceUpsert) SetSortOrder(v int) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldSortOrder, v)
	return u
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateSortOrder() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldSortOrder)
	return u
}

// AddSortOrder adds v to the "sort_order" field.
func (u *PaymentProviderInstanceUpsert) AddSortOrder(v int) *PaymentProviderInstanceUpsert {
	u.Add(paymentproviderinstance.FieldSortOrder, v)
	return u
}

// SetLimits sets the "limits" field.
func (u *PaymentProviderInstanceUpsert) SetLimits(v string) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldLimits, v)
	return u
}

// UpdateLimits sets the "limits" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateLimits() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldLimits)
	return u
}

// SetRefundEnabled sets the "refund_enabled" field.
func (u *PaymentProviderInstanceUpsert) SetRefundEnabled(v bool) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldRefundEnabled, v)
	return u
}

// UpdateRefundEnabled sets the "refund_enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateRefundEnabled() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldRefundEnabled)
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PaymentProviderInstanceUpsert) SetUpdatedAt(v time.Time) *PaymentProviderInstanceUpsert {
	u.Set(paymentproviderinstance.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsert) UpdateUpdatedAt() *PaymentProviderInstanceUpsert {
	u.SetExcluded(paymentproviderinstance.FieldUpdatedAt)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *PaymentProviderInstanceUpsertOne) UpdateNewValues() *PaymentProviderInstanceUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(paymentproviderinstance.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *PaymentProviderInstanceUpsertOne) Ignore() *PaymentProviderInstanceUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PaymentProviderInstanceUpsertOne) DoNothing() *PaymentProviderInstanceUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PaymentProviderInstanceCreate.OnConflict
// documentation for more info.
func (u *PaymentProviderInstanceUpsertOne) Update(set func(*PaymentProviderInstanceUpsert)) *PaymentProviderInstanceUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PaymentProviderInstanceUpsert{UpdateSet: update})
	}))
	return u
}

// SetProviderKey sets the "provider_key" field.
func (u *PaymentProviderInstanceUpsertOne) SetProviderKey(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetProviderKey(v)
	})
}

// UpdateProviderKey sets the "provider_key" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateProviderKey() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateProviderKey()
	})
}

// SetName sets the "name" field.
func (u *PaymentProviderInstanceUpsertOne) SetName(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateName() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateName()
	})
}

// SetConfig sets the "config" field.
func (u *PaymentProviderInstanceUpsertOne) SetConfig(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetConfig(v)
	})
}

// UpdateConfig sets the "config" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateConfig() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateConfig()
	})
}

// SetSupportedTypes sets the "supported_types" field.
func (u *PaymentProviderInstanceUpsertOne) SetSupportedTypes(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetSupportedTypes(v)
	})
}

// UpdateSupportedTypes sets the "supported_types" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateSupportedTypes() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateSupportedTypes()
	})
}

// SetEnabled sets the "enabled" field.
func (u *PaymentProviderInstanceUpsertOne) SetEnabled(v bool) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetEnabled(v)
	})
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateEnabled() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateEnabled()
	})
}

// SetPaymentMode sets the "payment_mode" field.
func (u *PaymentProviderInstanceUpsertOne) SetPaymentMode(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetPaymentMode(v)
	})
}

// UpdatePaymentMode sets the "payment_mode" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdatePaymentMode() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdatePaymentMode()
	})
}

// SetSortOrder sets the "sort_order" field.
func (u *PaymentProviderInstanceUpsertOne) SetSortOrder(v int) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetSortOrder(v)
	})
}

// AddSortOrder adds v to the "sort_order" field.
func (u *PaymentProviderInstanceUpsertOne) AddSortOrder(v int) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.AddSortOrder(v)
	})
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateSortOrder() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateSortOrder()
	})
}

// SetLimits sets the "limits" field.
func (u *PaymentProviderInstanceUpsertOne) SetLimits(v string) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetLimits(v)
	})
}

// UpdateLimits sets the "limits" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateLimits() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateLimits()
	})
}

// SetRefundEnabled sets the "refund_enabled" field.
func (u *PaymentProviderInstanceUpsertOne) SetRefundEnabled(v bool) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetRefundEnabled(v)
	})
}

// UpdateRefundEnabled sets the "refund_enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateRefundEnabled() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateRefundEnabled()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PaymentProviderInstanceUpsertOne) SetUpdatedAt(v time.Time) *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertOne) UpdateUpdatedAt() *PaymentProviderInstanceUpsertOne {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateUpdatedAt()
	})
}

// Exec executes the query.
func (u *PaymentProviderInstanceUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PaymentProviderInstanceCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *PaymentProviderInstanceUpsertOne) ID(ctx context.Context) (id int64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *PaymentProviderInstanceUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// PaymentProviderInstanceCreateBulk is the builder for creating many PaymentProviderInstance entities in bulk.
type PaymentProviderInstanceCreateBulk struct {
	config
	err      error
	builders []*PaymentProviderInstanceCreate
	conflict []sql.ConflictOption
}

// Save creates the PaymentProviderInstance entities in the database.
func (_c *PaymentProviderInstanceCreateBulk) Save(ctx context.Context) ([]*PaymentProviderInstance, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*PaymentProviderInstance, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*PaymentProviderInstanceMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = _c.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (_c *PaymentProviderInstanceCreateBulk) SaveX(ctx context.Context) []*PaymentProviderInstance {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PaymentProviderInstance.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PaymentProviderInstanceUpsert) {
//			SetProviderKey(v+v).
//		}).
//		Exec(ctx)
func (_c *PaymentProviderInstanceCreateBulk) OnConflict(opts ...sql.ConflictOption) *PaymentProviderInstanceUpsertBulk {
	_c.conflict = opts
	return &PaymentProviderInstanceUpsertBulk{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *PaymentProviderInstanceCreateBulk) OnConflictColumns(columns ...string) *PaymentProviderInstanceUpsertBulk {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &PaymentProviderInstanceUpsertBulk{
		create: _c,
	}
}

// PaymentProviderInstanceUpsertBulk is the builder for "upsert"-ing
// a bulk of PaymentProviderInstance nodes.
type PaymentProviderInstanceUpsertBulk struct {
	create *PaymentProviderInstanceCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *PaymentProviderInstanceUpsertBulk) UpdateNewValues() *PaymentProviderInstanceUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(paymentproviderinstance.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PaymentProviderInstance.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *PaymentProviderInstanceUpsertBulk) Ignore() *PaymentProviderInstanceUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PaymentProviderInstanceUpsertBulk) DoNothing() *PaymentProviderInstanceUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PaymentProviderInstanceCreateBulk.OnConflict
// documentation for more info.
func (u *PaymentProviderInstanceUpsertBulk) Update(set func(*PaymentProviderInstanceUpsert)) *PaymentProviderInstanceUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PaymentProviderInstanceUpsert{UpdateSet: update})
	}))
	return u
}

// SetProviderKey sets the "provider_key" field.
func (u *PaymentProviderInstanceUpsertBulk) SetProviderKey(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetProviderKey(v)
	})
}

// UpdateProviderKey sets the "provider_key" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateProviderKey() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateProviderKey()
	})
}

// SetName sets the "name" field.
func (u *PaymentProviderInstanceUpsertBulk) SetName(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateName() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateName()
	})
}

// SetConfig sets the "config" field.
func (u *PaymentProviderInstanceUpsertBulk) SetConfig(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetConfig(v)
	})
}

// UpdateConfig sets the "config" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateConfig() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateConfig()
	})
}

// SetSupportedTypes sets the "supported_types" field.
func (u *PaymentProviderInstanceUpsertBulk) SetSupportedTypes(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetSupportedTypes(v)
	})
}

// UpdateSupportedTypes sets the "supported_types" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateSupportedTypes() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateSupportedTypes()
	})
}

// SetEnabled sets the "enabled" field.
func (u *PaymentProviderInstanceUpsertBulk) SetEnabled(v bool) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetEnabled(v)
	})
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateEnabled() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateEnabled()
	})
}

// SetPaymentMode sets the "payment_mode" field.
func (u *PaymentProviderInstanceUpsertBulk) SetPaymentMode(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetPaymentMode(v)
	})
}

// UpdatePaymentMode sets the "payment_mode" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdatePaymentMode() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdatePaymentMode()
	})
}

// SetSortOrder sets the "sort_order" field.
func (u *PaymentProviderInstanceUpsertBulk) SetSortOrder(v int) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetSortOrder(v)
	})
}

// AddSortOrder adds v to the "sort_order" field.
func (u *PaymentProviderInstanceUpsertBulk) AddSortOrder(v int) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.AddSortOrder(v)
	})
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateSortOrder() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateSortOrder()
	})
}

// SetLimits sets the "limits" field.
func (u *PaymentProviderInstanceUpsertBulk) SetLimits(v string) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetLimits(v)
	})
}

// UpdateLimits sets the "limits" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateLimits() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateLimits()
	})
}

// SetRefundEnabled sets the "refund_enabled" field.
func (u *PaymentProviderInstanceUpsertBulk) SetRefundEnabled(v bool) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetRefundEnabled(v)
	})
}

// UpdateRefundEnabled sets the "refund_enabled" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateRefundEnabled() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateRefundEnabled()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PaymentProviderInstanceUpsertBulk) SetUpdatedAt(v time.Time) *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PaymentProviderInstanceUpsertBulk) UpdateUpdatedAt() *PaymentProviderInstanceUpsertBulk {
	return u.Update(func(s *PaymentProviderInstanceUpsert) {
		s.UpdateUpdatedAt()
	})
}

// Exec executes the query.
func (u *PaymentProviderInstanceUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PaymentProviderInstanceCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PaymentProviderInstanceCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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