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

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

// Where appends a list predicates to the EntityUpdate builder.
func (_u *EntityUpdate) Where(ps ...predicate.Entity) *EntityUpdate {
	_u.mutation.Where(ps...)
	return _u
}

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

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

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *EntityUpdate) SetNillableType(v *string) *EntityUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

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

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

// ClearSource clears the value of the "source" field.
func (_u *EntityUpdate) ClearSource() *EntityUpdate {
	_u.mutation.ClearSource()
	return _u
}

// SetSize sets the "size" field.
func (_u *EntityUpdate) SetSize(v int64) *EntityUpdate {
	_u.mutation.ResetSize()
	_u.mutation.SetSize(v)
	return _u
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_u *EntityUpdate) SetNillableSize(v *int64) *EntityUpdate {
	if v != nil {
		_u.SetSize(*v)
	}
	return _u
}

// AddSize adds value to the "size" field.
func (_u *EntityUpdate) AddSize(v int64) *EntityUpdate {
	_u.mutation.AddSize(v)
	return _u
}

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

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

// ClearUploadSessionID clears the value of the "upload_session_id" field.
func (_u *EntityUpdate) ClearUploadSessionID() *EntityUpdate {
	_u.mutation.ClearUploadSessionID()
	return _u
}

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

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

// ClearRecycleOptions clears the value of the "recycle_options" field.
func (_u *EntityUpdate) ClearRecycleOptions() *EntityUpdate {
	_u.mutation.ClearRecycleOptions()
	return _u
}

// SetPolicyID sets the "policy_id" field.
func (_u *EntityUpdate) SetPolicyID(v uint) *EntityUpdate {
	_u.mutation.ResetPolicyID()
	_u.mutation.SetPolicyID(v)
	return _u
}

// SetNillablePolicyID sets the "policy_id" field if the given value is not nil.
func (_u *EntityUpdate) SetNillablePolicyID(v *uint) *EntityUpdate {
	if v != nil {
		_u.SetPolicyID(*v)
	}
	return _u
}

// AddPolicyID adds value to the "policy_id" field.
func (_u *EntityUpdate) AddPolicyID(v int) *EntityUpdate {
	_u.mutation.AddPolicyID(v)
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *EntityUpdate) SetCreatedBy(v uint64) *EntityUpdate {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

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

// AddCreatedBy adds value to the "created_by" field.
func (_u *EntityUpdate) AddCreatedBy(v int64) *EntityUpdate {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (_u *EntityUpdate) ClearCreatedBy() *EntityUpdate {
	_u.mutation.ClearCreatedBy()
	return _u
}

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

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

// ClearEtag clears the value of the "etag" field.
func (_u *EntityUpdate) ClearEtag() *EntityUpdate {
	_u.mutation.ClearEtag()
	return _u
}

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

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

// ClearMimeType clears the value of the "mime_type" field.
func (_u *EntityUpdate) ClearMimeType() *EntityUpdate {
	_u.mutation.ClearMimeType()
	return _u
}

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

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

// ClearDimension clears the value of the "dimension" field.
func (_u *EntityUpdate) ClearDimension() *EntityUpdate {
	_u.mutation.ClearDimension()
	return _u
}

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

// ClearStorageMetadata clears the value of the "storage_metadata" field.
func (_u *EntityUpdate) ClearStorageMetadata() *EntityUpdate {
	_u.mutation.ClearStorageMetadata()
	return _u
}

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

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

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

// ClearFileVersions clears all "file_versions" edges to the FileEntity entity.
func (_u *EntityUpdate) ClearFileVersions() *EntityUpdate {
	_u.mutation.ClearFileVersions()
	return _u
}

// RemoveFileVersionIDs removes the "file_versions" edge to FileEntity entities by IDs.
func (_u *EntityUpdate) RemoveFileVersionIDs(ids ...uint) *EntityUpdate {
	_u.mutation.RemoveFileVersionIDs(ids...)
	return _u
}

// RemoveFileVersions removes "file_versions" edges to FileEntity entities.
func (_u *EntityUpdate) RemoveFileVersions(v ...*FileEntity) *EntityUpdate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveFileVersionIDs(ids...)
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *EntityUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := entity.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

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

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

func (_u *EntityUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(entity.Table, entity.Columns, sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUint))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(entity.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(entity.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Source(); ok {
		_spec.SetField(entity.FieldSource, field.TypeString, value)
	}
	if _u.mutation.SourceCleared() {
		_spec.ClearField(entity.FieldSource, field.TypeString)
	}
	if value, ok := _u.mutation.Size(); ok {
		_spec.SetField(entity.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSize(); ok {
		_spec.AddField(entity.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.UploadSessionID(); ok {
		_spec.SetField(entity.FieldUploadSessionID, field.TypeString, value)
	}
	if _u.mutation.UploadSessionIDCleared() {
		_spec.ClearField(entity.FieldUploadSessionID, field.TypeString)
	}
	if value, ok := _u.mutation.RecycleOptions(); ok {
		_spec.SetField(entity.FieldRecycleOptions, field.TypeString, value)
	}
	if _u.mutation.RecycleOptionsCleared() {
		_spec.ClearField(entity.FieldRecycleOptions, field.TypeString)
	}
	if value, ok := _u.mutation.PolicyID(); ok {
		_spec.SetField(entity.FieldPolicyID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedPolicyID(); ok {
		_spec.AddField(entity.FieldPolicyID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(entity.FieldCreatedBy, field.TypeUint64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(entity.FieldCreatedBy, field.TypeUint64, value)
	}
	if _u.mutation.CreatedByCleared() {
		_spec.ClearField(entity.FieldCreatedBy, field.TypeUint64)
	}
	if value, ok := _u.mutation.Etag(); ok {
		_spec.SetField(entity.FieldEtag, field.TypeString, value)
	}
	if _u.mutation.EtagCleared() {
		_spec.ClearField(entity.FieldEtag, field.TypeString)
	}
	if value, ok := _u.mutation.MimeType(); ok {
		_spec.SetField(entity.FieldMimeType, field.TypeString, value)
	}
	if _u.mutation.MimeTypeCleared() {
		_spec.ClearField(entity.FieldMimeType, field.TypeString)
	}
	if value, ok := _u.mutation.Dimension(); ok {
		_spec.SetField(entity.FieldDimension, field.TypeString, value)
	}
	if _u.mutation.DimensionCleared() {
		_spec.ClearField(entity.FieldDimension, field.TypeString)
	}
	if value, ok := _u.mutation.StorageMetadata(); ok {
		_spec.SetField(entity.FieldStorageMetadata, field.TypeOther, value)
	}
	if _u.mutation.StorageMetadataCleared() {
		_spec.ClearField(entity.FieldStorageMetadata, field.TypeOther)
	}
	if _u.mutation.FileVersionsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedFileVersionsIDs(); len(nodes) > 0 && !_u.mutation.FileVersionsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.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.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{entity.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *EntityUpdateOne) SetNillableType(v *string) *EntityUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

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

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

// ClearSource clears the value of the "source" field.
func (_u *EntityUpdateOne) ClearSource() *EntityUpdateOne {
	_u.mutation.ClearSource()
	return _u
}

// SetSize sets the "size" field.
func (_u *EntityUpdateOne) SetSize(v int64) *EntityUpdateOne {
	_u.mutation.ResetSize()
	_u.mutation.SetSize(v)
	return _u
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_u *EntityUpdateOne) SetNillableSize(v *int64) *EntityUpdateOne {
	if v != nil {
		_u.SetSize(*v)
	}
	return _u
}

// AddSize adds value to the "size" field.
func (_u *EntityUpdateOne) AddSize(v int64) *EntityUpdateOne {
	_u.mutation.AddSize(v)
	return _u
}

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

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

// ClearUploadSessionID clears the value of the "upload_session_id" field.
func (_u *EntityUpdateOne) ClearUploadSessionID() *EntityUpdateOne {
	_u.mutation.ClearUploadSessionID()
	return _u
}

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

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

// ClearRecycleOptions clears the value of the "recycle_options" field.
func (_u *EntityUpdateOne) ClearRecycleOptions() *EntityUpdateOne {
	_u.mutation.ClearRecycleOptions()
	return _u
}

// SetPolicyID sets the "policy_id" field.
func (_u *EntityUpdateOne) SetPolicyID(v uint) *EntityUpdateOne {
	_u.mutation.ResetPolicyID()
	_u.mutation.SetPolicyID(v)
	return _u
}

// SetNillablePolicyID sets the "policy_id" field if the given value is not nil.
func (_u *EntityUpdateOne) SetNillablePolicyID(v *uint) *EntityUpdateOne {
	if v != nil {
		_u.SetPolicyID(*v)
	}
	return _u
}

// AddPolicyID adds value to the "policy_id" field.
func (_u *EntityUpdateOne) AddPolicyID(v int) *EntityUpdateOne {
	_u.mutation.AddPolicyID(v)
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *EntityUpdateOne) SetCreatedBy(v uint64) *EntityUpdateOne {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

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

// AddCreatedBy adds value to the "created_by" field.
func (_u *EntityUpdateOne) AddCreatedBy(v int64) *EntityUpdateOne {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (_u *EntityUpdateOne) ClearCreatedBy() *EntityUpdateOne {
	_u.mutation.ClearCreatedBy()
	return _u
}

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

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

// ClearEtag clears the value of the "etag" field.
func (_u *EntityUpdateOne) ClearEtag() *EntityUpdateOne {
	_u.mutation.ClearEtag()
	return _u
}

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

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

// ClearMimeType clears the value of the "mime_type" field.
func (_u *EntityUpdateOne) ClearMimeType() *EntityUpdateOne {
	_u.mutation.ClearMimeType()
	return _u
}

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

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

// ClearDimension clears the value of the "dimension" field.
func (_u *EntityUpdateOne) ClearDimension() *EntityUpdateOne {
	_u.mutation.ClearDimension()
	return _u
}

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

// ClearStorageMetadata clears the value of the "storage_metadata" field.
func (_u *EntityUpdateOne) ClearStorageMetadata() *EntityUpdateOne {
	_u.mutation.ClearStorageMetadata()
	return _u
}

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

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

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

// ClearFileVersions clears all "file_versions" edges to the FileEntity entity.
func (_u *EntityUpdateOne) ClearFileVersions() *EntityUpdateOne {
	_u.mutation.ClearFileVersions()
	return _u
}

// RemoveFileVersionIDs removes the "file_versions" edge to FileEntity entities by IDs.
func (_u *EntityUpdateOne) RemoveFileVersionIDs(ids ...uint) *EntityUpdateOne {
	_u.mutation.RemoveFileVersionIDs(ids...)
	return _u
}

// RemoveFileVersions removes "file_versions" edges to FileEntity entities.
func (_u *EntityUpdateOne) RemoveFileVersions(v ...*FileEntity) *EntityUpdateOne {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveFileVersionIDs(ids...)
}

// Where appends a list predicates to the EntityUpdate builder.
func (_u *EntityUpdateOne) Where(ps ...predicate.Entity) *EntityUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated Entity entity.
func (_u *EntityUpdateOne) Save(ctx context.Context) (*Entity, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *EntityUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := entity.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

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

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

func (_u *EntityUpdateOne) sqlSave(ctx context.Context) (_node *Entity, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(entity.Table, entity.Columns, sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Entity.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, entity.FieldID)
		for _, f := range fields {
			if !entity.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != entity.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(entity.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(entity.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Source(); ok {
		_spec.SetField(entity.FieldSource, field.TypeString, value)
	}
	if _u.mutation.SourceCleared() {
		_spec.ClearField(entity.FieldSource, field.TypeString)
	}
	if value, ok := _u.mutation.Size(); ok {
		_spec.SetField(entity.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSize(); ok {
		_spec.AddField(entity.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.UploadSessionID(); ok {
		_spec.SetField(entity.FieldUploadSessionID, field.TypeString, value)
	}
	if _u.mutation.UploadSessionIDCleared() {
		_spec.ClearField(entity.FieldUploadSessionID, field.TypeString)
	}
	if value, ok := _u.mutation.RecycleOptions(); ok {
		_spec.SetField(entity.FieldRecycleOptions, field.TypeString, value)
	}
	if _u.mutation.RecycleOptionsCleared() {
		_spec.ClearField(entity.FieldRecycleOptions, field.TypeString)
	}
	if value, ok := _u.mutation.PolicyID(); ok {
		_spec.SetField(entity.FieldPolicyID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedPolicyID(); ok {
		_spec.AddField(entity.FieldPolicyID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(entity.FieldCreatedBy, field.TypeUint64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(entity.FieldCreatedBy, field.TypeUint64, value)
	}
	if _u.mutation.CreatedByCleared() {
		_spec.ClearField(entity.FieldCreatedBy, field.TypeUint64)
	}
	if value, ok := _u.mutation.Etag(); ok {
		_spec.SetField(entity.FieldEtag, field.TypeString, value)
	}
	if _u.mutation.EtagCleared() {
		_spec.ClearField(entity.FieldEtag, field.TypeString)
	}
	if value, ok := _u.mutation.MimeType(); ok {
		_spec.SetField(entity.FieldMimeType, field.TypeString, value)
	}
	if _u.mutation.MimeTypeCleared() {
		_spec.ClearField(entity.FieldMimeType, field.TypeString)
	}
	if value, ok := _u.mutation.Dimension(); ok {
		_spec.SetField(entity.FieldDimension, field.TypeString, value)
	}
	if _u.mutation.DimensionCleared() {
		_spec.ClearField(entity.FieldDimension, field.TypeString)
	}
	if value, ok := _u.mutation.StorageMetadata(); ok {
		_spec.SetField(entity.FieldStorageMetadata, field.TypeOther, value)
	}
	if _u.mutation.StorageMetadataCleared() {
		_spec.ClearField(entity.FieldStorageMetadata, field.TypeOther)
	}
	if _u.mutation.FileVersionsCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedFileVersionsIDs(); len(nodes) > 0 && !_u.mutation.FileVersionsCleared() {
		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.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.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.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &Entity{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{entity.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
