// 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/anzhiyu-c/anheyu-app/ent/user"
	"github.com/anzhiyu-c/anheyu-app/ent/usergroup"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// UserGroupCreate is the builder for creating a UserGroup entity.
type UserGroupCreate struct {
	config
	mutation *UserGroupMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

// SetDescription sets the "description" field.
func (_c *UserGroupCreate) SetDescription(v string) *UserGroupCreate {
	_c.mutation.SetDescription(v)
	return _c
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *UserGroupCreate) SetNillableDescription(v *string) *UserGroupCreate {
	if v != nil {
		_c.SetDescription(*v)
	}
	return _c
}

// SetPermissions sets the "permissions" field.
func (_c *UserGroupCreate) SetPermissions(v model.Boolset) *UserGroupCreate {
	_c.mutation.SetPermissions(v)
	return _c
}

// SetMaxStorage sets the "max_storage" field.
func (_c *UserGroupCreate) SetMaxStorage(v int64) *UserGroupCreate {
	_c.mutation.SetMaxStorage(v)
	return _c
}

// SetNillableMaxStorage sets the "max_storage" field if the given value is not nil.
func (_c *UserGroupCreate) SetNillableMaxStorage(v *int64) *UserGroupCreate {
	if v != nil {
		_c.SetMaxStorage(*v)
	}
	return _c
}

// SetSpeedLimit sets the "speed_limit" field.
func (_c *UserGroupCreate) SetSpeedLimit(v int64) *UserGroupCreate {
	_c.mutation.SetSpeedLimit(v)
	return _c
}

// SetNillableSpeedLimit sets the "speed_limit" field if the given value is not nil.
func (_c *UserGroupCreate) SetNillableSpeedLimit(v *int64) *UserGroupCreate {
	if v != nil {
		_c.SetSpeedLimit(*v)
	}
	return _c
}

// SetSettings sets the "settings" field.
func (_c *UserGroupCreate) SetSettings(v *model.GroupSettings) *UserGroupCreate {
	_c.mutation.SetSettings(v)
	return _c
}

// SetID sets the "id" field.
func (_c *UserGroupCreate) SetID(v uint) *UserGroupCreate {
	_c.mutation.SetID(v)
	return _c
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (_c *UserGroupCreate) AddUserIDs(ids ...uint) *UserGroupCreate {
	_c.mutation.AddUserIDs(ids...)
	return _c
}

// AddUsers adds the "users" edges to the User entity.
func (_c *UserGroupCreate) AddUsers(v ...*User) *UserGroupCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddUserIDs(ids...)
}

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

// Save creates the UserGroup in the database.
func (_c *UserGroupCreate) Save(ctx context.Context) (*UserGroup, 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 *UserGroupCreate) SaveX(ctx context.Context) *UserGroup {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *UserGroupCreate) 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 *UserGroupCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if usergroup.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized usergroup.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := usergroup.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if usergroup.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized usergroup.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := usergroup.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.MaxStorage(); !ok {
		v := usergroup.DefaultMaxStorage
		_c.mutation.SetMaxStorage(v)
	}
	if _, ok := _c.mutation.SpeedLimit(); !ok {
		v := usergroup.DefaultSpeedLimit
		_c.mutation.SetSpeedLimit(v)
	}
	if _, ok := _c.mutation.Settings(); !ok {
		v := usergroup.DefaultSettings
		_c.mutation.SetSettings(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *UserGroupCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "UserGroup.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "UserGroup.updated_at"`)}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "UserGroup.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := usergroup.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "UserGroup.name": %w`, err)}
		}
	}
	if v, ok := _c.mutation.Description(); ok {
		if err := usergroup.DescriptionValidator(v); err != nil {
			return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "UserGroup.description": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Permissions(); !ok {
		return &ValidationError{Name: "permissions", err: errors.New(`ent: missing required field "UserGroup.permissions"`)}
	}
	if _, ok := _c.mutation.MaxStorage(); !ok {
		return &ValidationError{Name: "max_storage", err: errors.New(`ent: missing required field "UserGroup.max_storage"`)}
	}
	if _, ok := _c.mutation.SpeedLimit(); !ok {
		return &ValidationError{Name: "speed_limit", err: errors.New(`ent: missing required field "UserGroup.speed_limit"`)}
	}
	if _, ok := _c.mutation.Settings(); !ok {
		return &ValidationError{Name: "settings", err: errors.New(`ent: missing required field "UserGroup.settings"`)}
	}
	return nil
}

func (_c *UserGroupCreate) sqlSave(ctx context.Context) (*UserGroup, 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
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *UserGroupCreate) createSpec() (*UserGroup, *sqlgraph.CreateSpec) {
	var (
		_node = &UserGroup{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(usergroup.Table, sqlgraph.NewFieldSpec(usergroup.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(usergroup.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(usergroup.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(usergroup.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(usergroup.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(usergroup.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := _c.mutation.Permissions(); ok {
		_spec.SetField(usergroup.FieldPermissions, field.TypeOther, value)
		_node.Permissions = value
	}
	if value, ok := _c.mutation.MaxStorage(); ok {
		_spec.SetField(usergroup.FieldMaxStorage, field.TypeInt64, value)
		_node.MaxStorage = value
	}
	if value, ok := _c.mutation.SpeedLimit(); ok {
		_spec.SetField(usergroup.FieldSpeedLimit, field.TypeInt64, value)
		_node.SpeedLimit = value
	}
	if value, ok := _c.mutation.Settings(); ok {
		_spec.SetField(usergroup.FieldSettings, field.TypeOther, value)
		_node.Settings = value
	}
	if nodes := _c.mutation.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		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.UserGroup.Create().
//		SetDeletedAt(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.UserGroupUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *UserGroupCreate) OnConflict(opts ...sql.ConflictOption) *UserGroupUpsertOne {
	_c.conflict = opts
	return &UserGroupUpsertOne{
		create: _c,
	}
}

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

type (
	// UserGroupUpsertOne is the builder for "upsert"-ing
	//  one UserGroup node.
	UserGroupUpsertOne struct {
		create *UserGroupCreate
	}

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

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

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

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

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

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

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

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

// SetDescription sets the "description" field.
func (u *UserGroupUpsert) SetDescription(v string) *UserGroupUpsert {
	u.Set(usergroup.FieldDescription, v)
	return u
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *UserGroupUpsert) UpdateDescription() *UserGroupUpsert {
	u.SetExcluded(usergroup.FieldDescription)
	return u
}

// ClearDescription clears the value of the "description" field.
func (u *UserGroupUpsert) ClearDescription() *UserGroupUpsert {
	u.SetNull(usergroup.FieldDescription)
	return u
}

// SetPermissions sets the "permissions" field.
func (u *UserGroupUpsert) SetPermissions(v model.Boolset) *UserGroupUpsert {
	u.Set(usergroup.FieldPermissions, v)
	return u
}

// UpdatePermissions sets the "permissions" field to the value that was provided on create.
func (u *UserGroupUpsert) UpdatePermissions() *UserGroupUpsert {
	u.SetExcluded(usergroup.FieldPermissions)
	return u
}

// SetMaxStorage sets the "max_storage" field.
func (u *UserGroupUpsert) SetMaxStorage(v int64) *UserGroupUpsert {
	u.Set(usergroup.FieldMaxStorage, v)
	return u
}

// UpdateMaxStorage sets the "max_storage" field to the value that was provided on create.
func (u *UserGroupUpsert) UpdateMaxStorage() *UserGroupUpsert {
	u.SetExcluded(usergroup.FieldMaxStorage)
	return u
}

// AddMaxStorage adds v to the "max_storage" field.
func (u *UserGroupUpsert) AddMaxStorage(v int64) *UserGroupUpsert {
	u.Add(usergroup.FieldMaxStorage, v)
	return u
}

// SetSpeedLimit sets the "speed_limit" field.
func (u *UserGroupUpsert) SetSpeedLimit(v int64) *UserGroupUpsert {
	u.Set(usergroup.FieldSpeedLimit, v)
	return u
}

// UpdateSpeedLimit sets the "speed_limit" field to the value that was provided on create.
func (u *UserGroupUpsert) UpdateSpeedLimit() *UserGroupUpsert {
	u.SetExcluded(usergroup.FieldSpeedLimit)
	return u
}

// AddSpeedLimit adds v to the "speed_limit" field.
func (u *UserGroupUpsert) AddSpeedLimit(v int64) *UserGroupUpsert {
	u.Add(usergroup.FieldSpeedLimit, v)
	return u
}

// SetSettings sets the "settings" field.
func (u *UserGroupUpsert) SetSettings(v *model.GroupSettings) *UserGroupUpsert {
	u.Set(usergroup.FieldSettings, v)
	return u
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *UserGroupUpsert) UpdateSettings() *UserGroupUpsert {
	u.SetExcluded(usergroup.FieldSettings)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.UserGroup.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(usergroup.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *UserGroupUpsertOne) UpdateNewValues() *UserGroupUpsertOne {
	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.ID(); exists {
			s.SetIgnore(usergroup.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(usergroup.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

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

// SetDescription sets the "description" field.
func (u *UserGroupUpsertOne) SetDescription(v string) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *UserGroupUpsertOne) UpdateDescription() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *UserGroupUpsertOne) ClearDescription() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.ClearDescription()
	})
}

// SetPermissions sets the "permissions" field.
func (u *UserGroupUpsertOne) SetPermissions(v model.Boolset) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetPermissions(v)
	})
}

// UpdatePermissions sets the "permissions" field to the value that was provided on create.
func (u *UserGroupUpsertOne) UpdatePermissions() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdatePermissions()
	})
}

// SetMaxStorage sets the "max_storage" field.
func (u *UserGroupUpsertOne) SetMaxStorage(v int64) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetMaxStorage(v)
	})
}

// AddMaxStorage adds v to the "max_storage" field.
func (u *UserGroupUpsertOne) AddMaxStorage(v int64) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.AddMaxStorage(v)
	})
}

// UpdateMaxStorage sets the "max_storage" field to the value that was provided on create.
func (u *UserGroupUpsertOne) UpdateMaxStorage() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateMaxStorage()
	})
}

// SetSpeedLimit sets the "speed_limit" field.
func (u *UserGroupUpsertOne) SetSpeedLimit(v int64) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetSpeedLimit(v)
	})
}

// AddSpeedLimit adds v to the "speed_limit" field.
func (u *UserGroupUpsertOne) AddSpeedLimit(v int64) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.AddSpeedLimit(v)
	})
}

// UpdateSpeedLimit sets the "speed_limit" field to the value that was provided on create.
func (u *UserGroupUpsertOne) UpdateSpeedLimit() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateSpeedLimit()
	})
}

// SetSettings sets the "settings" field.
func (u *UserGroupUpsertOne) SetSettings(v *model.GroupSettings) *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetSettings(v)
	})
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *UserGroupUpsertOne) UpdateSettings() *UserGroupUpsertOne {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateSettings()
	})
}

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

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

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

// Save creates the UserGroup entities in the database.
func (_c *UserGroupCreateBulk) Save(ctx context.Context) ([]*UserGroup, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*UserGroup, 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.(*UserGroupMutation)
				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 && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint(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 *UserGroupCreateBulk) SaveX(ctx context.Context) []*UserGroup {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

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

// UserGroupUpsertBulk is the builder for "upsert"-ing
// a bulk of UserGroup nodes.
type UserGroupUpsertBulk struct {
	create *UserGroupCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.UserGroup.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(usergroup.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *UserGroupUpsertBulk) UpdateNewValues() *UserGroupUpsertBulk {
	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.ID(); exists {
				s.SetIgnore(usergroup.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(usergroup.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

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

// SetDescription sets the "description" field.
func (u *UserGroupUpsertBulk) SetDescription(v string) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *UserGroupUpsertBulk) UpdateDescription() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *UserGroupUpsertBulk) ClearDescription() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.ClearDescription()
	})
}

// SetPermissions sets the "permissions" field.
func (u *UserGroupUpsertBulk) SetPermissions(v model.Boolset) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetPermissions(v)
	})
}

// UpdatePermissions sets the "permissions" field to the value that was provided on create.
func (u *UserGroupUpsertBulk) UpdatePermissions() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdatePermissions()
	})
}

// SetMaxStorage sets the "max_storage" field.
func (u *UserGroupUpsertBulk) SetMaxStorage(v int64) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetMaxStorage(v)
	})
}

// AddMaxStorage adds v to the "max_storage" field.
func (u *UserGroupUpsertBulk) AddMaxStorage(v int64) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.AddMaxStorage(v)
	})
}

// UpdateMaxStorage sets the "max_storage" field to the value that was provided on create.
func (u *UserGroupUpsertBulk) UpdateMaxStorage() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateMaxStorage()
	})
}

// SetSpeedLimit sets the "speed_limit" field.
func (u *UserGroupUpsertBulk) SetSpeedLimit(v int64) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetSpeedLimit(v)
	})
}

// AddSpeedLimit adds v to the "speed_limit" field.
func (u *UserGroupUpsertBulk) AddSpeedLimit(v int64) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.AddSpeedLimit(v)
	})
}

// UpdateSpeedLimit sets the "speed_limit" field to the value that was provided on create.
func (u *UserGroupUpsertBulk) UpdateSpeedLimit() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateSpeedLimit()
	})
}

// SetSettings sets the "settings" field.
func (u *UserGroupUpsertBulk) SetSettings(v *model.GroupSettings) *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.SetSettings(v)
	})
}

// UpdateSettings sets the "settings" field to the value that was provided on create.
func (u *UserGroupUpsertBulk) UpdateSettings() *UserGroupUpsertBulk {
	return u.Update(func(s *UserGroupUpsert) {
		s.UpdateSettings()
	})
}

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

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