// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/group"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/groupmembership"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
	"github.com/google/uuid"
)

// GroupMembershipCreate is the builder for creating a GroupMembership entity.
type GroupMembershipCreate struct {
	config
	mutation *GroupMembershipMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetGroupID sets the "group_id" field.
func (gmc *GroupMembershipCreate) SetGroupID(u uuid.UUID) *GroupMembershipCreate {
	gmc.mutation.SetGroupID(u)
	return gmc
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (gmc *GroupMembershipCreate) SetNillableGroupID(u *uuid.UUID) *GroupMembershipCreate {
	if u != nil {
		gmc.SetGroupID(*u)
	}
	return gmc
}

// SetUserID sets the "user_id" field.
func (gmc *GroupMembershipCreate) SetUserID(u uuid.UUID) *GroupMembershipCreate {
	gmc.mutation.SetUserID(u)
	return gmc
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (gmc *GroupMembershipCreate) SetNillableUserID(u *uuid.UUID) *GroupMembershipCreate {
	if u != nil {
		gmc.SetUserID(*u)
	}
	return gmc
}

// SetMaintainer sets the "maintainer" field.
func (gmc *GroupMembershipCreate) SetMaintainer(b bool) *GroupMembershipCreate {
	gmc.mutation.SetMaintainer(b)
	return gmc
}

// SetNillableMaintainer sets the "maintainer" field if the given value is not nil.
func (gmc *GroupMembershipCreate) SetNillableMaintainer(b *bool) *GroupMembershipCreate {
	if b != nil {
		gmc.SetMaintainer(*b)
	}
	return gmc
}

// SetCreatedAt sets the "created_at" field.
func (gmc *GroupMembershipCreate) SetCreatedAt(t time.Time) *GroupMembershipCreate {
	gmc.mutation.SetCreatedAt(t)
	return gmc
}

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

// SetUpdatedAt sets the "updated_at" field.
func (gmc *GroupMembershipCreate) SetUpdatedAt(t time.Time) *GroupMembershipCreate {
	gmc.mutation.SetUpdatedAt(t)
	return gmc
}

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

// SetDeletedAt sets the "deleted_at" field.
func (gmc *GroupMembershipCreate) SetDeletedAt(t time.Time) *GroupMembershipCreate {
	gmc.mutation.SetDeletedAt(t)
	return gmc
}

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

// SetID sets the "id" field.
func (gmc *GroupMembershipCreate) SetID(u uuid.UUID) *GroupMembershipCreate {
	gmc.mutation.SetID(u)
	return gmc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (gmc *GroupMembershipCreate) SetNillableID(u *uuid.UUID) *GroupMembershipCreate {
	if u != nil {
		gmc.SetID(*u)
	}
	return gmc
}

// SetGroup sets the "group" edge to the Group entity.
func (gmc *GroupMembershipCreate) SetGroup(g *Group) *GroupMembershipCreate {
	return gmc.SetGroupID(g.ID)
}

// SetUser sets the "user" edge to the User entity.
func (gmc *GroupMembershipCreate) SetUser(u *User) *GroupMembershipCreate {
	return gmc.SetUserID(u.ID)
}

// Mutation returns the GroupMembershipMutation object of the builder.
func (gmc *GroupMembershipCreate) Mutation() *GroupMembershipMutation {
	return gmc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (gmc *GroupMembershipCreate) defaults() {
	if _, ok := gmc.mutation.GroupID(); !ok {
		v := groupmembership.DefaultGroupID()
		gmc.mutation.SetGroupID(v)
	}
	if _, ok := gmc.mutation.UserID(); !ok {
		v := groupmembership.DefaultUserID()
		gmc.mutation.SetUserID(v)
	}
	if _, ok := gmc.mutation.Maintainer(); !ok {
		v := groupmembership.DefaultMaintainer
		gmc.mutation.SetMaintainer(v)
	}
	if _, ok := gmc.mutation.CreatedAt(); !ok {
		v := groupmembership.DefaultCreatedAt()
		gmc.mutation.SetCreatedAt(v)
	}
	if _, ok := gmc.mutation.UpdatedAt(); !ok {
		v := groupmembership.DefaultUpdatedAt()
		gmc.mutation.SetUpdatedAt(v)
	}
	if _, ok := gmc.mutation.ID(); !ok {
		v := groupmembership.DefaultID()
		gmc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (gmc *GroupMembershipCreate) check() error {
	if _, ok := gmc.mutation.GroupID(); !ok {
		return &ValidationError{Name: "group_id", err: errors.New(`ent: missing required field "GroupMembership.group_id"`)}
	}
	if _, ok := gmc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "GroupMembership.user_id"`)}
	}
	if _, ok := gmc.mutation.Maintainer(); !ok {
		return &ValidationError{Name: "maintainer", err: errors.New(`ent: missing required field "GroupMembership.maintainer"`)}
	}
	if _, ok := gmc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "GroupMembership.created_at"`)}
	}
	if _, ok := gmc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "GroupMembership.updated_at"`)}
	}
	if len(gmc.mutation.GroupIDs()) == 0 {
		return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "GroupMembership.group"`)}
	}
	if len(gmc.mutation.UserIDs()) == 0 {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "GroupMembership.user"`)}
	}
	return nil
}

func (gmc *GroupMembershipCreate) sqlSave(ctx context.Context) (*GroupMembership, error) {
	if err := gmc.check(); err != nil {
		return nil, err
	}
	_node, _spec := gmc.createSpec()
	if err := sqlgraph.CreateNode(ctx, gmc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	gmc.mutation.id = &_node.ID
	gmc.mutation.done = true
	return _node, nil
}

func (gmc *GroupMembershipCreate) createSpec() (*GroupMembership, *sqlgraph.CreateSpec) {
	var (
		_node = &GroupMembership{config: gmc.config}
		_spec = sqlgraph.NewCreateSpec(groupmembership.Table, sqlgraph.NewFieldSpec(groupmembership.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = gmc.conflict
	if id, ok := gmc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := gmc.mutation.Maintainer(); ok {
		_spec.SetField(groupmembership.FieldMaintainer, field.TypeBool, value)
		_node.Maintainer = value
	}
	if value, ok := gmc.mutation.CreatedAt(); ok {
		_spec.SetField(groupmembership.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := gmc.mutation.UpdatedAt(); ok {
		_spec.SetField(groupmembership.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := gmc.mutation.DeletedAt(); ok {
		_spec.SetField(groupmembership.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if nodes := gmc.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.GroupTable,
			Columns: []string{groupmembership.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.GroupID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := gmc.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.UserTable,
			Columns: []string{groupmembership.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_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.GroupMembership.Create().
//		SetGroupID(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.GroupMembershipUpsert) {
//			SetGroupID(v+v).
//		}).
//		Exec(ctx)
func (gmc *GroupMembershipCreate) OnConflict(opts ...sql.ConflictOption) *GroupMembershipUpsertOne {
	gmc.conflict = opts
	return &GroupMembershipUpsertOne{
		create: gmc,
	}
}

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

type (
	// GroupMembershipUpsertOne is the builder for "upsert"-ing
	//  one GroupMembership node.
	GroupMembershipUpsertOne struct {
		create *GroupMembershipCreate
	}

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

// SetGroupID sets the "group_id" field.
func (u *GroupMembershipUpsert) SetGroupID(v uuid.UUID) *GroupMembershipUpsert {
	u.Set(groupmembership.FieldGroupID, v)
	return u
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *GroupMembershipUpsert) UpdateGroupID() *GroupMembershipUpsert {
	u.SetExcluded(groupmembership.FieldGroupID)
	return u
}

// SetUserID sets the "user_id" field.
func (u *GroupMembershipUpsert) SetUserID(v uuid.UUID) *GroupMembershipUpsert {
	u.Set(groupmembership.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *GroupMembershipUpsert) UpdateUserID() *GroupMembershipUpsert {
	u.SetExcluded(groupmembership.FieldUserID)
	return u
}

// SetMaintainer sets the "maintainer" field.
func (u *GroupMembershipUpsert) SetMaintainer(v bool) *GroupMembershipUpsert {
	u.Set(groupmembership.FieldMaintainer, v)
	return u
}

// UpdateMaintainer sets the "maintainer" field to the value that was provided on create.
func (u *GroupMembershipUpsert) UpdateMaintainer() *GroupMembershipUpsert {
	u.SetExcluded(groupmembership.FieldMaintainer)
	return u
}

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

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *GroupMembershipUpsert) ClearDeletedAt() *GroupMembershipUpsert {
	u.SetNull(groupmembership.FieldDeletedAt)
	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.GroupMembership.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(groupmembership.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *GroupMembershipUpsertOne) UpdateNewValues() *GroupMembershipUpsertOne {
	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(groupmembership.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(groupmembership.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetGroupID sets the "group_id" field.
func (u *GroupMembershipUpsertOne) SetGroupID(v uuid.UUID) *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *GroupMembershipUpsertOne) UpdateGroupID() *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateGroupID()
	})
}

// SetUserID sets the "user_id" field.
func (u *GroupMembershipUpsertOne) SetUserID(v uuid.UUID) *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *GroupMembershipUpsertOne) UpdateUserID() *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateUserID()
	})
}

// SetMaintainer sets the "maintainer" field.
func (u *GroupMembershipUpsertOne) SetMaintainer(v bool) *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetMaintainer(v)
	})
}

// UpdateMaintainer sets the "maintainer" field to the value that was provided on create.
func (u *GroupMembershipUpsertOne) UpdateMaintainer() *GroupMembershipUpsertOne {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateMaintainer()
	})
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (u *GroupMembershipUpsertOne) 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 *GroupMembershipUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: GroupMembershipUpsertOne.ID is not supported by MySQL driver. Use GroupMembershipUpsertOne.Exec instead")
	}
	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 *GroupMembershipUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the GroupMembership entities in the database.
func (gmcb *GroupMembershipCreateBulk) Save(ctx context.Context) ([]*GroupMembership, error) {
	if gmcb.err != nil {
		return nil, gmcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(gmcb.builders))
	nodes := make([]*GroupMembership, len(gmcb.builders))
	mutators := make([]Mutator, len(gmcb.builders))
	for i := range gmcb.builders {
		func(i int, root context.Context) {
			builder := gmcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*GroupMembershipMutation)
				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, gmcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = gmcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, gmcb.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
				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, gmcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.GroupMembership.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.GroupMembershipUpsert) {
//			SetGroupID(v+v).
//		}).
//		Exec(ctx)
func (gmcb *GroupMembershipCreateBulk) OnConflict(opts ...sql.ConflictOption) *GroupMembershipUpsertBulk {
	gmcb.conflict = opts
	return &GroupMembershipUpsertBulk{
		create: gmcb,
	}
}

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

// GroupMembershipUpsertBulk is the builder for "upsert"-ing
// a bulk of GroupMembership nodes.
type GroupMembershipUpsertBulk struct {
	create *GroupMembershipCreateBulk
}

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

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

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

// SetGroupID sets the "group_id" field.
func (u *GroupMembershipUpsertBulk) SetGroupID(v uuid.UUID) *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *GroupMembershipUpsertBulk) UpdateGroupID() *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateGroupID()
	})
}

// SetUserID sets the "user_id" field.
func (u *GroupMembershipUpsertBulk) SetUserID(v uuid.UUID) *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *GroupMembershipUpsertBulk) UpdateUserID() *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateUserID()
	})
}

// SetMaintainer sets the "maintainer" field.
func (u *GroupMembershipUpsertBulk) SetMaintainer(v bool) *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.SetMaintainer(v)
	})
}

// UpdateMaintainer sets the "maintainer" field to the value that was provided on create.
func (u *GroupMembershipUpsertBulk) UpdateMaintainer() *GroupMembershipUpsertBulk {
	return u.Update(func(s *GroupMembershipUpsert) {
		s.UpdateMaintainer()
	})
}

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

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

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

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

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

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

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