package ent

import (
	"context"
	"fmt"

	"entgo.io/contrib/entproto/internal/todo/ent/multiwordschema"
	"entgo.io/contrib/entproto/internal/todo/ent/predicate"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

type MultiWordSchemaUpdate struct {
	config
	hooks    []Hook
	mutation *MultiWordSchemaMutation
}

func (mwsu *MultiWordSchemaUpdate) Where(ps ...predicate.MultiWordSchema) *MultiWordSchemaUpdate {
	mwsu.mutation.Where(ps...)
	return mwsu
}

func (mwsu *MultiWordSchemaUpdate) SetUnit(m multiwordschema.Unit) *MultiWordSchemaUpdate {
	mwsu.mutation.SetUnit(m)
	return mwsu
}

func (mwsu *MultiWordSchemaUpdate) SetNillableUnit(m *multiwordschema.Unit) *MultiWordSchemaUpdate {
	if m != nil {
		mwsu.SetUnit(*m)
	}
	return mwsu
}

func (mwsu *MultiWordSchemaUpdate) Mutation() *MultiWordSchemaMutation {
	return mwsu.mutation
}

func (mwsu *MultiWordSchemaUpdate) Save(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	if len(mwsu.hooks) == 0 {
		if err = mwsu.check(); err != nil {
			return 0, err
		}
		affected, err = mwsu.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*MultiWordSchemaMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = mwsu.check(); err != nil {
				return 0, err
			}
			mwsu.mutation = mutation
			affected, err = mwsu.sqlSave(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(mwsu.hooks) - 1; i >= 0; i-- {
			if mwsu.hooks[i] == nil {
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = mwsu.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, mwsu.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

func (mwsu *MultiWordSchemaUpdate) SaveX(ctx context.Context) int {
	affected, err := mwsu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

func (mwsu *MultiWordSchemaUpdate) Exec(ctx context.Context) error {
	_, err := mwsu.Save(ctx)
	return err
}

func (mwsu *MultiWordSchemaUpdate) ExecX(ctx context.Context) {
	if err := mwsu.Exec(ctx); err != nil {
		panic(err)
	}
}

func (mwsu *MultiWordSchemaUpdate) check() error {
	if v, ok := mwsu.mutation.Unit(); ok {
		if err := multiwordschema.UnitValidator(v); err != nil {
			return &ValidationError{Name: "unit", err: fmt.Errorf("ent: validator failed for field \"unit\": %w", err)}
		}
	}
	return nil
}

func (mwsu *MultiWordSchemaUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   multiwordschema.Table,
			Columns: multiwordschema.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: multiwordschema.FieldID,
			},
		},
	}
	if ps := mwsu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mwsu.mutation.Unit(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeEnum,
			Value:  value,
			Column: multiwordschema.FieldUnit,
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, mwsu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{multiwordschema.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return 0, err
	}
	return n, nil
}

type MultiWordSchemaUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MultiWordSchemaMutation
}

func (mwsuo *MultiWordSchemaUpdateOne) SetUnit(m multiwordschema.Unit) *MultiWordSchemaUpdateOne {
	mwsuo.mutation.SetUnit(m)
	return mwsuo
}

func (mwsuo *MultiWordSchemaUpdateOne) SetNillableUnit(m *multiwordschema.Unit) *MultiWordSchemaUpdateOne {
	if m != nil {
		mwsuo.SetUnit(*m)
	}
	return mwsuo
}

func (mwsuo *MultiWordSchemaUpdateOne) Mutation() *MultiWordSchemaMutation {
	return mwsuo.mutation
}

func (mwsuo *MultiWordSchemaUpdateOne) Select(field string, fields ...string) *MultiWordSchemaUpdateOne {
	mwsuo.fields = append([]string{field}, fields...)
	return mwsuo
}

func (mwsuo *MultiWordSchemaUpdateOne) Save(ctx context.Context) (*MultiWordSchema, error) {
	var (
		err  error
		node *MultiWordSchema
	)
	if len(mwsuo.hooks) == 0 {
		if err = mwsuo.check(); err != nil {
			return nil, err
		}
		node, err = mwsuo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*MultiWordSchemaMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = mwsuo.check(); err != nil {
				return nil, err
			}
			mwsuo.mutation = mutation
			node, err = mwsuo.sqlSave(ctx)
			mutation.done = true
			return node, err
		})
		for i := len(mwsuo.hooks) - 1; i >= 0; i-- {
			if mwsuo.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = mwsuo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, mwsuo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

func (mwsuo *MultiWordSchemaUpdateOne) SaveX(ctx context.Context) *MultiWordSchema {
	node, err := mwsuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

func (mwsuo *MultiWordSchemaUpdateOne) Exec(ctx context.Context) error {
	_, err := mwsuo.Save(ctx)
	return err
}

func (mwsuo *MultiWordSchemaUpdateOne) ExecX(ctx context.Context) {
	if err := mwsuo.Exec(ctx); err != nil {
		panic(err)
	}
}

func (mwsuo *MultiWordSchemaUpdateOne) check() error {
	if v, ok := mwsuo.mutation.Unit(); ok {
		if err := multiwordschema.UnitValidator(v); err != nil {
			return &ValidationError{Name: "unit", err: fmt.Errorf("ent: validator failed for field \"unit\": %w", err)}
		}
	}
	return nil
}

func (mwsuo *MultiWordSchemaUpdateOne) sqlSave(ctx context.Context) (_node *MultiWordSchema, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   multiwordschema.Table,
			Columns: multiwordschema.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: multiwordschema.FieldID,
			},
		},
	}
	id, ok := mwsuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing MultiWordSchema.ID for update")}
	}
	_spec.Node.ID.Value = id
	if fields := mwsuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, multiwordschema.FieldID)
		for _, f := range fields {
			if !multiwordschema.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != multiwordschema.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := mwsuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mwsuo.mutation.Unit(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeEnum,
			Value:  value,
			Column: multiwordschema.FieldUnit,
		})
	}
	_node = &MultiWordSchema{config: mwsuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, mwsuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{multiwordschema.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	return _node, nil
}
// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

	"entgo.io/contrib/entproto/internal/todo/ent/multiwordschema"
	"entgo.io/contrib/entproto/internal/todo/ent/predicate"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// MultiWordSchemaUpdate is the builder for updating MultiWordSchema entities.
type MultiWordSchemaUpdate struct {
	config
	hooks    []Hook
	mutation *MultiWordSchemaMutation
}

// Where appends a list predicates to the MultiWordSchemaUpdate builder.
func (mwsu *MultiWordSchemaUpdate) Where(ps ...predicate.MultiWordSchema) *MultiWordSchemaUpdate {
	mwsu.mutation.Where(ps...)
	return mwsu
}

// SetUnit sets the "unit" field.
func (mwsu *MultiWordSchemaUpdate) SetUnit(m multiwordschema.Unit) *MultiWordSchemaUpdate {
	mwsu.mutation.SetUnit(m)
	return mwsu
}

// SetNillableUnit sets the "unit" field if the given value is not nil.
func (mwsu *MultiWordSchemaUpdate) SetNillableUnit(m *multiwordschema.Unit) *MultiWordSchemaUpdate {
	if m != nil {
		mwsu.SetUnit(*m)
	}
	return mwsu
}

// Mutation returns the MultiWordSchemaMutation object of the builder.
func (mwsu *MultiWordSchemaUpdate) Mutation() *MultiWordSchemaMutation {
	return mwsu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (mwsu *MultiWordSchemaUpdate) Save(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	if len(mwsu.hooks) == 0 {
		if err = mwsu.check(); err != nil {
			return 0, err
		}
		affected, err = mwsu.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*MultiWordSchemaMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = mwsu.check(); err != nil {
				return 0, err
			}
			mwsu.mutation = mutation
			affected, err = mwsu.sqlSave(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(mwsu.hooks) - 1; i >= 0; i-- {
			if mwsu.hooks[i] == nil {
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = mwsu.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, mwsu.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (mwsu *MultiWordSchemaUpdate) check() error {
	if v, ok := mwsu.mutation.Unit(); ok {
		if err := multiwordschema.UnitValidator(v); err != nil {
			return &ValidationError{Name: "unit", err: fmt.Errorf("ent: validator failed for field \"unit\": %w", err)}
		}
	}
	return nil
}

func (mwsu *MultiWordSchemaUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   multiwordschema.Table,
			Columns: multiwordschema.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: multiwordschema.FieldID,
			},
		},
	}
	if ps := mwsu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mwsu.mutation.Unit(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeEnum,
			Value:  value,
			Column: multiwordschema.FieldUnit,
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, mwsu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{multiwordschema.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return 0, err
	}
	return n, nil
}

// MultiWordSchemaUpdateOne is the builder for updating a single MultiWordSchema entity.
type MultiWordSchemaUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MultiWordSchemaMutation
}

// SetUnit sets the "unit" field.
func (mwsuo *MultiWordSchemaUpdateOne) SetUnit(m multiwordschema.Unit) *MultiWordSchemaUpdateOne {
	mwsuo.mutation.SetUnit(m)
	return mwsuo
}

// SetNillableUnit sets the "unit" field if the given value is not nil.
func (mwsuo *MultiWordSchemaUpdateOne) SetNillableUnit(m *multiwordschema.Unit) *MultiWordSchemaUpdateOne {
	if m != nil {
		mwsuo.SetUnit(*m)
	}
	return mwsuo
}

// Mutation returns the MultiWordSchemaMutation object of the builder.
func (mwsuo *MultiWordSchemaUpdateOne) Mutation() *MultiWordSchemaMutation {
	return mwsuo.mutation
}

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

// Save executes the query and returns the updated MultiWordSchema entity.
func (mwsuo *MultiWordSchemaUpdateOne) Save(ctx context.Context) (*MultiWordSchema, error) {
	var (
		err  error
		node *MultiWordSchema
	)
	if len(mwsuo.hooks) == 0 {
		if err = mwsuo.check(); err != nil {
			return nil, err
		}
		node, err = mwsuo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*MultiWordSchemaMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = mwsuo.check(); err != nil {
				return nil, err
			}
			mwsuo.mutation = mutation
			node, err = mwsuo.sqlSave(ctx)
			mutation.done = true
			return node, err
		})
		for i := len(mwsuo.hooks) - 1; i >= 0; i-- {
			if mwsuo.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = mwsuo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, mwsuo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (mwsuo *MultiWordSchemaUpdateOne) check() error {
	if v, ok := mwsuo.mutation.Unit(); ok {
		if err := multiwordschema.UnitValidator(v); err != nil {
			return &ValidationError{Name: "unit", err: fmt.Errorf("ent: validator failed for field \"unit\": %w", err)}
		}
	}
	return nil
}

func (mwsuo *MultiWordSchemaUpdateOne) sqlSave(ctx context.Context) (_node *MultiWordSchema, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   multiwordschema.Table,
			Columns: multiwordschema.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: multiwordschema.FieldID,
			},
		},
	}
	id, ok := mwsuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing MultiWordSchema.ID for update")}
	}
	_spec.Node.ID.Value = id
	if fields := mwsuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, multiwordschema.FieldID)
		for _, f := range fields {
			if !multiwordschema.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != multiwordschema.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := mwsuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mwsuo.mutation.Unit(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeEnum,
			Value:  value,
			Column: multiwordschema.FieldUnit,
		})
	}
	_node = &MultiWordSchema{config: mwsuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, mwsuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{multiwordschema.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	return _node, nil
}
