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

// ProxyCreate is the builder for creating a Proxy entity.
type ProxyCreate struct {
	config
	mutation *ProxyMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

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

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

// SetPort sets the "port" field.
func (_c *ProxyCreate) SetPort(v int) *ProxyCreate {
	_c.mutation.SetPort(v)
	return _c
}

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

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

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

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

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

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

// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_c *ProxyCreate) AddAccountIDs(ids ...int64) *ProxyCreate {
	_c.mutation.AddAccountIDs(ids...)
	return _c
}

// AddAccounts adds the "accounts" edges to the Account entity.
func (_c *ProxyCreate) AddAccounts(v ...*Account) *ProxyCreate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddAccountIDs(ids...)
}

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

// Save creates the Proxy in the database.
func (_c *ProxyCreate) Save(ctx context.Context) (*Proxy, error) {
	if err := _c.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_c *ProxyCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Proxy.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Proxy.updated_at"`)}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Proxy.name"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Protocol(); !ok {
		return &ValidationError{Name: "protocol", err: errors.New(`ent: missing required field "Proxy.protocol"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Host(); !ok {
		return &ValidationError{Name: "host", err: errors.New(`ent: missing required field "Proxy.host"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Port(); !ok {
		return &ValidationError{Name: "port", err: errors.New(`ent: missing required field "Proxy.port"`)}
	}
	if v, ok := _c.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 := _c.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 _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Proxy.status"`)}
	}
	if v, ok := _c.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 (_c *ProxyCreate) sqlSave(ctx context.Context) (*Proxy, 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 *ProxyCreate) createSpec() (*Proxy, *sqlgraph.CreateSpec) {
	var (
		_node = &Proxy{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(proxy.Table, sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(proxy.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(proxy.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(proxy.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(proxy.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Protocol(); ok {
		_spec.SetField(proxy.FieldProtocol, field.TypeString, value)
		_node.Protocol = value
	}
	if value, ok := _c.mutation.Host(); ok {
		_spec.SetField(proxy.FieldHost, field.TypeString, value)
		_node.Host = value
	}
	if value, ok := _c.mutation.Port(); ok {
		_spec.SetField(proxy.FieldPort, field.TypeInt, value)
		_node.Port = value
	}
	if value, ok := _c.mutation.Username(); ok {
		_spec.SetField(proxy.FieldUsername, field.TypeString, value)
		_node.Username = &value
	}
	if value, ok := _c.mutation.Password(); ok {
		_spec.SetField(proxy.FieldPassword, field.TypeString, value)
		_node.Password = &value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(proxy.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if nodes := _c.mutation.AccountsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   proxy.AccountsTable,
			Columns: []string{proxy.AccountsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Proxy.Create().
//		SetCreatedAt(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.ProxyUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *ProxyCreate) OnConflict(opts ...sql.ConflictOption) *ProxyUpsertOne {
	_c.conflict = opts
	return &ProxyUpsertOne{
		create: _c,
	}
}

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

type (
	// ProxyUpsertOne is the builder for "upsert"-ing
	//  one Proxy node.
	ProxyUpsertOne struct {
		create *ProxyCreate
	}

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

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

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

// SetDeletedAt sets the "deleted_at" field.
func (u *ProxyUpsert) SetDeletedAt(v time.Time) *ProxyUpsert {
	u.Set(proxy.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ProxyUpsert) UpdateDeletedAt() *ProxyUpsert {
	u.SetExcluded(proxy.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ProxyUpsert) ClearDeletedAt() *ProxyUpsert {
	u.SetNull(proxy.FieldDeletedAt)
	return u
}

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

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

// SetProtocol sets the "protocol" field.
func (u *ProxyUpsert) SetProtocol(v string) *ProxyUpsert {
	u.Set(proxy.FieldProtocol, v)
	return u
}

// UpdateProtocol sets the "protocol" field to the value that was provided on create.
func (u *ProxyUpsert) UpdateProtocol() *ProxyUpsert {
	u.SetExcluded(proxy.FieldProtocol)
	return u
}

// SetHost sets the "host" field.
func (u *ProxyUpsert) SetHost(v string) *ProxyUpsert {
	u.Set(proxy.FieldHost, v)
	return u
}

// UpdateHost sets the "host" field to the value that was provided on create.
func (u *ProxyUpsert) UpdateHost() *ProxyUpsert {
	u.SetExcluded(proxy.FieldHost)
	return u
}

// SetPort sets the "port" field.
func (u *ProxyUpsert) SetPort(v int) *ProxyUpsert {
	u.Set(proxy.FieldPort, v)
	return u
}

// UpdatePort sets the "port" field to the value that was provided on create.
func (u *ProxyUpsert) UpdatePort() *ProxyUpsert {
	u.SetExcluded(proxy.FieldPort)
	return u
}

// AddPort adds v to the "port" field.
func (u *ProxyUpsert) AddPort(v int) *ProxyUpsert {
	u.Add(proxy.FieldPort, v)
	return u
}

// SetUsername sets the "username" field.
func (u *ProxyUpsert) SetUsername(v string) *ProxyUpsert {
	u.Set(proxy.FieldUsername, v)
	return u
}

// UpdateUsername sets the "username" field to the value that was provided on create.
func (u *ProxyUpsert) UpdateUsername() *ProxyUpsert {
	u.SetExcluded(proxy.FieldUsername)
	return u
}

// ClearUsername clears the value of the "username" field.
func (u *ProxyUpsert) ClearUsername() *ProxyUpsert {
	u.SetNull(proxy.FieldUsername)
	return u
}

// SetPassword sets the "password" field.
func (u *ProxyUpsert) SetPassword(v string) *ProxyUpsert {
	u.Set(proxy.FieldPassword, v)
	return u
}

// UpdatePassword sets the "password" field to the value that was provided on create.
func (u *ProxyUpsert) UpdatePassword() *ProxyUpsert {
	u.SetExcluded(proxy.FieldPassword)
	return u
}

// ClearPassword clears the value of the "password" field.
func (u *ProxyUpsert) ClearPassword() *ProxyUpsert {
	u.SetNull(proxy.FieldPassword)
	return u
}

// SetStatus sets the "status" field.
func (u *ProxyUpsert) SetStatus(v string) *ProxyUpsert {
	u.Set(proxy.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ProxyUpsert) UpdateStatus() *ProxyUpsert {
	u.SetExcluded(proxy.FieldStatus)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Proxy.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *ProxyUpsertOne) UpdateNewValues() *ProxyUpsertOne {
	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(proxy.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Proxy.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *ProxyUpsertOne) Ignore() *ProxyUpsertOne {
	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 *ProxyUpsertOne) DoNothing() *ProxyUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetDeletedAt sets the "deleted_at" field.
func (u *ProxyUpsertOne) SetDeletedAt(v time.Time) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdateDeletedAt() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ProxyUpsertOne) ClearDeletedAt() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearDeletedAt()
	})
}

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

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

// SetProtocol sets the "protocol" field.
func (u *ProxyUpsertOne) SetProtocol(v string) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetProtocol(v)
	})
}

// UpdateProtocol sets the "protocol" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdateProtocol() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateProtocol()
	})
}

// SetHost sets the "host" field.
func (u *ProxyUpsertOne) SetHost(v string) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetHost(v)
	})
}

// UpdateHost sets the "host" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdateHost() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateHost()
	})
}

// SetPort sets the "port" field.
func (u *ProxyUpsertOne) SetPort(v int) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetPort(v)
	})
}

// AddPort adds v to the "port" field.
func (u *ProxyUpsertOne) AddPort(v int) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.AddPort(v)
	})
}

// UpdatePort sets the "port" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdatePort() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdatePort()
	})
}

// SetUsername sets the "username" field.
func (u *ProxyUpsertOne) SetUsername(v string) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetUsername(v)
	})
}

// UpdateUsername sets the "username" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdateUsername() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateUsername()
	})
}

// ClearUsername clears the value of the "username" field.
func (u *ProxyUpsertOne) ClearUsername() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearUsername()
	})
}

// SetPassword sets the "password" field.
func (u *ProxyUpsertOne) SetPassword(v string) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetPassword(v)
	})
}

// UpdatePassword sets the "password" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdatePassword() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdatePassword()
	})
}

// ClearPassword clears the value of the "password" field.
func (u *ProxyUpsertOne) ClearPassword() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearPassword()
	})
}

// SetStatus sets the "status" field.
func (u *ProxyUpsertOne) SetStatus(v string) *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ProxyUpsertOne) UpdateStatus() *ProxyUpsertOne {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateStatus()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *ProxyUpsertOne) 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 *ProxyUpsertOne) 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 *ProxyUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the Proxy entities in the database.
func (_c *ProxyCreateBulk) Save(ctx context.Context) ([]*Proxy, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Proxy, 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.(*ProxyMutation)
				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 *ProxyCreateBulk) SaveX(ctx context.Context) []*Proxy {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *ProxyCreateBulk) 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.Proxy.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.ProxyUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *ProxyCreateBulk) OnConflict(opts ...sql.ConflictOption) *ProxyUpsertBulk {
	_c.conflict = opts
	return &ProxyUpsertBulk{
		create: _c,
	}
}

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

// ProxyUpsertBulk is the builder for "upsert"-ing
// a bulk of Proxy nodes.
type ProxyUpsertBulk struct {
	create *ProxyCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Proxy.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *ProxyUpsertBulk) UpdateNewValues() *ProxyUpsertBulk {
	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(proxy.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Proxy.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *ProxyUpsertBulk) Ignore() *ProxyUpsertBulk {
	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 *ProxyUpsertBulk) DoNothing() *ProxyUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetDeletedAt sets the "deleted_at" field.
func (u *ProxyUpsertBulk) SetDeletedAt(v time.Time) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdateDeletedAt() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ProxyUpsertBulk) ClearDeletedAt() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearDeletedAt()
	})
}

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

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

// SetProtocol sets the "protocol" field.
func (u *ProxyUpsertBulk) SetProtocol(v string) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetProtocol(v)
	})
}

// UpdateProtocol sets the "protocol" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdateProtocol() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateProtocol()
	})
}

// SetHost sets the "host" field.
func (u *ProxyUpsertBulk) SetHost(v string) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetHost(v)
	})
}

// UpdateHost sets the "host" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdateHost() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateHost()
	})
}

// SetPort sets the "port" field.
func (u *ProxyUpsertBulk) SetPort(v int) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetPort(v)
	})
}

// AddPort adds v to the "port" field.
func (u *ProxyUpsertBulk) AddPort(v int) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.AddPort(v)
	})
}

// UpdatePort sets the "port" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdatePort() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdatePort()
	})
}

// SetUsername sets the "username" field.
func (u *ProxyUpsertBulk) SetUsername(v string) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetUsername(v)
	})
}

// UpdateUsername sets the "username" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdateUsername() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateUsername()
	})
}

// ClearUsername clears the value of the "username" field.
func (u *ProxyUpsertBulk) ClearUsername() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearUsername()
	})
}

// SetPassword sets the "password" field.
func (u *ProxyUpsertBulk) SetPassword(v string) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetPassword(v)
	})
}

// UpdatePassword sets the "password" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdatePassword() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdatePassword()
	})
}

// ClearPassword clears the value of the "password" field.
func (u *ProxyUpsertBulk) ClearPassword() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.ClearPassword()
	})
}

// SetStatus sets the "status" field.
func (u *ProxyUpsertBulk) SetStatus(v string) *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ProxyUpsertBulk) UpdateStatus() *ProxyUpsertBulk {
	return u.Update(func(s *ProxyUpsert) {
		s.UpdateStatus()
	})
}

// Exec executes the query.
func (u *ProxyUpsertBulk) 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 ProxyCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for ProxyCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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