// 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/Wei-Shaw/sub2api/ent/compositemodelroute"
	"github.com/Wei-Shaw/sub2api/ent/group"
)

// CompositeModelRouteCreate is the builder for creating a CompositeModelRoute entity.
type CompositeModelRouteCreate struct {
	config
	mutation *CompositeModelRouteMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (_c *CompositeModelRouteCreate) SetCreatedAt(v time.Time) *CompositeModelRouteCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_c *CompositeModelRouteCreate) SetUpdatedAt(v time.Time) *CompositeModelRouteCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

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

// SetDeletedAt sets the "deleted_at" field.
func (_c *CompositeModelRouteCreate) SetDeletedAt(v time.Time) *CompositeModelRouteCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

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

// SetGroupID sets the "group_id" field.
func (_c *CompositeModelRouteCreate) SetGroupID(v int64) *CompositeModelRouteCreate {
	_c.mutation.SetGroupID(v)
	return _c
}

// SetPublicModel sets the "public_model" field.
func (_c *CompositeModelRouteCreate) SetPublicModel(v string) *CompositeModelRouteCreate {
	_c.mutation.SetPublicModel(v)
	return _c
}

// SetMatchType sets the "match_type" field.
func (_c *CompositeModelRouteCreate) SetMatchType(v string) *CompositeModelRouteCreate {
	_c.mutation.SetMatchType(v)
	return _c
}

// SetNillableMatchType sets the "match_type" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableMatchType(v *string) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetMatchType(*v)
	}
	return _c
}

// SetTargetPlatform sets the "target_platform" field.
func (_c *CompositeModelRouteCreate) SetTargetPlatform(v string) *CompositeModelRouteCreate {
	_c.mutation.SetTargetPlatform(v)
	return _c
}

// SetNillableTargetPlatform sets the "target_platform" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableTargetPlatform(v *string) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetTargetPlatform(*v)
	}
	return _c
}

// SetUpstreamModel sets the "upstream_model" field.
func (_c *CompositeModelRouteCreate) SetUpstreamModel(v string) *CompositeModelRouteCreate {
	_c.mutation.SetUpstreamModel(v)
	return _c
}

// SetNillableUpstreamModel sets the "upstream_model" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableUpstreamModel(v *string) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetUpstreamModel(*v)
	}
	return _c
}

// SetEndpoint sets the "endpoint" field.
func (_c *CompositeModelRouteCreate) SetEndpoint(v string) *CompositeModelRouteCreate {
	_c.mutation.SetEndpoint(v)
	return _c
}

// SetNillableEndpoint sets the "endpoint" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableEndpoint(v *string) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetEndpoint(*v)
	}
	return _c
}

// SetPriority sets the "priority" field.
func (_c *CompositeModelRouteCreate) SetPriority(v int) *CompositeModelRouteCreate {
	_c.mutation.SetPriority(v)
	return _c
}

// SetNillablePriority sets the "priority" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillablePriority(v *int) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetPriority(*v)
	}
	return _c
}

// SetEnabled sets the "enabled" field.
func (_c *CompositeModelRouteCreate) SetEnabled(v bool) *CompositeModelRouteCreate {
	_c.mutation.SetEnabled(v)
	return _c
}

// SetNillableEnabled sets the "enabled" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableEnabled(v *bool) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetEnabled(*v)
	}
	return _c
}

// SetNotes sets the "notes" field.
func (_c *CompositeModelRouteCreate) SetNotes(v string) *CompositeModelRouteCreate {
	_c.mutation.SetNotes(v)
	return _c
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_c *CompositeModelRouteCreate) SetNillableNotes(v *string) *CompositeModelRouteCreate {
	if v != nil {
		_c.SetNotes(*v)
	}
	return _c
}

// SetGroup sets the "group" edge to the Group entity.
func (_c *CompositeModelRouteCreate) SetGroup(v *Group) *CompositeModelRouteCreate {
	return _c.SetGroupID(v.ID)
}

// Mutation returns the CompositeModelRouteMutation object of the builder.
func (_c *CompositeModelRouteCreate) Mutation() *CompositeModelRouteMutation {
	return _c.mutation
}

// Save creates the CompositeModelRoute in the database.
func (_c *CompositeModelRouteCreate) Save(ctx context.Context) (*CompositeModelRoute, error) {
	if err := _c.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_c *CompositeModelRouteCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if compositemodelroute.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized compositemodelroute.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := compositemodelroute.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if compositemodelroute.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized compositemodelroute.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := compositemodelroute.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.MatchType(); !ok {
		v := compositemodelroute.DefaultMatchType
		_c.mutation.SetMatchType(v)
	}
	if _, ok := _c.mutation.TargetPlatform(); !ok {
		v := compositemodelroute.DefaultTargetPlatform
		_c.mutation.SetTargetPlatform(v)
	}
	if _, ok := _c.mutation.UpstreamModel(); !ok {
		v := compositemodelroute.DefaultUpstreamModel
		_c.mutation.SetUpstreamModel(v)
	}
	if _, ok := _c.mutation.Endpoint(); !ok {
		v := compositemodelroute.DefaultEndpoint
		_c.mutation.SetEndpoint(v)
	}
	if _, ok := _c.mutation.Priority(); !ok {
		v := compositemodelroute.DefaultPriority
		_c.mutation.SetPriority(v)
	}
	if _, ok := _c.mutation.Enabled(); !ok {
		v := compositemodelroute.DefaultEnabled
		_c.mutation.SetEnabled(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *CompositeModelRouteCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "CompositeModelRoute.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "CompositeModelRoute.updated_at"`)}
	}
	if _, ok := _c.mutation.GroupID(); !ok {
		return &ValidationError{Name: "group_id", err: errors.New(`ent: missing required field "CompositeModelRoute.group_id"`)}
	}
	if _, ok := _c.mutation.PublicModel(); !ok {
		return &ValidationError{Name: "public_model", err: errors.New(`ent: missing required field "CompositeModelRoute.public_model"`)}
	}
	if v, ok := _c.mutation.PublicModel(); ok {
		if err := compositemodelroute.PublicModelValidator(v); err != nil {
			return &ValidationError{Name: "public_model", err: fmt.Errorf(`ent: validator failed for field "CompositeModelRoute.public_model": %w`, err)}
		}
	}
	if _, ok := _c.mutation.MatchType(); !ok {
		return &ValidationError{Name: "match_type", err: errors.New(`ent: missing required field "CompositeModelRoute.match_type"`)}
	}
	if v, ok := _c.mutation.MatchType(); ok {
		if err := compositemodelroute.MatchTypeValidator(v); err != nil {
			return &ValidationError{Name: "match_type", err: fmt.Errorf(`ent: validator failed for field "CompositeModelRoute.match_type": %w`, err)}
		}
	}
	if _, ok := _c.mutation.TargetPlatform(); !ok {
		return &ValidationError{Name: "target_platform", err: errors.New(`ent: missing required field "CompositeModelRoute.target_platform"`)}
	}
	if v, ok := _c.mutation.TargetPlatform(); ok {
		if err := compositemodelroute.TargetPlatformValidator(v); err != nil {
			return &ValidationError{Name: "target_platform", err: fmt.Errorf(`ent: validator failed for field "CompositeModelRoute.target_platform": %w`, err)}
		}
	}
	if _, ok := _c.mutation.UpstreamModel(); !ok {
		return &ValidationError{Name: "upstream_model", err: errors.New(`ent: missing required field "CompositeModelRoute.upstream_model"`)}
	}
	if v, ok := _c.mutation.UpstreamModel(); ok {
		if err := compositemodelroute.UpstreamModelValidator(v); err != nil {
			return &ValidationError{Name: "upstream_model", err: fmt.Errorf(`ent: validator failed for field "CompositeModelRoute.upstream_model": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Endpoint(); !ok {
		return &ValidationError{Name: "endpoint", err: errors.New(`ent: missing required field "CompositeModelRoute.endpoint"`)}
	}
	if v, ok := _c.mutation.Endpoint(); ok {
		if err := compositemodelroute.EndpointValidator(v); err != nil {
			return &ValidationError{Name: "endpoint", err: fmt.Errorf(`ent: validator failed for field "CompositeModelRoute.endpoint": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Priority(); !ok {
		return &ValidationError{Name: "priority", err: errors.New(`ent: missing required field "CompositeModelRoute.priority"`)}
	}
	if _, ok := _c.mutation.Enabled(); !ok {
		return &ValidationError{Name: "enabled", err: errors.New(`ent: missing required field "CompositeModelRoute.enabled"`)}
	}
	if len(_c.mutation.GroupIDs()) == 0 {
		return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "CompositeModelRoute.group"`)}
	}
	return nil
}

func (_c *CompositeModelRouteCreate) sqlSave(ctx context.Context) (*CompositeModelRoute, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int64(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *CompositeModelRouteCreate) createSpec() (*CompositeModelRoute, *sqlgraph.CreateSpec) {
	var (
		_node = &CompositeModelRoute{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(compositemodelroute.Table, sqlgraph.NewFieldSpec(compositemodelroute.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(compositemodelroute.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(compositemodelroute.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(compositemodelroute.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.PublicModel(); ok {
		_spec.SetField(compositemodelroute.FieldPublicModel, field.TypeString, value)
		_node.PublicModel = value
	}
	if value, ok := _c.mutation.MatchType(); ok {
		_spec.SetField(compositemodelroute.FieldMatchType, field.TypeString, value)
		_node.MatchType = value
	}
	if value, ok := _c.mutation.TargetPlatform(); ok {
		_spec.SetField(compositemodelroute.FieldTargetPlatform, field.TypeString, value)
		_node.TargetPlatform = value
	}
	if value, ok := _c.mutation.UpstreamModel(); ok {
		_spec.SetField(compositemodelroute.FieldUpstreamModel, field.TypeString, value)
		_node.UpstreamModel = value
	}
	if value, ok := _c.mutation.Endpoint(); ok {
		_spec.SetField(compositemodelroute.FieldEndpoint, field.TypeString, value)
		_node.Endpoint = value
	}
	if value, ok := _c.mutation.Priority(); ok {
		_spec.SetField(compositemodelroute.FieldPriority, field.TypeInt, value)
		_node.Priority = value
	}
	if value, ok := _c.mutation.Enabled(); ok {
		_spec.SetField(compositemodelroute.FieldEnabled, field.TypeBool, value)
		_node.Enabled = value
	}
	if value, ok := _c.mutation.Notes(); ok {
		_spec.SetField(compositemodelroute.FieldNotes, field.TypeString, value)
		_node.Notes = &value
	}
	if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   compositemodelroute.GroupTable,
			Columns: []string{compositemodelroute.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.GroupID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.CompositeModelRoute.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.CompositeModelRouteUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *CompositeModelRouteCreate) OnConflict(opts ...sql.ConflictOption) *CompositeModelRouteUpsertOne {
	_c.conflict = opts
	return &CompositeModelRouteUpsertOne{
		create: _c,
	}
}

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

type (
	// CompositeModelRouteUpsertOne is the builder for "upsert"-ing
	//  one CompositeModelRoute node.
	CompositeModelRouteUpsertOne struct {
		create *CompositeModelRouteCreate
	}

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

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

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

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

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

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

// SetGroupID sets the "group_id" field.
func (u *CompositeModelRouteUpsert) SetGroupID(v int64) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldGroupID, v)
	return u
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateGroupID() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldGroupID)
	return u
}

// SetPublicModel sets the "public_model" field.
func (u *CompositeModelRouteUpsert) SetPublicModel(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldPublicModel, v)
	return u
}

// UpdatePublicModel sets the "public_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdatePublicModel() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldPublicModel)
	return u
}

// SetMatchType sets the "match_type" field.
func (u *CompositeModelRouteUpsert) SetMatchType(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldMatchType, v)
	return u
}

// UpdateMatchType sets the "match_type" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateMatchType() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldMatchType)
	return u
}

// SetTargetPlatform sets the "target_platform" field.
func (u *CompositeModelRouteUpsert) SetTargetPlatform(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldTargetPlatform, v)
	return u
}

// UpdateTargetPlatform sets the "target_platform" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateTargetPlatform() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldTargetPlatform)
	return u
}

// SetUpstreamModel sets the "upstream_model" field.
func (u *CompositeModelRouteUpsert) SetUpstreamModel(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldUpstreamModel, v)
	return u
}

// UpdateUpstreamModel sets the "upstream_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateUpstreamModel() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldUpstreamModel)
	return u
}

// SetEndpoint sets the "endpoint" field.
func (u *CompositeModelRouteUpsert) SetEndpoint(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldEndpoint, v)
	return u
}

// UpdateEndpoint sets the "endpoint" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateEndpoint() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldEndpoint)
	return u
}

// SetPriority sets the "priority" field.
func (u *CompositeModelRouteUpsert) SetPriority(v int) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldPriority, v)
	return u
}

// UpdatePriority sets the "priority" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdatePriority() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldPriority)
	return u
}

// AddPriority adds v to the "priority" field.
func (u *CompositeModelRouteUpsert) AddPriority(v int) *CompositeModelRouteUpsert {
	u.Add(compositemodelroute.FieldPriority, v)
	return u
}

// SetEnabled sets the "enabled" field.
func (u *CompositeModelRouteUpsert) SetEnabled(v bool) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldEnabled, v)
	return u
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateEnabled() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldEnabled)
	return u
}

// SetNotes sets the "notes" field.
func (u *CompositeModelRouteUpsert) SetNotes(v string) *CompositeModelRouteUpsert {
	u.Set(compositemodelroute.FieldNotes, v)
	return u
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *CompositeModelRouteUpsert) UpdateNotes() *CompositeModelRouteUpsert {
	u.SetExcluded(compositemodelroute.FieldNotes)
	return u
}

// ClearNotes clears the value of the "notes" field.
func (u *CompositeModelRouteUpsert) ClearNotes() *CompositeModelRouteUpsert {
	u.SetNull(compositemodelroute.FieldNotes)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.CompositeModelRoute.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *CompositeModelRouteUpsertOne) UpdateNewValues() *CompositeModelRouteUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(compositemodelroute.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetGroupID sets the "group_id" field.
func (u *CompositeModelRouteUpsertOne) SetGroupID(v int64) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateGroupID() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateGroupID()
	})
}

// SetPublicModel sets the "public_model" field.
func (u *CompositeModelRouteUpsertOne) SetPublicModel(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetPublicModel(v)
	})
}

// UpdatePublicModel sets the "public_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdatePublicModel() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdatePublicModel()
	})
}

// SetMatchType sets the "match_type" field.
func (u *CompositeModelRouteUpsertOne) SetMatchType(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetMatchType(v)
	})
}

// UpdateMatchType sets the "match_type" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateMatchType() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateMatchType()
	})
}

// SetTargetPlatform sets the "target_platform" field.
func (u *CompositeModelRouteUpsertOne) SetTargetPlatform(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetTargetPlatform(v)
	})
}

// UpdateTargetPlatform sets the "target_platform" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateTargetPlatform() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateTargetPlatform()
	})
}

// SetUpstreamModel sets the "upstream_model" field.
func (u *CompositeModelRouteUpsertOne) SetUpstreamModel(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetUpstreamModel(v)
	})
}

// UpdateUpstreamModel sets the "upstream_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateUpstreamModel() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateUpstreamModel()
	})
}

// SetEndpoint sets the "endpoint" field.
func (u *CompositeModelRouteUpsertOne) SetEndpoint(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetEndpoint(v)
	})
}

// UpdateEndpoint sets the "endpoint" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateEndpoint() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateEndpoint()
	})
}

// SetPriority sets the "priority" field.
func (u *CompositeModelRouteUpsertOne) SetPriority(v int) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetPriority(v)
	})
}

// AddPriority adds v to the "priority" field.
func (u *CompositeModelRouteUpsertOne) AddPriority(v int) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.AddPriority(v)
	})
}

// UpdatePriority sets the "priority" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdatePriority() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdatePriority()
	})
}

// SetEnabled sets the "enabled" field.
func (u *CompositeModelRouteUpsertOne) SetEnabled(v bool) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetEnabled(v)
	})
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateEnabled() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateEnabled()
	})
}

// SetNotes sets the "notes" field.
func (u *CompositeModelRouteUpsertOne) SetNotes(v string) *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetNotes(v)
	})
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertOne) UpdateNotes() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateNotes()
	})
}

// ClearNotes clears the value of the "notes" field.
func (u *CompositeModelRouteUpsertOne) ClearNotes() *CompositeModelRouteUpsertOne {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.ClearNotes()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *CompositeModelRouteUpsertOne) 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 *CompositeModelRouteUpsertOne) ID(ctx context.Context) (id int64, err error) {
	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 *CompositeModelRouteUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

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

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

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

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

// CompositeModelRouteUpsertBulk is the builder for "upsert"-ing
// a bulk of CompositeModelRoute nodes.
type CompositeModelRouteUpsertBulk struct {
	create *CompositeModelRouteCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.CompositeModelRoute.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *CompositeModelRouteUpsertBulk) UpdateNewValues() *CompositeModelRouteUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(compositemodelroute.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetGroupID sets the "group_id" field.
func (u *CompositeModelRouteUpsertBulk) SetGroupID(v int64) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateGroupID() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateGroupID()
	})
}

// SetPublicModel sets the "public_model" field.
func (u *CompositeModelRouteUpsertBulk) SetPublicModel(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetPublicModel(v)
	})
}

// UpdatePublicModel sets the "public_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdatePublicModel() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdatePublicModel()
	})
}

// SetMatchType sets the "match_type" field.
func (u *CompositeModelRouteUpsertBulk) SetMatchType(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetMatchType(v)
	})
}

// UpdateMatchType sets the "match_type" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateMatchType() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateMatchType()
	})
}

// SetTargetPlatform sets the "target_platform" field.
func (u *CompositeModelRouteUpsertBulk) SetTargetPlatform(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetTargetPlatform(v)
	})
}

// UpdateTargetPlatform sets the "target_platform" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateTargetPlatform() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateTargetPlatform()
	})
}

// SetUpstreamModel sets the "upstream_model" field.
func (u *CompositeModelRouteUpsertBulk) SetUpstreamModel(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetUpstreamModel(v)
	})
}

// UpdateUpstreamModel sets the "upstream_model" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateUpstreamModel() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateUpstreamModel()
	})
}

// SetEndpoint sets the "endpoint" field.
func (u *CompositeModelRouteUpsertBulk) SetEndpoint(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetEndpoint(v)
	})
}

// UpdateEndpoint sets the "endpoint" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateEndpoint() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateEndpoint()
	})
}

// SetPriority sets the "priority" field.
func (u *CompositeModelRouteUpsertBulk) SetPriority(v int) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetPriority(v)
	})
}

// AddPriority adds v to the "priority" field.
func (u *CompositeModelRouteUpsertBulk) AddPriority(v int) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.AddPriority(v)
	})
}

// UpdatePriority sets the "priority" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdatePriority() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdatePriority()
	})
}

// SetEnabled sets the "enabled" field.
func (u *CompositeModelRouteUpsertBulk) SetEnabled(v bool) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetEnabled(v)
	})
}

// UpdateEnabled sets the "enabled" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateEnabled() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateEnabled()
	})
}

// SetNotes sets the "notes" field.
func (u *CompositeModelRouteUpsertBulk) SetNotes(v string) *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.SetNotes(v)
	})
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *CompositeModelRouteUpsertBulk) UpdateNotes() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.UpdateNotes()
	})
}

// ClearNotes clears the value of the "notes" field.
func (u *CompositeModelRouteUpsertBulk) ClearNotes() *CompositeModelRouteUpsertBulk {
	return u.Update(func(s *CompositeModelRouteUpsert) {
		s.ClearNotes()
	})
}

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

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