// 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/integration"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/integrationattachment"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflow"
	"github.com/google/uuid"
)

// IntegrationAttachmentCreate is the builder for creating a IntegrationAttachment entity.
type IntegrationAttachmentCreate struct {
	config
	mutation *IntegrationAttachmentMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

// SetConfiguration sets the "configuration" field.
func (iac *IntegrationAttachmentCreate) SetConfiguration(b []byte) *IntegrationAttachmentCreate {
	iac.mutation.SetConfiguration(b)
	return iac
}

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

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

// SetWorkflowID sets the "workflow_id" field.
func (iac *IntegrationAttachmentCreate) SetWorkflowID(u uuid.UUID) *IntegrationAttachmentCreate {
	iac.mutation.SetWorkflowID(u)
	return iac
}

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

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

// SetIntegrationID sets the "integration" edge to the Integration entity by ID.
func (iac *IntegrationAttachmentCreate) SetIntegrationID(id uuid.UUID) *IntegrationAttachmentCreate {
	iac.mutation.SetIntegrationID(id)
	return iac
}

// SetIntegration sets the "integration" edge to the Integration entity.
func (iac *IntegrationAttachmentCreate) SetIntegration(i *Integration) *IntegrationAttachmentCreate {
	return iac.SetIntegrationID(i.ID)
}

// SetWorkflow sets the "workflow" edge to the Workflow entity.
func (iac *IntegrationAttachmentCreate) SetWorkflow(w *Workflow) *IntegrationAttachmentCreate {
	return iac.SetWorkflowID(w.ID)
}

// Mutation returns the IntegrationAttachmentMutation object of the builder.
func (iac *IntegrationAttachmentCreate) Mutation() *IntegrationAttachmentMutation {
	return iac.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (iac *IntegrationAttachmentCreate) defaults() {
	if _, ok := iac.mutation.CreatedAt(); !ok {
		v := integrationattachment.DefaultCreatedAt()
		iac.mutation.SetCreatedAt(v)
	}
	if _, ok := iac.mutation.ID(); !ok {
		v := integrationattachment.DefaultID()
		iac.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (iac *IntegrationAttachmentCreate) check() error {
	if _, ok := iac.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "IntegrationAttachment.created_at"`)}
	}
	if _, ok := iac.mutation.WorkflowID(); !ok {
		return &ValidationError{Name: "workflow_id", err: errors.New(`ent: missing required field "IntegrationAttachment.workflow_id"`)}
	}
	if len(iac.mutation.IntegrationIDs()) == 0 {
		return &ValidationError{Name: "integration", err: errors.New(`ent: missing required edge "IntegrationAttachment.integration"`)}
	}
	if len(iac.mutation.WorkflowIDs()) == 0 {
		return &ValidationError{Name: "workflow", err: errors.New(`ent: missing required edge "IntegrationAttachment.workflow"`)}
	}
	return nil
}

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

func (iac *IntegrationAttachmentCreate) createSpec() (*IntegrationAttachment, *sqlgraph.CreateSpec) {
	var (
		_node = &IntegrationAttachment{config: iac.config}
		_spec = sqlgraph.NewCreateSpec(integrationattachment.Table, sqlgraph.NewFieldSpec(integrationattachment.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = iac.conflict
	if id, ok := iac.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := iac.mutation.CreatedAt(); ok {
		_spec.SetField(integrationattachment.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := iac.mutation.Configuration(); ok {
		_spec.SetField(integrationattachment.FieldConfiguration, field.TypeBytes, value)
		_node.Configuration = value
	}
	if value, ok := iac.mutation.DeletedAt(); ok {
		_spec.SetField(integrationattachment.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if nodes := iac.mutation.IntegrationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   integrationattachment.IntegrationTable,
			Columns: []string{integrationattachment.IntegrationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(integration.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.integration_attachment_integration = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := iac.mutation.WorkflowIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   integrationattachment.WorkflowTable,
			Columns: []string{integrationattachment.WorkflowColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.WorkflowID = 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.IntegrationAttachment.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.IntegrationAttachmentUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (iac *IntegrationAttachmentCreate) OnConflict(opts ...sql.ConflictOption) *IntegrationAttachmentUpsertOne {
	iac.conflict = opts
	return &IntegrationAttachmentUpsertOne{
		create: iac,
	}
}

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

type (
	// IntegrationAttachmentUpsertOne is the builder for "upsert"-ing
	//  one IntegrationAttachment node.
	IntegrationAttachmentUpsertOne struct {
		create *IntegrationAttachmentCreate
	}

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

// SetConfiguration sets the "configuration" field.
func (u *IntegrationAttachmentUpsert) SetConfiguration(v []byte) *IntegrationAttachmentUpsert {
	u.Set(integrationattachment.FieldConfiguration, v)
	return u
}

// UpdateConfiguration sets the "configuration" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsert) UpdateConfiguration() *IntegrationAttachmentUpsert {
	u.SetExcluded(integrationattachment.FieldConfiguration)
	return u
}

// ClearConfiguration clears the value of the "configuration" field.
func (u *IntegrationAttachmentUpsert) ClearConfiguration() *IntegrationAttachmentUpsert {
	u.SetNull(integrationattachment.FieldConfiguration)
	return u
}

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

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

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

// SetWorkflowID sets the "workflow_id" field.
func (u *IntegrationAttachmentUpsert) SetWorkflowID(v uuid.UUID) *IntegrationAttachmentUpsert {
	u.Set(integrationattachment.FieldWorkflowID, v)
	return u
}

// UpdateWorkflowID sets the "workflow_id" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsert) UpdateWorkflowID() *IntegrationAttachmentUpsert {
	u.SetExcluded(integrationattachment.FieldWorkflowID)
	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.IntegrationAttachment.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(integrationattachment.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *IntegrationAttachmentUpsertOne) UpdateNewValues() *IntegrationAttachmentUpsertOne {
	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(integrationattachment.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(integrationattachment.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetConfiguration sets the "configuration" field.
func (u *IntegrationAttachmentUpsertOne) SetConfiguration(v []byte) *IntegrationAttachmentUpsertOne {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.SetConfiguration(v)
	})
}

// UpdateConfiguration sets the "configuration" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsertOne) UpdateConfiguration() *IntegrationAttachmentUpsertOne {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.UpdateConfiguration()
	})
}

// ClearConfiguration clears the value of the "configuration" field.
func (u *IntegrationAttachmentUpsertOne) ClearConfiguration() *IntegrationAttachmentUpsertOne {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.ClearConfiguration()
	})
}

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

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

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

// SetWorkflowID sets the "workflow_id" field.
func (u *IntegrationAttachmentUpsertOne) SetWorkflowID(v uuid.UUID) *IntegrationAttachmentUpsertOne {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.SetWorkflowID(v)
	})
}

// UpdateWorkflowID sets the "workflow_id" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsertOne) UpdateWorkflowID() *IntegrationAttachmentUpsertOne {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.UpdateWorkflowID()
	})
}

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

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

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

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

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

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

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

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

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

// IntegrationAttachmentUpsertBulk is the builder for "upsert"-ing
// a bulk of IntegrationAttachment nodes.
type IntegrationAttachmentUpsertBulk struct {
	create *IntegrationAttachmentCreateBulk
}

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

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

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

// SetConfiguration sets the "configuration" field.
func (u *IntegrationAttachmentUpsertBulk) SetConfiguration(v []byte) *IntegrationAttachmentUpsertBulk {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.SetConfiguration(v)
	})
}

// UpdateConfiguration sets the "configuration" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsertBulk) UpdateConfiguration() *IntegrationAttachmentUpsertBulk {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.UpdateConfiguration()
	})
}

// ClearConfiguration clears the value of the "configuration" field.
func (u *IntegrationAttachmentUpsertBulk) ClearConfiguration() *IntegrationAttachmentUpsertBulk {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.ClearConfiguration()
	})
}

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

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

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

// SetWorkflowID sets the "workflow_id" field.
func (u *IntegrationAttachmentUpsertBulk) SetWorkflowID(v uuid.UUID) *IntegrationAttachmentUpsertBulk {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.SetWorkflowID(v)
	})
}

// UpdateWorkflowID sets the "workflow_id" field to the value that was provided on create.
func (u *IntegrationAttachmentUpsertBulk) UpdateWorkflowID() *IntegrationAttachmentUpsertBulk {
	return u.Update(func(s *IntegrationAttachmentUpsert) {
		s.UpdateWorkflowID()
	})
}

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

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