// 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"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/emplacement"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/lienobjetemplacement"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/objet"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/predicate"
	"github.com/google/uuid"
)

// LienObjetEmplacementUpdate is the builder for updating LienObjetEmplacement entities.
type LienObjetEmplacementUpdate struct {
	config
	hooks    []Hook
	mutation *LienObjetEmplacementMutation
}

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

// SetObjetID sets the "objet_id" field.
func (_u *LienObjetEmplacementUpdate) SetObjetID(v uuid.UUID) *LienObjetEmplacementUpdate {
	_u.mutation.SetObjetID(v)
	return _u
}

// SetNillableObjetID sets the "objet_id" field if the given value is not nil.
func (_u *LienObjetEmplacementUpdate) SetNillableObjetID(v *uuid.UUID) *LienObjetEmplacementUpdate {
	if v != nil {
		_u.SetObjetID(*v)
	}
	return _u
}

// SetEmplacementID sets the "emplacement_id" field.
func (_u *LienObjetEmplacementUpdate) SetEmplacementID(v uuid.UUID) *LienObjetEmplacementUpdate {
	_u.mutation.SetEmplacementID(v)
	return _u
}

// SetNillableEmplacementID sets the "emplacement_id" field if the given value is not nil.
func (_u *LienObjetEmplacementUpdate) SetNillableEmplacementID(v *uuid.UUID) *LienObjetEmplacementUpdate {
	if v != nil {
		_u.SetEmplacementID(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *LienObjetEmplacementUpdate) SetType(v lienobjetemplacement.Type) *LienObjetEmplacementUpdate {
	_u.mutation.SetType(v)
	return _u
}

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

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

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

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

// SetObjet sets the "objet" edge to the Objet entity.
func (_u *LienObjetEmplacementUpdate) SetObjet(v *Objet) *LienObjetEmplacementUpdate {
	return _u.SetObjetID(v.ID)
}

// SetEmplacement sets the "emplacement" edge to the Emplacement entity.
func (_u *LienObjetEmplacementUpdate) SetEmplacement(v *Emplacement) *LienObjetEmplacementUpdate {
	return _u.SetEmplacementID(v.ID)
}

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

// ClearObjet clears the "objet" edge to the Objet entity.
func (_u *LienObjetEmplacementUpdate) ClearObjet() *LienObjetEmplacementUpdate {
	_u.mutation.ClearObjet()
	return _u
}

// ClearEmplacement clears the "emplacement" edge to the Emplacement entity.
func (_u *LienObjetEmplacementUpdate) ClearEmplacement() *LienObjetEmplacementUpdate {
	_u.mutation.ClearEmplacement()
	return _u
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *LienObjetEmplacementUpdate) 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 *LienObjetEmplacementUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LienObjetEmplacementUpdate) 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 *LienObjetEmplacementUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := lienobjetemplacement.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *LienObjetEmplacementUpdate) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := lienobjetemplacement.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "LienObjetEmplacement.type": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "LienObjetEmplacement.objet"`)
	}
	if _u.mutation.EmplacementCleared() && len(_u.mutation.EmplacementIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "LienObjetEmplacement.emplacement"`)
	}
	return nil
}

func (_u *LienObjetEmplacementUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(lienobjetemplacement.Table, lienobjetemplacement.Columns, sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID))
	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.GetType(); ok {
		_spec.SetField(lienobjetemplacement.FieldType, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(lienobjetemplacement.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(lienobjetemplacement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.ObjetTable,
			Columns: []string{lienobjetemplacement.ObjetColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(objet.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ObjetIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.ObjetTable,
			Columns: []string{lienobjetemplacement.ObjetColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(objet.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EmplacementCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.EmplacementTable,
			Columns: []string{lienobjetemplacement.EmplacementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EmplacementIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.EmplacementTable,
			Columns: []string{lienobjetemplacement.EmplacementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{lienobjetemplacement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// LienObjetEmplacementUpdateOne is the builder for updating a single LienObjetEmplacement entity.
type LienObjetEmplacementUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LienObjetEmplacementMutation
}

// SetObjetID sets the "objet_id" field.
func (_u *LienObjetEmplacementUpdateOne) SetObjetID(v uuid.UUID) *LienObjetEmplacementUpdateOne {
	_u.mutation.SetObjetID(v)
	return _u
}

// SetNillableObjetID sets the "objet_id" field if the given value is not nil.
func (_u *LienObjetEmplacementUpdateOne) SetNillableObjetID(v *uuid.UUID) *LienObjetEmplacementUpdateOne {
	if v != nil {
		_u.SetObjetID(*v)
	}
	return _u
}

// SetEmplacementID sets the "emplacement_id" field.
func (_u *LienObjetEmplacementUpdateOne) SetEmplacementID(v uuid.UUID) *LienObjetEmplacementUpdateOne {
	_u.mutation.SetEmplacementID(v)
	return _u
}

// SetNillableEmplacementID sets the "emplacement_id" field if the given value is not nil.
func (_u *LienObjetEmplacementUpdateOne) SetNillableEmplacementID(v *uuid.UUID) *LienObjetEmplacementUpdateOne {
	if v != nil {
		_u.SetEmplacementID(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *LienObjetEmplacementUpdateOne) SetType(v lienobjetemplacement.Type) *LienObjetEmplacementUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

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

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

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

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

// SetObjet sets the "objet" edge to the Objet entity.
func (_u *LienObjetEmplacementUpdateOne) SetObjet(v *Objet) *LienObjetEmplacementUpdateOne {
	return _u.SetObjetID(v.ID)
}

// SetEmplacement sets the "emplacement" edge to the Emplacement entity.
func (_u *LienObjetEmplacementUpdateOne) SetEmplacement(v *Emplacement) *LienObjetEmplacementUpdateOne {
	return _u.SetEmplacementID(v.ID)
}

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

// ClearObjet clears the "objet" edge to the Objet entity.
func (_u *LienObjetEmplacementUpdateOne) ClearObjet() *LienObjetEmplacementUpdateOne {
	_u.mutation.ClearObjet()
	return _u
}

// ClearEmplacement clears the "emplacement" edge to the Emplacement entity.
func (_u *LienObjetEmplacementUpdateOne) ClearEmplacement() *LienObjetEmplacementUpdateOne {
	_u.mutation.ClearEmplacement()
	return _u
}

// Where appends a list predicates to the LienObjetEmplacementUpdate builder.
func (_u *LienObjetEmplacementUpdateOne) Where(ps ...predicate.LienObjetEmplacement) *LienObjetEmplacementUpdateOne {
	_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 *LienObjetEmplacementUpdateOne) Select(field string, fields ...string) *LienObjetEmplacementUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LienObjetEmplacementUpdateOne) 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 *LienObjetEmplacementUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := lienobjetemplacement.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *LienObjetEmplacementUpdateOne) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := lienobjetemplacement.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "LienObjetEmplacement.type": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "LienObjetEmplacement.objet"`)
	}
	if _u.mutation.EmplacementCleared() && len(_u.mutation.EmplacementIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "LienObjetEmplacement.emplacement"`)
	}
	return nil
}

func (_u *LienObjetEmplacementUpdateOne) sqlSave(ctx context.Context) (_node *LienObjetEmplacement, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(lienobjetemplacement.Table, lienobjetemplacement.Columns, sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LienObjetEmplacement.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, lienobjetemplacement.FieldID)
		for _, f := range fields {
			if !lienobjetemplacement.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != lienobjetemplacement.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.GetType(); ok {
		_spec.SetField(lienobjetemplacement.FieldType, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(lienobjetemplacement.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(lienobjetemplacement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.ObjetTable,
			Columns: []string{lienobjetemplacement.ObjetColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(objet.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ObjetIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.ObjetTable,
			Columns: []string{lienobjetemplacement.ObjetColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(objet.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EmplacementCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.EmplacementTable,
			Columns: []string{lienobjetemplacement.EmplacementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EmplacementIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   lienobjetemplacement.EmplacementTable,
			Columns: []string{lienobjetemplacement.EmplacementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &LienObjetEmplacement{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{lienobjetemplacement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
