// 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/project"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/projectversion"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflowrun"
	"github.com/google/uuid"
)

// ProjectVersionCreate is the builder for creating a ProjectVersion entity.
type ProjectVersionCreate struct {
	config
	mutation *ProjectVersionMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetVersion sets the "version" field.
func (pvc *ProjectVersionCreate) SetVersion(s string) *ProjectVersionCreate {
	pvc.mutation.SetVersion(s)
	return pvc
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillableVersion(s *string) *ProjectVersionCreate {
	if s != nil {
		pvc.SetVersion(*s)
	}
	return pvc
}

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

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

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

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

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

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

// SetProjectID sets the "project_id" field.
func (pvc *ProjectVersionCreate) SetProjectID(u uuid.UUID) *ProjectVersionCreate {
	pvc.mutation.SetProjectID(u)
	return pvc
}

// SetPrerelease sets the "prerelease" field.
func (pvc *ProjectVersionCreate) SetPrerelease(b bool) *ProjectVersionCreate {
	pvc.mutation.SetPrerelease(b)
	return pvc
}

// SetNillablePrerelease sets the "prerelease" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillablePrerelease(b *bool) *ProjectVersionCreate {
	if b != nil {
		pvc.SetPrerelease(*b)
	}
	return pvc
}

// SetWorkflowRunCount sets the "workflow_run_count" field.
func (pvc *ProjectVersionCreate) SetWorkflowRunCount(i int) *ProjectVersionCreate {
	pvc.mutation.SetWorkflowRunCount(i)
	return pvc
}

// SetNillableWorkflowRunCount sets the "workflow_run_count" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillableWorkflowRunCount(i *int) *ProjectVersionCreate {
	if i != nil {
		pvc.SetWorkflowRunCount(*i)
	}
	return pvc
}

// SetReleasedAt sets the "released_at" field.
func (pvc *ProjectVersionCreate) SetReleasedAt(t time.Time) *ProjectVersionCreate {
	pvc.mutation.SetReleasedAt(t)
	return pvc
}

// SetNillableReleasedAt sets the "released_at" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillableReleasedAt(t *time.Time) *ProjectVersionCreate {
	if t != nil {
		pvc.SetReleasedAt(*t)
	}
	return pvc
}

// SetLastRunAt sets the "last_run_at" field.
func (pvc *ProjectVersionCreate) SetLastRunAt(t time.Time) *ProjectVersionCreate {
	pvc.mutation.SetLastRunAt(t)
	return pvc
}

// SetNillableLastRunAt sets the "last_run_at" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillableLastRunAt(t *time.Time) *ProjectVersionCreate {
	if t != nil {
		pvc.SetLastRunAt(*t)
	}
	return pvc
}

// SetLatest sets the "latest" field.
func (pvc *ProjectVersionCreate) SetLatest(b bool) *ProjectVersionCreate {
	pvc.mutation.SetLatest(b)
	return pvc
}

// SetNillableLatest sets the "latest" field if the given value is not nil.
func (pvc *ProjectVersionCreate) SetNillableLatest(b *bool) *ProjectVersionCreate {
	if b != nil {
		pvc.SetLatest(*b)
	}
	return pvc
}

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

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

// SetProject sets the "project" edge to the Project entity.
func (pvc *ProjectVersionCreate) SetProject(p *Project) *ProjectVersionCreate {
	return pvc.SetProjectID(p.ID)
}

// AddRunIDs adds the "runs" edge to the WorkflowRun entity by IDs.
func (pvc *ProjectVersionCreate) AddRunIDs(ids ...uuid.UUID) *ProjectVersionCreate {
	pvc.mutation.AddRunIDs(ids...)
	return pvc
}

// AddRuns adds the "runs" edges to the WorkflowRun entity.
func (pvc *ProjectVersionCreate) AddRuns(w ...*WorkflowRun) *ProjectVersionCreate {
	ids := make([]uuid.UUID, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return pvc.AddRunIDs(ids...)
}

// Mutation returns the ProjectVersionMutation object of the builder.
func (pvc *ProjectVersionCreate) Mutation() *ProjectVersionMutation {
	return pvc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (pvc *ProjectVersionCreate) defaults() {
	if _, ok := pvc.mutation.Version(); !ok {
		v := projectversion.DefaultVersion
		pvc.mutation.SetVersion(v)
	}
	if _, ok := pvc.mutation.CreatedAt(); !ok {
		v := projectversion.DefaultCreatedAt()
		pvc.mutation.SetCreatedAt(v)
	}
	if _, ok := pvc.mutation.UpdatedAt(); !ok {
		v := projectversion.DefaultUpdatedAt()
		pvc.mutation.SetUpdatedAt(v)
	}
	if _, ok := pvc.mutation.Prerelease(); !ok {
		v := projectversion.DefaultPrerelease
		pvc.mutation.SetPrerelease(v)
	}
	if _, ok := pvc.mutation.WorkflowRunCount(); !ok {
		v := projectversion.DefaultWorkflowRunCount
		pvc.mutation.SetWorkflowRunCount(v)
	}
	if _, ok := pvc.mutation.Latest(); !ok {
		v := projectversion.DefaultLatest
		pvc.mutation.SetLatest(v)
	}
	if _, ok := pvc.mutation.ID(); !ok {
		v := projectversion.DefaultID()
		pvc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (pvc *ProjectVersionCreate) check() error {
	if _, ok := pvc.mutation.Version(); !ok {
		return &ValidationError{Name: "version", err: errors.New(`ent: missing required field "ProjectVersion.version"`)}
	}
	if v, ok := pvc.mutation.Version(); ok {
		if err := projectversion.VersionValidator(v); err != nil {
			return &ValidationError{Name: "version", err: fmt.Errorf(`ent: validator failed for field "ProjectVersion.version": %w`, err)}
		}
	}
	if _, ok := pvc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ProjectVersion.created_at"`)}
	}
	if _, ok := pvc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ProjectVersion.updated_at"`)}
	}
	if _, ok := pvc.mutation.ProjectID(); !ok {
		return &ValidationError{Name: "project_id", err: errors.New(`ent: missing required field "ProjectVersion.project_id"`)}
	}
	if _, ok := pvc.mutation.Prerelease(); !ok {
		return &ValidationError{Name: "prerelease", err: errors.New(`ent: missing required field "ProjectVersion.prerelease"`)}
	}
	if _, ok := pvc.mutation.WorkflowRunCount(); !ok {
		return &ValidationError{Name: "workflow_run_count", err: errors.New(`ent: missing required field "ProjectVersion.workflow_run_count"`)}
	}
	if _, ok := pvc.mutation.Latest(); !ok {
		return &ValidationError{Name: "latest", err: errors.New(`ent: missing required field "ProjectVersion.latest"`)}
	}
	if len(pvc.mutation.ProjectIDs()) == 0 {
		return &ValidationError{Name: "project", err: errors.New(`ent: missing required edge "ProjectVersion.project"`)}
	}
	return nil
}

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

func (pvc *ProjectVersionCreate) createSpec() (*ProjectVersion, *sqlgraph.CreateSpec) {
	var (
		_node = &ProjectVersion{config: pvc.config}
		_spec = sqlgraph.NewCreateSpec(projectversion.Table, sqlgraph.NewFieldSpec(projectversion.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = pvc.conflict
	if id, ok := pvc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := pvc.mutation.Version(); ok {
		_spec.SetField(projectversion.FieldVersion, field.TypeString, value)
		_node.Version = value
	}
	if value, ok := pvc.mutation.CreatedAt(); ok {
		_spec.SetField(projectversion.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := pvc.mutation.UpdatedAt(); ok {
		_spec.SetField(projectversion.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := pvc.mutation.DeletedAt(); ok {
		_spec.SetField(projectversion.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := pvc.mutation.Prerelease(); ok {
		_spec.SetField(projectversion.FieldPrerelease, field.TypeBool, value)
		_node.Prerelease = value
	}
	if value, ok := pvc.mutation.WorkflowRunCount(); ok {
		_spec.SetField(projectversion.FieldWorkflowRunCount, field.TypeInt, value)
		_node.WorkflowRunCount = value
	}
	if value, ok := pvc.mutation.ReleasedAt(); ok {
		_spec.SetField(projectversion.FieldReleasedAt, field.TypeTime, value)
		_node.ReleasedAt = value
	}
	if value, ok := pvc.mutation.LastRunAt(); ok {
		_spec.SetField(projectversion.FieldLastRunAt, field.TypeTime, value)
		_node.LastRunAt = value
	}
	if value, ok := pvc.mutation.Latest(); ok {
		_spec.SetField(projectversion.FieldLatest, field.TypeBool, value)
		_node.Latest = value
	}
	if nodes := pvc.mutation.ProjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   projectversion.ProjectTable,
			Columns: []string{projectversion.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.ProjectID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := pvc.mutation.RunsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   projectversion.RunsTable,
			Columns: []string{projectversion.RunsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflowrun.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.ProjectVersion.Create().
//		SetVersion(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.ProjectVersionUpsert) {
//			SetVersion(v+v).
//		}).
//		Exec(ctx)
func (pvc *ProjectVersionCreate) OnConflict(opts ...sql.ConflictOption) *ProjectVersionUpsertOne {
	pvc.conflict = opts
	return &ProjectVersionUpsertOne{
		create: pvc,
	}
}

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

type (
	// ProjectVersionUpsertOne is the builder for "upsert"-ing
	//  one ProjectVersion node.
	ProjectVersionUpsertOne struct {
		create *ProjectVersionCreate
	}

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

// SetVersion sets the "version" field.
func (u *ProjectVersionUpsert) SetVersion(v string) *ProjectVersionUpsert {
	u.Set(projectversion.FieldVersion, v)
	return u
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateVersion() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldVersion)
	return u
}

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

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

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

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

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

// SetProjectID sets the "project_id" field.
func (u *ProjectVersionUpsert) SetProjectID(v uuid.UUID) *ProjectVersionUpsert {
	u.Set(projectversion.FieldProjectID, v)
	return u
}

// UpdateProjectID sets the "project_id" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateProjectID() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldProjectID)
	return u
}

// SetPrerelease sets the "prerelease" field.
func (u *ProjectVersionUpsert) SetPrerelease(v bool) *ProjectVersionUpsert {
	u.Set(projectversion.FieldPrerelease, v)
	return u
}

// UpdatePrerelease sets the "prerelease" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdatePrerelease() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldPrerelease)
	return u
}

// SetWorkflowRunCount sets the "workflow_run_count" field.
func (u *ProjectVersionUpsert) SetWorkflowRunCount(v int) *ProjectVersionUpsert {
	u.Set(projectversion.FieldWorkflowRunCount, v)
	return u
}

// UpdateWorkflowRunCount sets the "workflow_run_count" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateWorkflowRunCount() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldWorkflowRunCount)
	return u
}

// AddWorkflowRunCount adds v to the "workflow_run_count" field.
func (u *ProjectVersionUpsert) AddWorkflowRunCount(v int) *ProjectVersionUpsert {
	u.Add(projectversion.FieldWorkflowRunCount, v)
	return u
}

// SetReleasedAt sets the "released_at" field.
func (u *ProjectVersionUpsert) SetReleasedAt(v time.Time) *ProjectVersionUpsert {
	u.Set(projectversion.FieldReleasedAt, v)
	return u
}

// UpdateReleasedAt sets the "released_at" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateReleasedAt() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldReleasedAt)
	return u
}

// ClearReleasedAt clears the value of the "released_at" field.
func (u *ProjectVersionUpsert) ClearReleasedAt() *ProjectVersionUpsert {
	u.SetNull(projectversion.FieldReleasedAt)
	return u
}

// SetLastRunAt sets the "last_run_at" field.
func (u *ProjectVersionUpsert) SetLastRunAt(v time.Time) *ProjectVersionUpsert {
	u.Set(projectversion.FieldLastRunAt, v)
	return u
}

// UpdateLastRunAt sets the "last_run_at" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateLastRunAt() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldLastRunAt)
	return u
}

// ClearLastRunAt clears the value of the "last_run_at" field.
func (u *ProjectVersionUpsert) ClearLastRunAt() *ProjectVersionUpsert {
	u.SetNull(projectversion.FieldLastRunAt)
	return u
}

// SetLatest sets the "latest" field.
func (u *ProjectVersionUpsert) SetLatest(v bool) *ProjectVersionUpsert {
	u.Set(projectversion.FieldLatest, v)
	return u
}

// UpdateLatest sets the "latest" field to the value that was provided on create.
func (u *ProjectVersionUpsert) UpdateLatest() *ProjectVersionUpsert {
	u.SetExcluded(projectversion.FieldLatest)
	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.ProjectVersion.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(projectversion.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ProjectVersionUpsertOne) UpdateNewValues() *ProjectVersionUpsertOne {
	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(projectversion.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(projectversion.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetVersion sets the "version" field.
func (u *ProjectVersionUpsertOne) SetVersion(v string) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetVersion(v)
	})
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateVersion() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateVersion()
	})
}

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

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

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

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

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

// SetProjectID sets the "project_id" field.
func (u *ProjectVersionUpsertOne) SetProjectID(v uuid.UUID) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetProjectID(v)
	})
}

// UpdateProjectID sets the "project_id" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateProjectID() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateProjectID()
	})
}

// SetPrerelease sets the "prerelease" field.
func (u *ProjectVersionUpsertOne) SetPrerelease(v bool) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetPrerelease(v)
	})
}

// UpdatePrerelease sets the "prerelease" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdatePrerelease() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdatePrerelease()
	})
}

// SetWorkflowRunCount sets the "workflow_run_count" field.
func (u *ProjectVersionUpsertOne) SetWorkflowRunCount(v int) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetWorkflowRunCount(v)
	})
}

// AddWorkflowRunCount adds v to the "workflow_run_count" field.
func (u *ProjectVersionUpsertOne) AddWorkflowRunCount(v int) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.AddWorkflowRunCount(v)
	})
}

// UpdateWorkflowRunCount sets the "workflow_run_count" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateWorkflowRunCount() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateWorkflowRunCount()
	})
}

// SetReleasedAt sets the "released_at" field.
func (u *ProjectVersionUpsertOne) SetReleasedAt(v time.Time) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetReleasedAt(v)
	})
}

// UpdateReleasedAt sets the "released_at" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateReleasedAt() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateReleasedAt()
	})
}

// ClearReleasedAt clears the value of the "released_at" field.
func (u *ProjectVersionUpsertOne) ClearReleasedAt() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.ClearReleasedAt()
	})
}

// SetLastRunAt sets the "last_run_at" field.
func (u *ProjectVersionUpsertOne) SetLastRunAt(v time.Time) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetLastRunAt(v)
	})
}

// UpdateLastRunAt sets the "last_run_at" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateLastRunAt() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateLastRunAt()
	})
}

// ClearLastRunAt clears the value of the "last_run_at" field.
func (u *ProjectVersionUpsertOne) ClearLastRunAt() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.ClearLastRunAt()
	})
}

// SetLatest sets the "latest" field.
func (u *ProjectVersionUpsertOne) SetLatest(v bool) *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetLatest(v)
	})
}

// UpdateLatest sets the "latest" field to the value that was provided on create.
func (u *ProjectVersionUpsertOne) UpdateLatest() *ProjectVersionUpsertOne {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateLatest()
	})
}

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

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

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.ProjectVersion.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.ProjectVersionUpsert) {
//			SetVersion(v+v).
//		}).
//		Exec(ctx)
func (pvcb *ProjectVersionCreateBulk) OnConflict(opts ...sql.ConflictOption) *ProjectVersionUpsertBulk {
	pvcb.conflict = opts
	return &ProjectVersionUpsertBulk{
		create: pvcb,
	}
}

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

// ProjectVersionUpsertBulk is the builder for "upsert"-ing
// a bulk of ProjectVersion nodes.
type ProjectVersionUpsertBulk struct {
	create *ProjectVersionCreateBulk
}

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

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

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

// SetVersion sets the "version" field.
func (u *ProjectVersionUpsertBulk) SetVersion(v string) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetVersion(v)
	})
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateVersion() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateVersion()
	})
}

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

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

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

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

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

// SetProjectID sets the "project_id" field.
func (u *ProjectVersionUpsertBulk) SetProjectID(v uuid.UUID) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetProjectID(v)
	})
}

// UpdateProjectID sets the "project_id" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateProjectID() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateProjectID()
	})
}

// SetPrerelease sets the "prerelease" field.
func (u *ProjectVersionUpsertBulk) SetPrerelease(v bool) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetPrerelease(v)
	})
}

// UpdatePrerelease sets the "prerelease" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdatePrerelease() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdatePrerelease()
	})
}

// SetWorkflowRunCount sets the "workflow_run_count" field.
func (u *ProjectVersionUpsertBulk) SetWorkflowRunCount(v int) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetWorkflowRunCount(v)
	})
}

// AddWorkflowRunCount adds v to the "workflow_run_count" field.
func (u *ProjectVersionUpsertBulk) AddWorkflowRunCount(v int) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.AddWorkflowRunCount(v)
	})
}

// UpdateWorkflowRunCount sets the "workflow_run_count" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateWorkflowRunCount() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateWorkflowRunCount()
	})
}

// SetReleasedAt sets the "released_at" field.
func (u *ProjectVersionUpsertBulk) SetReleasedAt(v time.Time) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetReleasedAt(v)
	})
}

// UpdateReleasedAt sets the "released_at" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateReleasedAt() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateReleasedAt()
	})
}

// ClearReleasedAt clears the value of the "released_at" field.
func (u *ProjectVersionUpsertBulk) ClearReleasedAt() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.ClearReleasedAt()
	})
}

// SetLastRunAt sets the "last_run_at" field.
func (u *ProjectVersionUpsertBulk) SetLastRunAt(v time.Time) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetLastRunAt(v)
	})
}

// UpdateLastRunAt sets the "last_run_at" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateLastRunAt() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateLastRunAt()
	})
}

// ClearLastRunAt clears the value of the "last_run_at" field.
func (u *ProjectVersionUpsertBulk) ClearLastRunAt() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.ClearLastRunAt()
	})
}

// SetLatest sets the "latest" field.
func (u *ProjectVersionUpsertBulk) SetLatest(v bool) *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.SetLatest(v)
	})
}

// UpdateLatest sets the "latest" field to the value that was provided on create.
func (u *ProjectVersionUpsertBulk) UpdateLatest() *ProjectVersionUpsertBulk {
	return u.Update(func(s *ProjectVersionUpsert) {
		s.UpdateLatest()
	})
}

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

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