// 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/cloudreve/Cloudreve/v4/ent/file"
	"github.com/cloudreve/Cloudreve/v4/ent/metadata"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
)

// MetadataUpdate is the builder for updating Metadata entities.
type MetadataUpdate struct {
	config
	hooks    []Hook
	mutation *MetadataMutation
}

// Where appends a list predicates to the MetadataUpdate builder.
func (mu *MetadataUpdate) Where(ps ...predicate.Metadata) *MetadataUpdate {
	mu.mutation.Where(ps...)
	return mu
}

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

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

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

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

// SetName sets the "name" field.
func (mu *MetadataUpdate) SetName(s string) *MetadataUpdate {
	mu.mutation.SetName(s)
	return mu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (mu *MetadataUpdate) SetNillableName(s *string) *MetadataUpdate {
	if s != nil {
		mu.SetName(*s)
	}
	return mu
}

// SetValue sets the "value" field.
func (mu *MetadataUpdate) SetValue(s string) *MetadataUpdate {
	mu.mutation.SetValue(s)
	return mu
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (mu *MetadataUpdate) SetNillableValue(s *string) *MetadataUpdate {
	if s != nil {
		mu.SetValue(*s)
	}
	return mu
}

// SetFileID sets the "file_id" field.
func (mu *MetadataUpdate) SetFileID(i int) *MetadataUpdate {
	mu.mutation.SetFileID(i)
	return mu
}

// SetNillableFileID sets the "file_id" field if the given value is not nil.
func (mu *MetadataUpdate) SetNillableFileID(i *int) *MetadataUpdate {
	if i != nil {
		mu.SetFileID(*i)
	}
	return mu
}

// SetIsPublic sets the "is_public" field.
func (mu *MetadataUpdate) SetIsPublic(b bool) *MetadataUpdate {
	mu.mutation.SetIsPublic(b)
	return mu
}

// SetNillableIsPublic sets the "is_public" field if the given value is not nil.
func (mu *MetadataUpdate) SetNillableIsPublic(b *bool) *MetadataUpdate {
	if b != nil {
		mu.SetIsPublic(*b)
	}
	return mu
}

// SetFile sets the "file" edge to the File entity.
func (mu *MetadataUpdate) SetFile(f *File) *MetadataUpdate {
	return mu.SetFileID(f.ID)
}

// Mutation returns the MetadataMutation object of the builder.
func (mu *MetadataUpdate) Mutation() *MetadataMutation {
	return mu.mutation
}

// ClearFile clears the "file" edge to the File entity.
func (mu *MetadataUpdate) ClearFile() *MetadataUpdate {
	mu.mutation.ClearFile()
	return mu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (mu *MetadataUpdate) Save(ctx context.Context) (int, error) {
	if err := mu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, mu.sqlSave, mu.mutation, mu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (mu *MetadataUpdate) defaults() error {
	if _, ok := mu.mutation.UpdatedAt(); !ok {
		if metadata.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized metadata.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := metadata.UpdateDefaultUpdatedAt()
		mu.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (mu *MetadataUpdate) check() error {
	if _, ok := mu.mutation.FileID(); mu.mutation.FileCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Metadata.file"`)
	}
	return nil
}

func (mu *MetadataUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := mu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(metadata.Table, metadata.Columns, sqlgraph.NewFieldSpec(metadata.FieldID, field.TypeInt))
	if ps := mu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mu.mutation.UpdatedAt(); ok {
		_spec.SetField(metadata.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := mu.mutation.DeletedAt(); ok {
		_spec.SetField(metadata.FieldDeletedAt, field.TypeTime, value)
	}
	if mu.mutation.DeletedAtCleared() {
		_spec.ClearField(metadata.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := mu.mutation.Name(); ok {
		_spec.SetField(metadata.FieldName, field.TypeString, value)
	}
	if value, ok := mu.mutation.Value(); ok {
		_spec.SetField(metadata.FieldValue, field.TypeString, value)
	}
	if value, ok := mu.mutation.IsPublic(); ok {
		_spec.SetField(metadata.FieldIsPublic, field.TypeBool, value)
	}
	if mu.mutation.FileCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   metadata.FileTable,
			Columns: []string{metadata.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mu.mutation.FileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   metadata.FileTable,
			Columns: []string{metadata.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{metadata.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	mu.mutation.done = true
	return n, nil
}

// MetadataUpdateOne is the builder for updating a single Metadata entity.
type MetadataUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MetadataMutation
}

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

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

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

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

// SetName sets the "name" field.
func (muo *MetadataUpdateOne) SetName(s string) *MetadataUpdateOne {
	muo.mutation.SetName(s)
	return muo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (muo *MetadataUpdateOne) SetNillableName(s *string) *MetadataUpdateOne {
	if s != nil {
		muo.SetName(*s)
	}
	return muo
}

// SetValue sets the "value" field.
func (muo *MetadataUpdateOne) SetValue(s string) *MetadataUpdateOne {
	muo.mutation.SetValue(s)
	return muo
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (muo *MetadataUpdateOne) SetNillableValue(s *string) *MetadataUpdateOne {
	if s != nil {
		muo.SetValue(*s)
	}
	return muo
}

// SetFileID sets the "file_id" field.
func (muo *MetadataUpdateOne) SetFileID(i int) *MetadataUpdateOne {
	muo.mutation.SetFileID(i)
	return muo
}

// SetNillableFileID sets the "file_id" field if the given value is not nil.
func (muo *MetadataUpdateOne) SetNillableFileID(i *int) *MetadataUpdateOne {
	if i != nil {
		muo.SetFileID(*i)
	}
	return muo
}

// SetIsPublic sets the "is_public" field.
func (muo *MetadataUpdateOne) SetIsPublic(b bool) *MetadataUpdateOne {
	muo.mutation.SetIsPublic(b)
	return muo
}

// SetNillableIsPublic sets the "is_public" field if the given value is not nil.
func (muo *MetadataUpdateOne) SetNillableIsPublic(b *bool) *MetadataUpdateOne {
	if b != nil {
		muo.SetIsPublic(*b)
	}
	return muo
}

// SetFile sets the "file" edge to the File entity.
func (muo *MetadataUpdateOne) SetFile(f *File) *MetadataUpdateOne {
	return muo.SetFileID(f.ID)
}

// Mutation returns the MetadataMutation object of the builder.
func (muo *MetadataUpdateOne) Mutation() *MetadataMutation {
	return muo.mutation
}

// ClearFile clears the "file" edge to the File entity.
func (muo *MetadataUpdateOne) ClearFile() *MetadataUpdateOne {
	muo.mutation.ClearFile()
	return muo
}

// Where appends a list predicates to the MetadataUpdate builder.
func (muo *MetadataUpdateOne) Where(ps ...predicate.Metadata) *MetadataUpdateOne {
	muo.mutation.Where(ps...)
	return muo
}

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

// Save executes the query and returns the updated Metadata entity.
func (muo *MetadataUpdateOne) Save(ctx context.Context) (*Metadata, error) {
	if err := muo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, muo.sqlSave, muo.mutation, muo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (muo *MetadataUpdateOne) defaults() error {
	if _, ok := muo.mutation.UpdatedAt(); !ok {
		if metadata.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized metadata.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := metadata.UpdateDefaultUpdatedAt()
		muo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (muo *MetadataUpdateOne) check() error {
	if _, ok := muo.mutation.FileID(); muo.mutation.FileCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Metadata.file"`)
	}
	return nil
}

func (muo *MetadataUpdateOne) sqlSave(ctx context.Context) (_node *Metadata, err error) {
	if err := muo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(metadata.Table, metadata.Columns, sqlgraph.NewFieldSpec(metadata.FieldID, field.TypeInt))
	id, ok := muo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Metadata.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := muo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, metadata.FieldID)
		for _, f := range fields {
			if !metadata.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != metadata.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := muo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := muo.mutation.UpdatedAt(); ok {
		_spec.SetField(metadata.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := muo.mutation.DeletedAt(); ok {
		_spec.SetField(metadata.FieldDeletedAt, field.TypeTime, value)
	}
	if muo.mutation.DeletedAtCleared() {
		_spec.ClearField(metadata.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := muo.mutation.Name(); ok {
		_spec.SetField(metadata.FieldName, field.TypeString, value)
	}
	if value, ok := muo.mutation.Value(); ok {
		_spec.SetField(metadata.FieldValue, field.TypeString, value)
	}
	if value, ok := muo.mutation.IsPublic(); ok {
		_spec.SetField(metadata.FieldIsPublic, field.TypeBool, value)
	}
	if muo.mutation.FileCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   metadata.FileTable,
			Columns: []string{metadata.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := muo.mutation.FileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   metadata.FileTable,
			Columns: []string{metadata.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Metadata{config: muo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{metadata.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	muo.mutation.done = true
	return _node, nil
}
