// 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/authz"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/orginvitation"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
	"github.com/google/uuid"
)

// OrgInvitationCreate is the builder for creating a OrgInvitation entity.
type OrgInvitationCreate struct {
	config
	mutation *OrgInvitationMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetReceiverEmail sets the "receiver_email" field.
func (oic *OrgInvitationCreate) SetReceiverEmail(s string) *OrgInvitationCreate {
	oic.mutation.SetReceiverEmail(s)
	return oic
}

// SetStatus sets the "status" field.
func (oic *OrgInvitationCreate) SetStatus(bis biz.OrgInvitationStatus) *OrgInvitationCreate {
	oic.mutation.SetStatus(bis)
	return oic
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (oic *OrgInvitationCreate) SetNillableStatus(bis *biz.OrgInvitationStatus) *OrgInvitationCreate {
	if bis != nil {
		oic.SetStatus(*bis)
	}
	return oic
}

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (oic *OrgInvitationCreate) SetOrganizationID(u uuid.UUID) *OrgInvitationCreate {
	oic.mutation.SetOrganizationID(u)
	return oic
}

// SetSenderID sets the "sender_id" field.
func (oic *OrgInvitationCreate) SetSenderID(u uuid.UUID) *OrgInvitationCreate {
	oic.mutation.SetSenderID(u)
	return oic
}

// SetRole sets the "role" field.
func (oic *OrgInvitationCreate) SetRole(a authz.Role) *OrgInvitationCreate {
	oic.mutation.SetRole(a)
	return oic
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (oic *OrgInvitationCreate) SetNillableRole(a *authz.Role) *OrgInvitationCreate {
	if a != nil {
		oic.SetRole(*a)
	}
	return oic
}

// SetContext sets the "context" field.
func (oic *OrgInvitationCreate) SetContext(bic biz.OrgInvitationContext) *OrgInvitationCreate {
	oic.mutation.SetContext(bic)
	return oic
}

// SetNillableContext sets the "context" field if the given value is not nil.
func (oic *OrgInvitationCreate) SetNillableContext(bic *biz.OrgInvitationContext) *OrgInvitationCreate {
	if bic != nil {
		oic.SetContext(*bic)
	}
	return oic
}

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

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

// SetOrganization sets the "organization" edge to the Organization entity.
func (oic *OrgInvitationCreate) SetOrganization(o *Organization) *OrgInvitationCreate {
	return oic.SetOrganizationID(o.ID)
}

// SetSender sets the "sender" edge to the User entity.
func (oic *OrgInvitationCreate) SetSender(u *User) *OrgInvitationCreate {
	return oic.SetSenderID(u.ID)
}

// Mutation returns the OrgInvitationMutation object of the builder.
func (oic *OrgInvitationCreate) Mutation() *OrgInvitationMutation {
	return oic.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (oic *OrgInvitationCreate) defaults() {
	if _, ok := oic.mutation.Status(); !ok {
		v := orginvitation.DefaultStatus
		oic.mutation.SetStatus(v)
	}
	if _, ok := oic.mutation.CreatedAt(); !ok {
		v := orginvitation.DefaultCreatedAt()
		oic.mutation.SetCreatedAt(v)
	}
	if _, ok := oic.mutation.ID(); !ok {
		v := orginvitation.DefaultID()
		oic.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (oic *OrgInvitationCreate) check() error {
	if _, ok := oic.mutation.ReceiverEmail(); !ok {
		return &ValidationError{Name: "receiver_email", err: errors.New(`ent: missing required field "OrgInvitation.receiver_email"`)}
	}
	if _, ok := oic.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "OrgInvitation.status"`)}
	}
	if v, ok := oic.mutation.Status(); ok {
		if err := orginvitation.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "OrgInvitation.status": %w`, err)}
		}
	}
	if _, ok := oic.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "OrgInvitation.created_at"`)}
	}
	if _, ok := oic.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "OrgInvitation.organization_id"`)}
	}
	if _, ok := oic.mutation.SenderID(); !ok {
		return &ValidationError{Name: "sender_id", err: errors.New(`ent: missing required field "OrgInvitation.sender_id"`)}
	}
	if v, ok := oic.mutation.Role(); ok {
		if err := orginvitation.RoleValidator(v); err != nil {
			return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "OrgInvitation.role": %w`, err)}
		}
	}
	if len(oic.mutation.OrganizationIDs()) == 0 {
		return &ValidationError{Name: "organization", err: errors.New(`ent: missing required edge "OrgInvitation.organization"`)}
	}
	if len(oic.mutation.SenderIDs()) == 0 {
		return &ValidationError{Name: "sender", err: errors.New(`ent: missing required edge "OrgInvitation.sender"`)}
	}
	return nil
}

func (oic *OrgInvitationCreate) sqlSave(ctx context.Context) (*OrgInvitation, error) {
	if err := oic.check(); err != nil {
		return nil, err
	}
	_node, _spec := oic.createSpec()
	if err := sqlgraph.CreateNode(ctx, oic.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
		}
	}
	oic.mutation.id = &_node.ID
	oic.mutation.done = true
	return _node, nil
}

func (oic *OrgInvitationCreate) createSpec() (*OrgInvitation, *sqlgraph.CreateSpec) {
	var (
		_node = &OrgInvitation{config: oic.config}
		_spec = sqlgraph.NewCreateSpec(orginvitation.Table, sqlgraph.NewFieldSpec(orginvitation.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = oic.conflict
	if id, ok := oic.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := oic.mutation.ReceiverEmail(); ok {
		_spec.SetField(orginvitation.FieldReceiverEmail, field.TypeString, value)
		_node.ReceiverEmail = value
	}
	if value, ok := oic.mutation.Status(); ok {
		_spec.SetField(orginvitation.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := oic.mutation.CreatedAt(); ok {
		_spec.SetField(orginvitation.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := oic.mutation.DeletedAt(); ok {
		_spec.SetField(orginvitation.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := oic.mutation.Role(); ok {
		_spec.SetField(orginvitation.FieldRole, field.TypeEnum, value)
		_node.Role = value
	}
	if value, ok := oic.mutation.Context(); ok {
		_spec.SetField(orginvitation.FieldContext, field.TypeJSON, value)
		_node.Context = value
	}
	if nodes := oic.mutation.OrganizationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   orginvitation.OrganizationTable,
			Columns: []string{orginvitation.OrganizationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(organization.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.OrganizationID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := oic.mutation.SenderIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   orginvitation.SenderTable,
			Columns: []string{orginvitation.SenderColumn},
			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.SenderID = 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.OrgInvitation.Create().
//		SetReceiverEmail(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.OrgInvitationUpsert) {
//			SetReceiverEmail(v+v).
//		}).
//		Exec(ctx)
func (oic *OrgInvitationCreate) OnConflict(opts ...sql.ConflictOption) *OrgInvitationUpsertOne {
	oic.conflict = opts
	return &OrgInvitationUpsertOne{
		create: oic,
	}
}

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

type (
	// OrgInvitationUpsertOne is the builder for "upsert"-ing
	//  one OrgInvitation node.
	OrgInvitationUpsertOne struct {
		create *OrgInvitationCreate
	}

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

// SetStatus sets the "status" field.
func (u *OrgInvitationUpsert) SetStatus(v biz.OrgInvitationStatus) *OrgInvitationUpsert {
	u.Set(orginvitation.FieldStatus, v)
	return u
}

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (u *OrgInvitationUpsert) SetOrganizationID(v uuid.UUID) *OrgInvitationUpsert {
	u.Set(orginvitation.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *OrgInvitationUpsert) UpdateOrganizationID() *OrgInvitationUpsert {
	u.SetExcluded(orginvitation.FieldOrganizationID)
	return u
}

// SetSenderID sets the "sender_id" field.
func (u *OrgInvitationUpsert) SetSenderID(v uuid.UUID) *OrgInvitationUpsert {
	u.Set(orginvitation.FieldSenderID, v)
	return u
}

// UpdateSenderID sets the "sender_id" field to the value that was provided on create.
func (u *OrgInvitationUpsert) UpdateSenderID() *OrgInvitationUpsert {
	u.SetExcluded(orginvitation.FieldSenderID)
	return u
}

// SetRole sets the "role" field.
func (u *OrgInvitationUpsert) SetRole(v authz.Role) *OrgInvitationUpsert {
	u.Set(orginvitation.FieldRole, v)
	return u
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *OrgInvitationUpsert) UpdateRole() *OrgInvitationUpsert {
	u.SetExcluded(orginvitation.FieldRole)
	return u
}

// ClearRole clears the value of the "role" field.
func (u *OrgInvitationUpsert) ClearRole() *OrgInvitationUpsert {
	u.SetNull(orginvitation.FieldRole)
	return u
}

// SetContext sets the "context" field.
func (u *OrgInvitationUpsert) SetContext(v biz.OrgInvitationContext) *OrgInvitationUpsert {
	u.Set(orginvitation.FieldContext, v)
	return u
}

// UpdateContext sets the "context" field to the value that was provided on create.
func (u *OrgInvitationUpsert) UpdateContext() *OrgInvitationUpsert {
	u.SetExcluded(orginvitation.FieldContext)
	return u
}

// ClearContext clears the value of the "context" field.
func (u *OrgInvitationUpsert) ClearContext() *OrgInvitationUpsert {
	u.SetNull(orginvitation.FieldContext)
	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.OrgInvitation.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(orginvitation.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *OrgInvitationUpsertOne) UpdateNewValues() *OrgInvitationUpsertOne {
	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(orginvitation.FieldID)
		}
		if _, exists := u.create.mutation.ReceiverEmail(); exists {
			s.SetIgnore(orginvitation.FieldReceiverEmail)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(orginvitation.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetStatus sets the "status" field.
func (u *OrgInvitationUpsertOne) SetStatus(v biz.OrgInvitationStatus) *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetStatus(v)
	})
}

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (u *OrgInvitationUpsertOne) SetOrganizationID(v uuid.UUID) *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *OrgInvitationUpsertOne) UpdateOrganizationID() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateOrganizationID()
	})
}

// SetSenderID sets the "sender_id" field.
func (u *OrgInvitationUpsertOne) SetSenderID(v uuid.UUID) *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetSenderID(v)
	})
}

// UpdateSenderID sets the "sender_id" field to the value that was provided on create.
func (u *OrgInvitationUpsertOne) UpdateSenderID() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateSenderID()
	})
}

// SetRole sets the "role" field.
func (u *OrgInvitationUpsertOne) SetRole(v authz.Role) *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetRole(v)
	})
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *OrgInvitationUpsertOne) UpdateRole() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateRole()
	})
}

// ClearRole clears the value of the "role" field.
func (u *OrgInvitationUpsertOne) ClearRole() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.ClearRole()
	})
}

// SetContext sets the "context" field.
func (u *OrgInvitationUpsertOne) SetContext(v biz.OrgInvitationContext) *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetContext(v)
	})
}

// UpdateContext sets the "context" field to the value that was provided on create.
func (u *OrgInvitationUpsertOne) UpdateContext() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateContext()
	})
}

// ClearContext clears the value of the "context" field.
func (u *OrgInvitationUpsertOne) ClearContext() *OrgInvitationUpsertOne {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.ClearContext()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *OrgInvitationUpsertOne) 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 *OrgInvitationUpsertOne) 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: OrgInvitationUpsertOne.ID is not supported by MySQL driver. Use OrgInvitationUpsertOne.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 *OrgInvitationUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.OrgInvitation.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.OrgInvitationUpsert) {
//			SetReceiverEmail(v+v).
//		}).
//		Exec(ctx)
func (oicb *OrgInvitationCreateBulk) OnConflict(opts ...sql.ConflictOption) *OrgInvitationUpsertBulk {
	oicb.conflict = opts
	return &OrgInvitationUpsertBulk{
		create: oicb,
	}
}

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

// OrgInvitationUpsertBulk is the builder for "upsert"-ing
// a bulk of OrgInvitation nodes.
type OrgInvitationUpsertBulk struct {
	create *OrgInvitationCreateBulk
}

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

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

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

// SetStatus sets the "status" field.
func (u *OrgInvitationUpsertBulk) SetStatus(v biz.OrgInvitationStatus) *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetStatus(v)
	})
}

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (u *OrgInvitationUpsertBulk) SetOrganizationID(v uuid.UUID) *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *OrgInvitationUpsertBulk) UpdateOrganizationID() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateOrganizationID()
	})
}

// SetSenderID sets the "sender_id" field.
func (u *OrgInvitationUpsertBulk) SetSenderID(v uuid.UUID) *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetSenderID(v)
	})
}

// UpdateSenderID sets the "sender_id" field to the value that was provided on create.
func (u *OrgInvitationUpsertBulk) UpdateSenderID() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateSenderID()
	})
}

// SetRole sets the "role" field.
func (u *OrgInvitationUpsertBulk) SetRole(v authz.Role) *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetRole(v)
	})
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *OrgInvitationUpsertBulk) UpdateRole() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateRole()
	})
}

// ClearRole clears the value of the "role" field.
func (u *OrgInvitationUpsertBulk) ClearRole() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.ClearRole()
	})
}

// SetContext sets the "context" field.
func (u *OrgInvitationUpsertBulk) SetContext(v biz.OrgInvitationContext) *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.SetContext(v)
	})
}

// UpdateContext sets the "context" field to the value that was provided on create.
func (u *OrgInvitationUpsertBulk) UpdateContext() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.UpdateContext()
	})
}

// ClearContext clears the value of the "context" field.
func (u *OrgInvitationUpsertBulk) ClearContext() *OrgInvitationUpsertBulk {
	return u.Update(func(s *OrgInvitationUpsert) {
		s.ClearContext()
	})
}

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

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