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

// WorkflowContractCreate is the builder for creating a WorkflowContract entity.
type WorkflowContractCreate struct {
	config
	mutation *WorkflowContractMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetName sets the "name" field.
func (wcc *WorkflowContractCreate) SetName(s string) *WorkflowContractCreate {
	wcc.mutation.SetName(s)
	return wcc
}

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

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

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

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

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

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

// SetDescription sets the "description" field.
func (wcc *WorkflowContractCreate) SetDescription(s string) *WorkflowContractCreate {
	wcc.mutation.SetDescription(s)
	return wcc
}

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

// SetScopedResourceType sets the "scoped_resource_type" field.
func (wcc *WorkflowContractCreate) SetScopedResourceType(bs biz.ContractScope) *WorkflowContractCreate {
	wcc.mutation.SetScopedResourceType(bs)
	return wcc
}

// SetNillableScopedResourceType sets the "scoped_resource_type" field if the given value is not nil.
func (wcc *WorkflowContractCreate) SetNillableScopedResourceType(bs *biz.ContractScope) *WorkflowContractCreate {
	if bs != nil {
		wcc.SetScopedResourceType(*bs)
	}
	return wcc
}

// SetScopedResourceID sets the "scoped_resource_id" field.
func (wcc *WorkflowContractCreate) SetScopedResourceID(u uuid.UUID) *WorkflowContractCreate {
	wcc.mutation.SetScopedResourceID(u)
	return wcc
}

// SetNillableScopedResourceID sets the "scoped_resource_id" field if the given value is not nil.
func (wcc *WorkflowContractCreate) SetNillableScopedResourceID(u *uuid.UUID) *WorkflowContractCreate {
	if u != nil {
		wcc.SetScopedResourceID(*u)
	}
	return wcc
}

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

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

// AddVersionIDs adds the "versions" edge to the WorkflowContractVersion entity by IDs.
func (wcc *WorkflowContractCreate) AddVersionIDs(ids ...uuid.UUID) *WorkflowContractCreate {
	wcc.mutation.AddVersionIDs(ids...)
	return wcc
}

// AddVersions adds the "versions" edges to the WorkflowContractVersion entity.
func (wcc *WorkflowContractCreate) AddVersions(w ...*WorkflowContractVersion) *WorkflowContractCreate {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcc.AddVersionIDs(ids...)
}

// SetOrganizationID sets the "organization" edge to the Organization entity by ID.
func (wcc *WorkflowContractCreate) SetOrganizationID(id uuid.UUID) *WorkflowContractCreate {
	wcc.mutation.SetOrganizationID(id)
	return wcc
}

// SetNillableOrganizationID sets the "organization" edge to the Organization entity by ID if the given value is not nil.
func (wcc *WorkflowContractCreate) SetNillableOrganizationID(id *uuid.UUID) *WorkflowContractCreate {
	if id != nil {
		wcc = wcc.SetOrganizationID(*id)
	}
	return wcc
}

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

// AddWorkflowIDs adds the "workflows" edge to the Workflow entity by IDs.
func (wcc *WorkflowContractCreate) AddWorkflowIDs(ids ...uuid.UUID) *WorkflowContractCreate {
	wcc.mutation.AddWorkflowIDs(ids...)
	return wcc
}

// AddWorkflows adds the "workflows" edges to the Workflow entity.
func (wcc *WorkflowContractCreate) AddWorkflows(w ...*Workflow) *WorkflowContractCreate {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcc.AddWorkflowIDs(ids...)
}

// Mutation returns the WorkflowContractMutation object of the builder.
func (wcc *WorkflowContractCreate) Mutation() *WorkflowContractMutation {
	return wcc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (wcc *WorkflowContractCreate) defaults() {
	if _, ok := wcc.mutation.CreatedAt(); !ok {
		v := workflowcontract.DefaultCreatedAt()
		wcc.mutation.SetCreatedAt(v)
	}
	if _, ok := wcc.mutation.UpdatedAt(); !ok {
		v := workflowcontract.DefaultUpdatedAt()
		wcc.mutation.SetUpdatedAt(v)
	}
	if _, ok := wcc.mutation.ID(); !ok {
		v := workflowcontract.DefaultID()
		wcc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (wcc *WorkflowContractCreate) check() error {
	if _, ok := wcc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "WorkflowContract.name"`)}
	}
	if _, ok := wcc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "WorkflowContract.created_at"`)}
	}
	if _, ok := wcc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "WorkflowContract.updated_at"`)}
	}
	if v, ok := wcc.mutation.ScopedResourceType(); ok {
		if err := workflowcontract.ScopedResourceTypeValidator(v); err != nil {
			return &ValidationError{Name: "scoped_resource_type", err: fmt.Errorf(`ent: validator failed for field "WorkflowContract.scoped_resource_type": %w`, err)}
		}
	}
	return nil
}

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

func (wcc *WorkflowContractCreate) createSpec() (*WorkflowContract, *sqlgraph.CreateSpec) {
	var (
		_node = &WorkflowContract{config: wcc.config}
		_spec = sqlgraph.NewCreateSpec(workflowcontract.Table, sqlgraph.NewFieldSpec(workflowcontract.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = wcc.conflict
	if id, ok := wcc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := wcc.mutation.Name(); ok {
		_spec.SetField(workflowcontract.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := wcc.mutation.CreatedAt(); ok {
		_spec.SetField(workflowcontract.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := wcc.mutation.UpdatedAt(); ok {
		_spec.SetField(workflowcontract.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := wcc.mutation.DeletedAt(); ok {
		_spec.SetField(workflowcontract.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := wcc.mutation.Description(); ok {
		_spec.SetField(workflowcontract.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := wcc.mutation.ScopedResourceType(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceType, field.TypeEnum, value)
		_node.ScopedResourceType = value
	}
	if value, ok := wcc.mutation.ScopedResourceID(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceID, field.TypeUUID, value)
		_node.ScopedResourceID = value
	}
	if nodes := wcc.mutation.VersionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   workflowcontract.VersionsTable,
			Columns: []string{workflowcontract.VersionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflowcontractversion.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := wcc.mutation.OrganizationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   workflowcontract.OrganizationTable,
			Columns: []string{workflowcontract.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.organization_workflow_contracts = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := wcc.mutation.WorkflowsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   workflowcontract.WorkflowsTable,
			Columns: []string{workflowcontract.WorkflowsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		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.WorkflowContract.Create().
//		SetName(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.WorkflowContractUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (wcc *WorkflowContractCreate) OnConflict(opts ...sql.ConflictOption) *WorkflowContractUpsertOne {
	wcc.conflict = opts
	return &WorkflowContractUpsertOne{
		create: wcc,
	}
}

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

type (
	// WorkflowContractUpsertOne is the builder for "upsert"-ing
	//  one WorkflowContract node.
	WorkflowContractUpsertOne struct {
		create *WorkflowContractCreate
	}

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

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

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

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

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

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

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

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

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

// SetScopedResourceType sets the "scoped_resource_type" field.
func (u *WorkflowContractUpsert) SetScopedResourceType(v biz.ContractScope) *WorkflowContractUpsert {
	u.Set(workflowcontract.FieldScopedResourceType, v)
	return u
}

// UpdateScopedResourceType sets the "scoped_resource_type" field to the value that was provided on create.
func (u *WorkflowContractUpsert) UpdateScopedResourceType() *WorkflowContractUpsert {
	u.SetExcluded(workflowcontract.FieldScopedResourceType)
	return u
}

// ClearScopedResourceType clears the value of the "scoped_resource_type" field.
func (u *WorkflowContractUpsert) ClearScopedResourceType() *WorkflowContractUpsert {
	u.SetNull(workflowcontract.FieldScopedResourceType)
	return u
}

// SetScopedResourceID sets the "scoped_resource_id" field.
func (u *WorkflowContractUpsert) SetScopedResourceID(v uuid.UUID) *WorkflowContractUpsert {
	u.Set(workflowcontract.FieldScopedResourceID, v)
	return u
}

// UpdateScopedResourceID sets the "scoped_resource_id" field to the value that was provided on create.
func (u *WorkflowContractUpsert) UpdateScopedResourceID() *WorkflowContractUpsert {
	u.SetExcluded(workflowcontract.FieldScopedResourceID)
	return u
}

// ClearScopedResourceID clears the value of the "scoped_resource_id" field.
func (u *WorkflowContractUpsert) ClearScopedResourceID() *WorkflowContractUpsert {
	u.SetNull(workflowcontract.FieldScopedResourceID)
	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.WorkflowContract.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(workflowcontract.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *WorkflowContractUpsertOne) UpdateNewValues() *WorkflowContractUpsertOne {
	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(workflowcontract.FieldID)
		}
		if _, exists := u.create.mutation.Name(); exists {
			s.SetIgnore(workflowcontract.FieldName)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(workflowcontract.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

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

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

// SetScopedResourceType sets the "scoped_resource_type" field.
func (u *WorkflowContractUpsertOne) SetScopedResourceType(v biz.ContractScope) *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.SetScopedResourceType(v)
	})
}

// UpdateScopedResourceType sets the "scoped_resource_type" field to the value that was provided on create.
func (u *WorkflowContractUpsertOne) UpdateScopedResourceType() *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.UpdateScopedResourceType()
	})
}

// ClearScopedResourceType clears the value of the "scoped_resource_type" field.
func (u *WorkflowContractUpsertOne) ClearScopedResourceType() *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.ClearScopedResourceType()
	})
}

// SetScopedResourceID sets the "scoped_resource_id" field.
func (u *WorkflowContractUpsertOne) SetScopedResourceID(v uuid.UUID) *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.SetScopedResourceID(v)
	})
}

// UpdateScopedResourceID sets the "scoped_resource_id" field to the value that was provided on create.
func (u *WorkflowContractUpsertOne) UpdateScopedResourceID() *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.UpdateScopedResourceID()
	})
}

// ClearScopedResourceID clears the value of the "scoped_resource_id" field.
func (u *WorkflowContractUpsertOne) ClearScopedResourceID() *WorkflowContractUpsertOne {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.ClearScopedResourceID()
	})
}

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

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

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.WorkflowContract.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.WorkflowContractUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (wccb *WorkflowContractCreateBulk) OnConflict(opts ...sql.ConflictOption) *WorkflowContractUpsertBulk {
	wccb.conflict = opts
	return &WorkflowContractUpsertBulk{
		create: wccb,
	}
}

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

// WorkflowContractUpsertBulk is the builder for "upsert"-ing
// a bulk of WorkflowContract nodes.
type WorkflowContractUpsertBulk struct {
	create *WorkflowContractCreateBulk
}

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

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

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

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

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

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

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

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

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

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

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

// SetScopedResourceType sets the "scoped_resource_type" field.
func (u *WorkflowContractUpsertBulk) SetScopedResourceType(v biz.ContractScope) *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.SetScopedResourceType(v)
	})
}

// UpdateScopedResourceType sets the "scoped_resource_type" field to the value that was provided on create.
func (u *WorkflowContractUpsertBulk) UpdateScopedResourceType() *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.UpdateScopedResourceType()
	})
}

// ClearScopedResourceType clears the value of the "scoped_resource_type" field.
func (u *WorkflowContractUpsertBulk) ClearScopedResourceType() *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.ClearScopedResourceType()
	})
}

// SetScopedResourceID sets the "scoped_resource_id" field.
func (u *WorkflowContractUpsertBulk) SetScopedResourceID(v uuid.UUID) *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.SetScopedResourceID(v)
	})
}

// UpdateScopedResourceID sets the "scoped_resource_id" field to the value that was provided on create.
func (u *WorkflowContractUpsertBulk) UpdateScopedResourceID() *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.UpdateScopedResourceID()
	})
}

// ClearScopedResourceID clears the value of the "scoped_resource_id" field.
func (u *WorkflowContractUpsertBulk) ClearScopedResourceID() *WorkflowContractUpsertBulk {
	return u.Update(func(s *WorkflowContractUpsert) {
		s.ClearScopedResourceID()
	})
}

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

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