// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/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/predicate"
	"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"
)

// WorkflowContractUpdate is the builder for updating WorkflowContract entities.
type WorkflowContractUpdate struct {
	config
	hooks     []Hook
	mutation  *WorkflowContractMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the WorkflowContractUpdate builder.
func (wcu *WorkflowContractUpdate) Where(ps ...predicate.WorkflowContract) *WorkflowContractUpdate {
	wcu.mutation.Where(ps...)
	return wcu
}

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

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (wcu *WorkflowContractUpdate) ClearDeletedAt() *WorkflowContractUpdate {
	wcu.mutation.ClearDeletedAt()
	return wcu
}

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

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

// ClearDescription clears the value of the "description" field.
func (wcu *WorkflowContractUpdate) ClearDescription() *WorkflowContractUpdate {
	wcu.mutation.ClearDescription()
	return wcu
}

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

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

// ClearScopedResourceType clears the value of the "scoped_resource_type" field.
func (wcu *WorkflowContractUpdate) ClearScopedResourceType() *WorkflowContractUpdate {
	wcu.mutation.ClearScopedResourceType()
	return wcu
}

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

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

// ClearScopedResourceID clears the value of the "scoped_resource_id" field.
func (wcu *WorkflowContractUpdate) ClearScopedResourceID() *WorkflowContractUpdate {
	wcu.mutation.ClearScopedResourceID()
	return wcu
}

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

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

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

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

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

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

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

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

// ClearVersions clears all "versions" edges to the WorkflowContractVersion entity.
func (wcu *WorkflowContractUpdate) ClearVersions() *WorkflowContractUpdate {
	wcu.mutation.ClearVersions()
	return wcu
}

// RemoveVersionIDs removes the "versions" edge to WorkflowContractVersion entities by IDs.
func (wcu *WorkflowContractUpdate) RemoveVersionIDs(ids ...uuid.UUID) *WorkflowContractUpdate {
	wcu.mutation.RemoveVersionIDs(ids...)
	return wcu
}

// RemoveVersions removes "versions" edges to WorkflowContractVersion entities.
func (wcu *WorkflowContractUpdate) RemoveVersions(w ...*WorkflowContractVersion) *WorkflowContractUpdate {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcu.RemoveVersionIDs(ids...)
}

// ClearOrganization clears the "organization" edge to the Organization entity.
func (wcu *WorkflowContractUpdate) ClearOrganization() *WorkflowContractUpdate {
	wcu.mutation.ClearOrganization()
	return wcu
}

// ClearWorkflows clears all "workflows" edges to the Workflow entity.
func (wcu *WorkflowContractUpdate) ClearWorkflows() *WorkflowContractUpdate {
	wcu.mutation.ClearWorkflows()
	return wcu
}

// RemoveWorkflowIDs removes the "workflows" edge to Workflow entities by IDs.
func (wcu *WorkflowContractUpdate) RemoveWorkflowIDs(ids ...uuid.UUID) *WorkflowContractUpdate {
	wcu.mutation.RemoveWorkflowIDs(ids...)
	return wcu
}

// RemoveWorkflows removes "workflows" edges to Workflow entities.
func (wcu *WorkflowContractUpdate) RemoveWorkflows(w ...*Workflow) *WorkflowContractUpdate {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcu.RemoveWorkflowIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (wcu *WorkflowContractUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, wcu.sqlSave, wcu.mutation, wcu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (wcu *WorkflowContractUpdate) SaveX(ctx context.Context) int {
	affected, err := wcu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// check runs all checks and user-defined validators on the builder.
func (wcu *WorkflowContractUpdate) check() error {
	if v, ok := wcu.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
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (wcu *WorkflowContractUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *WorkflowContractUpdate {
	wcu.modifiers = append(wcu.modifiers, modifiers...)
	return wcu
}

func (wcu *WorkflowContractUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := wcu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(workflowcontract.Table, workflowcontract.Columns, sqlgraph.NewFieldSpec(workflowcontract.FieldID, field.TypeUUID))
	if ps := wcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcu.mutation.UpdatedAt(); ok {
		_spec.SetField(workflowcontract.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcu.mutation.DeletedAt(); ok {
		_spec.SetField(workflowcontract.FieldDeletedAt, field.TypeTime, value)
	}
	if wcu.mutation.DeletedAtCleared() {
		_spec.ClearField(workflowcontract.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcu.mutation.Description(); ok {
		_spec.SetField(workflowcontract.FieldDescription, field.TypeString, value)
	}
	if wcu.mutation.DescriptionCleared() {
		_spec.ClearField(workflowcontract.FieldDescription, field.TypeString)
	}
	if value, ok := wcu.mutation.ScopedResourceType(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceType, field.TypeEnum, value)
	}
	if wcu.mutation.ScopedResourceTypeCleared() {
		_spec.ClearField(workflowcontract.FieldScopedResourceType, field.TypeEnum)
	}
	if value, ok := wcu.mutation.ScopedResourceID(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceID, field.TypeUUID, value)
	}
	if wcu.mutation.ScopedResourceIDCleared() {
		_spec.ClearField(workflowcontract.FieldScopedResourceID, field.TypeUUID)
	}
	if wcu.mutation.VersionsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcu.mutation.RemovedVersionsIDs(); len(nodes) > 0 && !wcu.mutation.VersionsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcu.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.Add = append(_spec.Edges.Add, edge)
	}
	if wcu.mutation.OrganizationCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcu.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if wcu.mutation.WorkflowsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcu.mutation.RemovedWorkflowsIDs(); len(nodes) > 0 && !wcu.mutation.WorkflowsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcu.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.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(wcu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, wcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{workflowcontract.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wcu.mutation.done = true
	return n, nil
}

// WorkflowContractUpdateOne is the builder for updating a single WorkflowContract entity.
type WorkflowContractUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *WorkflowContractMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (wcuo *WorkflowContractUpdateOne) ClearDeletedAt() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearDeletedAt()
	return wcuo
}

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

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

// ClearDescription clears the value of the "description" field.
func (wcuo *WorkflowContractUpdateOne) ClearDescription() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearDescription()
	return wcuo
}

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

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

// ClearScopedResourceType clears the value of the "scoped_resource_type" field.
func (wcuo *WorkflowContractUpdateOne) ClearScopedResourceType() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearScopedResourceType()
	return wcuo
}

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

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

// ClearScopedResourceID clears the value of the "scoped_resource_id" field.
func (wcuo *WorkflowContractUpdateOne) ClearScopedResourceID() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearScopedResourceID()
	return wcuo
}

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

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

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

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

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

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

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

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

// ClearVersions clears all "versions" edges to the WorkflowContractVersion entity.
func (wcuo *WorkflowContractUpdateOne) ClearVersions() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearVersions()
	return wcuo
}

// RemoveVersionIDs removes the "versions" edge to WorkflowContractVersion entities by IDs.
func (wcuo *WorkflowContractUpdateOne) RemoveVersionIDs(ids ...uuid.UUID) *WorkflowContractUpdateOne {
	wcuo.mutation.RemoveVersionIDs(ids...)
	return wcuo
}

// RemoveVersions removes "versions" edges to WorkflowContractVersion entities.
func (wcuo *WorkflowContractUpdateOne) RemoveVersions(w ...*WorkflowContractVersion) *WorkflowContractUpdateOne {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcuo.RemoveVersionIDs(ids...)
}

// ClearOrganization clears the "organization" edge to the Organization entity.
func (wcuo *WorkflowContractUpdateOne) ClearOrganization() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearOrganization()
	return wcuo
}

// ClearWorkflows clears all "workflows" edges to the Workflow entity.
func (wcuo *WorkflowContractUpdateOne) ClearWorkflows() *WorkflowContractUpdateOne {
	wcuo.mutation.ClearWorkflows()
	return wcuo
}

// RemoveWorkflowIDs removes the "workflows" edge to Workflow entities by IDs.
func (wcuo *WorkflowContractUpdateOne) RemoveWorkflowIDs(ids ...uuid.UUID) *WorkflowContractUpdateOne {
	wcuo.mutation.RemoveWorkflowIDs(ids...)
	return wcuo
}

// RemoveWorkflows removes "workflows" edges to Workflow entities.
func (wcuo *WorkflowContractUpdateOne) RemoveWorkflows(w ...*Workflow) *WorkflowContractUpdateOne {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return wcuo.RemoveWorkflowIDs(ids...)
}

// Where appends a list predicates to the WorkflowContractUpdate builder.
func (wcuo *WorkflowContractUpdateOne) Where(ps ...predicate.WorkflowContract) *WorkflowContractUpdateOne {
	wcuo.mutation.Where(ps...)
	return wcuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (wcuo *WorkflowContractUpdateOne) Select(field string, fields ...string) *WorkflowContractUpdateOne {
	wcuo.fields = append([]string{field}, fields...)
	return wcuo
}

// Save executes the query and returns the updated WorkflowContract entity.
func (wcuo *WorkflowContractUpdateOne) Save(ctx context.Context) (*WorkflowContract, error) {
	return withHooks(ctx, wcuo.sqlSave, wcuo.mutation, wcuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (wcuo *WorkflowContractUpdateOne) SaveX(ctx context.Context) *WorkflowContract {
	node, err := wcuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (wcuo *WorkflowContractUpdateOne) Exec(ctx context.Context) error {
	_, err := wcuo.Save(ctx)
	return err
}

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

// check runs all checks and user-defined validators on the builder.
func (wcuo *WorkflowContractUpdateOne) check() error {
	if v, ok := wcuo.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
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (wcuo *WorkflowContractUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *WorkflowContractUpdateOne {
	wcuo.modifiers = append(wcuo.modifiers, modifiers...)
	return wcuo
}

func (wcuo *WorkflowContractUpdateOne) sqlSave(ctx context.Context) (_node *WorkflowContract, err error) {
	if err := wcuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(workflowcontract.Table, workflowcontract.Columns, sqlgraph.NewFieldSpec(workflowcontract.FieldID, field.TypeUUID))
	id, ok := wcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WorkflowContract.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := wcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, workflowcontract.FieldID)
		for _, f := range fields {
			if !workflowcontract.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != workflowcontract.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := wcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcuo.mutation.UpdatedAt(); ok {
		_spec.SetField(workflowcontract.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcuo.mutation.DeletedAt(); ok {
		_spec.SetField(workflowcontract.FieldDeletedAt, field.TypeTime, value)
	}
	if wcuo.mutation.DeletedAtCleared() {
		_spec.ClearField(workflowcontract.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcuo.mutation.Description(); ok {
		_spec.SetField(workflowcontract.FieldDescription, field.TypeString, value)
	}
	if wcuo.mutation.DescriptionCleared() {
		_spec.ClearField(workflowcontract.FieldDescription, field.TypeString)
	}
	if value, ok := wcuo.mutation.ScopedResourceType(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceType, field.TypeEnum, value)
	}
	if wcuo.mutation.ScopedResourceTypeCleared() {
		_spec.ClearField(workflowcontract.FieldScopedResourceType, field.TypeEnum)
	}
	if value, ok := wcuo.mutation.ScopedResourceID(); ok {
		_spec.SetField(workflowcontract.FieldScopedResourceID, field.TypeUUID, value)
	}
	if wcuo.mutation.ScopedResourceIDCleared() {
		_spec.ClearField(workflowcontract.FieldScopedResourceID, field.TypeUUID)
	}
	if wcuo.mutation.VersionsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcuo.mutation.RemovedVersionsIDs(); len(nodes) > 0 && !wcuo.mutation.VersionsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcuo.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.Add = append(_spec.Edges.Add, edge)
	}
	if wcuo.mutation.OrganizationCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if wcuo.mutation.WorkflowsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcuo.mutation.RemovedWorkflowsIDs(); len(nodes) > 0 && !wcuo.mutation.WorkflowsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := wcuo.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.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(wcuo.modifiers...)
	_node = &WorkflowContract{config: wcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, wcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{workflowcontract.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	wcuo.mutation.done = true
	return _node, nil
}
