// 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/anzhiyu-c/anheyu-app/ent/entity"
	"github.com/anzhiyu-c/anheyu-app/ent/fileentity"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// EntityCreate is the builder for creating a Entity entity.
type EntityCreate struct {
	config
	mutation *EntityMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetType sets the "type" field.
func (_c *EntityCreate) SetType(v string) *EntityCreate {
	_c.mutation.SetType(v)
	return _c
}

// SetSource sets the "source" field.
func (_c *EntityCreate) SetSource(v string) *EntityCreate {
	_c.mutation.SetSource(v)
	return _c
}

// SetNillableSource sets the "source" field if the given value is not nil.
func (_c *EntityCreate) SetNillableSource(v *string) *EntityCreate {
	if v != nil {
		_c.SetSource(*v)
	}
	return _c
}

// SetSize sets the "size" field.
func (_c *EntityCreate) SetSize(v int64) *EntityCreate {
	_c.mutation.SetSize(v)
	return _c
}

// SetUploadSessionID sets the "upload_session_id" field.
func (_c *EntityCreate) SetUploadSessionID(v string) *EntityCreate {
	_c.mutation.SetUploadSessionID(v)
	return _c
}

// SetNillableUploadSessionID sets the "upload_session_id" field if the given value is not nil.
func (_c *EntityCreate) SetNillableUploadSessionID(v *string) *EntityCreate {
	if v != nil {
		_c.SetUploadSessionID(*v)
	}
	return _c
}

// SetRecycleOptions sets the "recycle_options" field.
func (_c *EntityCreate) SetRecycleOptions(v string) *EntityCreate {
	_c.mutation.SetRecycleOptions(v)
	return _c
}

// SetNillableRecycleOptions sets the "recycle_options" field if the given value is not nil.
func (_c *EntityCreate) SetNillableRecycleOptions(v *string) *EntityCreate {
	if v != nil {
		_c.SetRecycleOptions(*v)
	}
	return _c
}

// SetPolicyID sets the "policy_id" field.
func (_c *EntityCreate) SetPolicyID(v uint) *EntityCreate {
	_c.mutation.SetPolicyID(v)
	return _c
}

// SetCreatedBy sets the "created_by" field.
func (_c *EntityCreate) SetCreatedBy(v uint64) *EntityCreate {
	_c.mutation.SetCreatedBy(v)
	return _c
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_c *EntityCreate) SetNillableCreatedBy(v *uint64) *EntityCreate {
	if v != nil {
		_c.SetCreatedBy(*v)
	}
	return _c
}

// SetEtag sets the "etag" field.
func (_c *EntityCreate) SetEtag(v string) *EntityCreate {
	_c.mutation.SetEtag(v)
	return _c
}

// SetNillableEtag sets the "etag" field if the given value is not nil.
func (_c *EntityCreate) SetNillableEtag(v *string) *EntityCreate {
	if v != nil {
		_c.SetEtag(*v)
	}
	return _c
}

// SetMimeType sets the "mime_type" field.
func (_c *EntityCreate) SetMimeType(v string) *EntityCreate {
	_c.mutation.SetMimeType(v)
	return _c
}

// SetNillableMimeType sets the "mime_type" field if the given value is not nil.
func (_c *EntityCreate) SetNillableMimeType(v *string) *EntityCreate {
	if v != nil {
		_c.SetMimeType(*v)
	}
	return _c
}

// SetDimension sets the "dimension" field.
func (_c *EntityCreate) SetDimension(v string) *EntityCreate {
	_c.mutation.SetDimension(v)
	return _c
}

// SetNillableDimension sets the "dimension" field if the given value is not nil.
func (_c *EntityCreate) SetNillableDimension(v *string) *EntityCreate {
	if v != nil {
		_c.SetDimension(*v)
	}
	return _c
}

// SetStorageMetadata sets the "storage_metadata" field.
func (_c *EntityCreate) SetStorageMetadata(v model.JSONMap) *EntityCreate {
	_c.mutation.SetStorageMetadata(v)
	return _c
}

// SetID sets the "id" field.
func (_c *EntityCreate) SetID(v uint) *EntityCreate {
	_c.mutation.SetID(v)
	return _c
}

// AddFileVersionIDs adds the "file_versions" edge to the FileEntity entity by IDs.
func (_c *EntityCreate) AddFileVersionIDs(ids ...uint) *EntityCreate {
	_c.mutation.AddFileVersionIDs(ids...)
	return _c
}

// AddFileVersions adds the "file_versions" edges to the FileEntity entity.
func (_c *EntityCreate) AddFileVersions(v ...*FileEntity) *EntityCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddFileVersionIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *EntityCreate) 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 *EntityCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := entity.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := entity.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *EntityCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Entity.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Entity.updated_at"`)}
	}
	if _, ok := _c.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Entity.type"`)}
	}
	if _, ok := _c.mutation.Size(); !ok {
		return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "Entity.size"`)}
	}
	if v, ok := _c.mutation.UploadSessionID(); ok {
		if err := entity.UploadSessionIDValidator(v); err != nil {
			return &ValidationError{Name: "upload_session_id", err: fmt.Errorf(`ent: validator failed for field "Entity.upload_session_id": %w`, err)}
		}
	}
	if _, ok := _c.mutation.PolicyID(); !ok {
		return &ValidationError{Name: "policy_id", err: errors.New(`ent: missing required field "Entity.policy_id"`)}
	}
	if v, ok := _c.mutation.Etag(); ok {
		if err := entity.EtagValidator(v); err != nil {
			return &ValidationError{Name: "etag", err: fmt.Errorf(`ent: validator failed for field "Entity.etag": %w`, err)}
		}
	}
	if v, ok := _c.mutation.MimeType(); ok {
		if err := entity.MimeTypeValidator(v); err != nil {
			return &ValidationError{Name: "mime_type", err: fmt.Errorf(`ent: validator failed for field "Entity.mime_type": %w`, err)}
		}
	}
	if v, ok := _c.mutation.Dimension(); ok {
		if err := entity.DimensionValidator(v); err != nil {
			return &ValidationError{Name: "dimension", err: fmt.Errorf(`ent: validator failed for field "Entity.dimension": %w`, err)}
		}
	}
	return nil
}

func (_c *EntityCreate) sqlSave(ctx context.Context) (*Entity, 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
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *EntityCreate) createSpec() (*Entity, *sqlgraph.CreateSpec) {
	var (
		_node = &Entity{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(entity.Table, sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(entity.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(entity.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.GetType(); ok {
		_spec.SetField(entity.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := _c.mutation.Source(); ok {
		_spec.SetField(entity.FieldSource, field.TypeString, value)
		_node.Source = &value
	}
	if value, ok := _c.mutation.Size(); ok {
		_spec.SetField(entity.FieldSize, field.TypeInt64, value)
		_node.Size = value
	}
	if value, ok := _c.mutation.UploadSessionID(); ok {
		_spec.SetField(entity.FieldUploadSessionID, field.TypeString, value)
		_node.UploadSessionID = &value
	}
	if value, ok := _c.mutation.RecycleOptions(); ok {
		_spec.SetField(entity.FieldRecycleOptions, field.TypeString, value)
		_node.RecycleOptions = &value
	}
	if value, ok := _c.mutation.PolicyID(); ok {
		_spec.SetField(entity.FieldPolicyID, field.TypeUint, value)
		_node.PolicyID = value
	}
	if value, ok := _c.mutation.CreatedBy(); ok {
		_spec.SetField(entity.FieldCreatedBy, field.TypeUint64, value)
		_node.CreatedBy = &value
	}
	if value, ok := _c.mutation.Etag(); ok {
		_spec.SetField(entity.FieldEtag, field.TypeString, value)
		_node.Etag = &value
	}
	if value, ok := _c.mutation.MimeType(); ok {
		_spec.SetField(entity.FieldMimeType, field.TypeString, value)
		_node.MimeType = &value
	}
	if value, ok := _c.mutation.Dimension(); ok {
		_spec.SetField(entity.FieldDimension, field.TypeString, value)
		_node.Dimension = &value
	}
	if value, ok := _c.mutation.StorageMetadata(); ok {
		_spec.SetField(entity.FieldStorageMetadata, field.TypeOther, value)
		_node.StorageMetadata = value
	}
	if nodes := _c.mutation.FileVersionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   entity.FileVersionsTable,
			Columns: []string{entity.FileVersionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(fileentity.FieldID, field.TypeUint),
			},
		}
		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.Entity.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.EntityUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *EntityCreate) OnConflict(opts ...sql.ConflictOption) *EntityUpsertOne {
	_c.conflict = opts
	return &EntityUpsertOne{
		create: _c,
	}
}

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

type (
	// EntityUpsertOne is the builder for "upsert"-ing
	//  one Entity node.
	EntityUpsertOne struct {
		create *EntityCreate
	}

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

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

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

// SetType sets the "type" field.
func (u *EntityUpsert) SetType(v string) *EntityUpsert {
	u.Set(entity.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *EntityUpsert) UpdateType() *EntityUpsert {
	u.SetExcluded(entity.FieldType)
	return u
}

// SetSource sets the "source" field.
func (u *EntityUpsert) SetSource(v string) *EntityUpsert {
	u.Set(entity.FieldSource, v)
	return u
}

// UpdateSource sets the "source" field to the value that was provided on create.
func (u *EntityUpsert) UpdateSource() *EntityUpsert {
	u.SetExcluded(entity.FieldSource)
	return u
}

// ClearSource clears the value of the "source" field.
func (u *EntityUpsert) ClearSource() *EntityUpsert {
	u.SetNull(entity.FieldSource)
	return u
}

// SetSize sets the "size" field.
func (u *EntityUpsert) SetSize(v int64) *EntityUpsert {
	u.Set(entity.FieldSize, v)
	return u
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *EntityUpsert) UpdateSize() *EntityUpsert {
	u.SetExcluded(entity.FieldSize)
	return u
}

// AddSize adds v to the "size" field.
func (u *EntityUpsert) AddSize(v int64) *EntityUpsert {
	u.Add(entity.FieldSize, v)
	return u
}

// SetUploadSessionID sets the "upload_session_id" field.
func (u *EntityUpsert) SetUploadSessionID(v string) *EntityUpsert {
	u.Set(entity.FieldUploadSessionID, v)
	return u
}

// UpdateUploadSessionID sets the "upload_session_id" field to the value that was provided on create.
func (u *EntityUpsert) UpdateUploadSessionID() *EntityUpsert {
	u.SetExcluded(entity.FieldUploadSessionID)
	return u
}

// ClearUploadSessionID clears the value of the "upload_session_id" field.
func (u *EntityUpsert) ClearUploadSessionID() *EntityUpsert {
	u.SetNull(entity.FieldUploadSessionID)
	return u
}

// SetRecycleOptions sets the "recycle_options" field.
func (u *EntityUpsert) SetRecycleOptions(v string) *EntityUpsert {
	u.Set(entity.FieldRecycleOptions, v)
	return u
}

// UpdateRecycleOptions sets the "recycle_options" field to the value that was provided on create.
func (u *EntityUpsert) UpdateRecycleOptions() *EntityUpsert {
	u.SetExcluded(entity.FieldRecycleOptions)
	return u
}

// ClearRecycleOptions clears the value of the "recycle_options" field.
func (u *EntityUpsert) ClearRecycleOptions() *EntityUpsert {
	u.SetNull(entity.FieldRecycleOptions)
	return u
}

// SetPolicyID sets the "policy_id" field.
func (u *EntityUpsert) SetPolicyID(v uint) *EntityUpsert {
	u.Set(entity.FieldPolicyID, v)
	return u
}

// UpdatePolicyID sets the "policy_id" field to the value that was provided on create.
func (u *EntityUpsert) UpdatePolicyID() *EntityUpsert {
	u.SetExcluded(entity.FieldPolicyID)
	return u
}

// AddPolicyID adds v to the "policy_id" field.
func (u *EntityUpsert) AddPolicyID(v uint) *EntityUpsert {
	u.Add(entity.FieldPolicyID, v)
	return u
}

// SetCreatedBy sets the "created_by" field.
func (u *EntityUpsert) SetCreatedBy(v uint64) *EntityUpsert {
	u.Set(entity.FieldCreatedBy, v)
	return u
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *EntityUpsert) UpdateCreatedBy() *EntityUpsert {
	u.SetExcluded(entity.FieldCreatedBy)
	return u
}

// AddCreatedBy adds v to the "created_by" field.
func (u *EntityUpsert) AddCreatedBy(v uint64) *EntityUpsert {
	u.Add(entity.FieldCreatedBy, v)
	return u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *EntityUpsert) ClearCreatedBy() *EntityUpsert {
	u.SetNull(entity.FieldCreatedBy)
	return u
}

// SetEtag sets the "etag" field.
func (u *EntityUpsert) SetEtag(v string) *EntityUpsert {
	u.Set(entity.FieldEtag, v)
	return u
}

// UpdateEtag sets the "etag" field to the value that was provided on create.
func (u *EntityUpsert) UpdateEtag() *EntityUpsert {
	u.SetExcluded(entity.FieldEtag)
	return u
}

// ClearEtag clears the value of the "etag" field.
func (u *EntityUpsert) ClearEtag() *EntityUpsert {
	u.SetNull(entity.FieldEtag)
	return u
}

// SetMimeType sets the "mime_type" field.
func (u *EntityUpsert) SetMimeType(v string) *EntityUpsert {
	u.Set(entity.FieldMimeType, v)
	return u
}

// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *EntityUpsert) UpdateMimeType() *EntityUpsert {
	u.SetExcluded(entity.FieldMimeType)
	return u
}

// ClearMimeType clears the value of the "mime_type" field.
func (u *EntityUpsert) ClearMimeType() *EntityUpsert {
	u.SetNull(entity.FieldMimeType)
	return u
}

// SetDimension sets the "dimension" field.
func (u *EntityUpsert) SetDimension(v string) *EntityUpsert {
	u.Set(entity.FieldDimension, v)
	return u
}

// UpdateDimension sets the "dimension" field to the value that was provided on create.
func (u *EntityUpsert) UpdateDimension() *EntityUpsert {
	u.SetExcluded(entity.FieldDimension)
	return u
}

// ClearDimension clears the value of the "dimension" field.
func (u *EntityUpsert) ClearDimension() *EntityUpsert {
	u.SetNull(entity.FieldDimension)
	return u
}

// SetStorageMetadata sets the "storage_metadata" field.
func (u *EntityUpsert) SetStorageMetadata(v model.JSONMap) *EntityUpsert {
	u.Set(entity.FieldStorageMetadata, v)
	return u
}

// UpdateStorageMetadata sets the "storage_metadata" field to the value that was provided on create.
func (u *EntityUpsert) UpdateStorageMetadata() *EntityUpsert {
	u.SetExcluded(entity.FieldStorageMetadata)
	return u
}

// ClearStorageMetadata clears the value of the "storage_metadata" field.
func (u *EntityUpsert) ClearStorageMetadata() *EntityUpsert {
	u.SetNull(entity.FieldStorageMetadata)
	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.Entity.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(entity.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *EntityUpsertOne) UpdateNewValues() *EntityUpsertOne {
	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(entity.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(entity.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

// SetType sets the "type" field.
func (u *EntityUpsertOne) SetType(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateType() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateType()
	})
}

// SetSource sets the "source" field.
func (u *EntityUpsertOne) SetSource(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetSource(v)
	})
}

// UpdateSource sets the "source" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateSource() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateSource()
	})
}

// ClearSource clears the value of the "source" field.
func (u *EntityUpsertOne) ClearSource() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearSource()
	})
}

// SetSize sets the "size" field.
func (u *EntityUpsertOne) SetSize(v int64) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetSize(v)
	})
}

// AddSize adds v to the "size" field.
func (u *EntityUpsertOne) AddSize(v int64) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.AddSize(v)
	})
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateSize() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateSize()
	})
}

// SetUploadSessionID sets the "upload_session_id" field.
func (u *EntityUpsertOne) SetUploadSessionID(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetUploadSessionID(v)
	})
}

// UpdateUploadSessionID sets the "upload_session_id" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateUploadSessionID() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateUploadSessionID()
	})
}

// ClearUploadSessionID clears the value of the "upload_session_id" field.
func (u *EntityUpsertOne) ClearUploadSessionID() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearUploadSessionID()
	})
}

// SetRecycleOptions sets the "recycle_options" field.
func (u *EntityUpsertOne) SetRecycleOptions(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetRecycleOptions(v)
	})
}

// UpdateRecycleOptions sets the "recycle_options" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateRecycleOptions() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateRecycleOptions()
	})
}

// ClearRecycleOptions clears the value of the "recycle_options" field.
func (u *EntityUpsertOne) ClearRecycleOptions() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearRecycleOptions()
	})
}

// SetPolicyID sets the "policy_id" field.
func (u *EntityUpsertOne) SetPolicyID(v uint) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetPolicyID(v)
	})
}

// AddPolicyID adds v to the "policy_id" field.
func (u *EntityUpsertOne) AddPolicyID(v uint) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.AddPolicyID(v)
	})
}

// UpdatePolicyID sets the "policy_id" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdatePolicyID() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdatePolicyID()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *EntityUpsertOne) SetCreatedBy(v uint64) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *EntityUpsertOne) AddCreatedBy(v uint64) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateCreatedBy() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateCreatedBy()
	})
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *EntityUpsertOne) ClearCreatedBy() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearCreatedBy()
	})
}

// SetEtag sets the "etag" field.
func (u *EntityUpsertOne) SetEtag(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetEtag(v)
	})
}

// UpdateEtag sets the "etag" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateEtag() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateEtag()
	})
}

// ClearEtag clears the value of the "etag" field.
func (u *EntityUpsertOne) ClearEtag() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearEtag()
	})
}

// SetMimeType sets the "mime_type" field.
func (u *EntityUpsertOne) SetMimeType(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetMimeType(v)
	})
}

// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateMimeType() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateMimeType()
	})
}

// ClearMimeType clears the value of the "mime_type" field.
func (u *EntityUpsertOne) ClearMimeType() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearMimeType()
	})
}

// SetDimension sets the "dimension" field.
func (u *EntityUpsertOne) SetDimension(v string) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetDimension(v)
	})
}

// UpdateDimension sets the "dimension" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateDimension() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateDimension()
	})
}

// ClearDimension clears the value of the "dimension" field.
func (u *EntityUpsertOne) ClearDimension() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearDimension()
	})
}

// SetStorageMetadata sets the "storage_metadata" field.
func (u *EntityUpsertOne) SetStorageMetadata(v model.JSONMap) *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.SetStorageMetadata(v)
	})
}

// UpdateStorageMetadata sets the "storage_metadata" field to the value that was provided on create.
func (u *EntityUpsertOne) UpdateStorageMetadata() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateStorageMetadata()
	})
}

// ClearStorageMetadata clears the value of the "storage_metadata" field.
func (u *EntityUpsertOne) ClearStorageMetadata() *EntityUpsertOne {
	return u.Update(func(s *EntityUpsert) {
		s.ClearStorageMetadata()
	})
}

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

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

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

// Save creates the Entity entities in the database.
func (_c *EntityCreateBulk) Save(ctx context.Context) ([]*Entity, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Entity, 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.(*EntityMutation)
				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 && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint(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 *EntityCreateBulk) SaveX(ctx context.Context) []*Entity {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *EntityCreateBulk) 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.Entity.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.EntityUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *EntityCreateBulk) OnConflict(opts ...sql.ConflictOption) *EntityUpsertBulk {
	_c.conflict = opts
	return &EntityUpsertBulk{
		create: _c,
	}
}

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

// EntityUpsertBulk is the builder for "upsert"-ing
// a bulk of Entity nodes.
type EntityUpsertBulk struct {
	create *EntityCreateBulk
}

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

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

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

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

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

// SetType sets the "type" field.
func (u *EntityUpsertBulk) SetType(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateType() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateType()
	})
}

// SetSource sets the "source" field.
func (u *EntityUpsertBulk) SetSource(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetSource(v)
	})
}

// UpdateSource sets the "source" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateSource() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateSource()
	})
}

// ClearSource clears the value of the "source" field.
func (u *EntityUpsertBulk) ClearSource() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearSource()
	})
}

// SetSize sets the "size" field.
func (u *EntityUpsertBulk) SetSize(v int64) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetSize(v)
	})
}

// AddSize adds v to the "size" field.
func (u *EntityUpsertBulk) AddSize(v int64) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.AddSize(v)
	})
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateSize() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateSize()
	})
}

// SetUploadSessionID sets the "upload_session_id" field.
func (u *EntityUpsertBulk) SetUploadSessionID(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetUploadSessionID(v)
	})
}

// UpdateUploadSessionID sets the "upload_session_id" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateUploadSessionID() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateUploadSessionID()
	})
}

// ClearUploadSessionID clears the value of the "upload_session_id" field.
func (u *EntityUpsertBulk) ClearUploadSessionID() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearUploadSessionID()
	})
}

// SetRecycleOptions sets the "recycle_options" field.
func (u *EntityUpsertBulk) SetRecycleOptions(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetRecycleOptions(v)
	})
}

// UpdateRecycleOptions sets the "recycle_options" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateRecycleOptions() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateRecycleOptions()
	})
}

// ClearRecycleOptions clears the value of the "recycle_options" field.
func (u *EntityUpsertBulk) ClearRecycleOptions() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearRecycleOptions()
	})
}

// SetPolicyID sets the "policy_id" field.
func (u *EntityUpsertBulk) SetPolicyID(v uint) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetPolicyID(v)
	})
}

// AddPolicyID adds v to the "policy_id" field.
func (u *EntityUpsertBulk) AddPolicyID(v uint) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.AddPolicyID(v)
	})
}

// UpdatePolicyID sets the "policy_id" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdatePolicyID() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdatePolicyID()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *EntityUpsertBulk) SetCreatedBy(v uint64) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *EntityUpsertBulk) AddCreatedBy(v uint64) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateCreatedBy() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateCreatedBy()
	})
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *EntityUpsertBulk) ClearCreatedBy() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearCreatedBy()
	})
}

// SetEtag sets the "etag" field.
func (u *EntityUpsertBulk) SetEtag(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetEtag(v)
	})
}

// UpdateEtag sets the "etag" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateEtag() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateEtag()
	})
}

// ClearEtag clears the value of the "etag" field.
func (u *EntityUpsertBulk) ClearEtag() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearEtag()
	})
}

// SetMimeType sets the "mime_type" field.
func (u *EntityUpsertBulk) SetMimeType(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetMimeType(v)
	})
}

// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateMimeType() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateMimeType()
	})
}

// ClearMimeType clears the value of the "mime_type" field.
func (u *EntityUpsertBulk) ClearMimeType() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearMimeType()
	})
}

// SetDimension sets the "dimension" field.
func (u *EntityUpsertBulk) SetDimension(v string) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetDimension(v)
	})
}

// UpdateDimension sets the "dimension" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateDimension() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateDimension()
	})
}

// ClearDimension clears the value of the "dimension" field.
func (u *EntityUpsertBulk) ClearDimension() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearDimension()
	})
}

// SetStorageMetadata sets the "storage_metadata" field.
func (u *EntityUpsertBulk) SetStorageMetadata(v model.JSONMap) *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.SetStorageMetadata(v)
	})
}

// UpdateStorageMetadata sets the "storage_metadata" field to the value that was provided on create.
func (u *EntityUpsertBulk) UpdateStorageMetadata() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.UpdateStorageMetadata()
	})
}

// ClearStorageMetadata clears the value of the "storage_metadata" field.
func (u *EntityUpsertBulk) ClearStorageMetadata() *EntityUpsertBulk {
	return u.Update(func(s *EntityUpsert) {
		s.ClearStorageMetadata()
	})
}

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

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