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

// PieceJointeUpdate is the builder for updating PieceJointe entities.
type PieceJointeUpdate struct {
	config
	hooks    []Hook
	mutation *PieceJointeMutation
}

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

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

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

// SetNomFichier sets the "nom_fichier" field.
func (_u *PieceJointeUpdate) SetNomFichier(v string) *PieceJointeUpdate {
	_u.mutation.SetNomFichier(v)
	return _u
}

// SetNillableNomFichier sets the "nom_fichier" field if the given value is not nil.
func (_u *PieceJointeUpdate) SetNillableNomFichier(v *string) *PieceJointeUpdate {
	if v != nil {
		_u.SetNomFichier(*v)
	}
	return _u
}

// SetChemin sets the "chemin" field.
func (_u *PieceJointeUpdate) SetChemin(v string) *PieceJointeUpdate {
	_u.mutation.SetChemin(v)
	return _u
}

// SetNillableChemin sets the "chemin" field if the given value is not nil.
func (_u *PieceJointeUpdate) SetNillableChemin(v *string) *PieceJointeUpdate {
	if v != nil {
		_u.SetChemin(*v)
	}
	return _u
}

// SetTypeMime sets the "type_mime" field.
func (_u *PieceJointeUpdate) SetTypeMime(v string) *PieceJointeUpdate {
	_u.mutation.SetTypeMime(v)
	return _u
}

// SetNillableTypeMime sets the "type_mime" field if the given value is not nil.
func (_u *PieceJointeUpdate) SetNillableTypeMime(v *string) *PieceJointeUpdate {
	if v != nil {
		_u.SetTypeMime(*v)
	}
	return _u
}

// SetEstPrincipale sets the "est_principale" field.
func (_u *PieceJointeUpdate) SetEstPrincipale(v bool) *PieceJointeUpdate {
	_u.mutation.SetEstPrincipale(v)
	return _u
}

// SetNillableEstPrincipale sets the "est_principale" field if the given value is not nil.
func (_u *PieceJointeUpdate) SetNillableEstPrincipale(v *bool) *PieceJointeUpdate {
	if v != nil {
		_u.SetEstPrincipale(*v)
	}
	return _u
}

// SetCategorie sets the "categorie" field.
func (_u *PieceJointeUpdate) SetCategorie(v piecejointe.Categorie) *PieceJointeUpdate {
	_u.mutation.SetCategorie(v)
	return _u
}

// SetNillableCategorie sets the "categorie" field if the given value is not nil.
func (_u *PieceJointeUpdate) SetNillableCategorie(v *piecejointe.Categorie) *PieceJointeUpdate {
	if v != nil {
		_u.SetCategorie(*v)
	}
	return _u
}

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *PieceJointeUpdate) check() error {
	if v, ok := _u.mutation.NomFichier(); ok {
		if err := piecejointe.NomFichierValidator(v); err != nil {
			return &ValidationError{Name: "nom_fichier", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.nom_fichier": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Chemin(); ok {
		if err := piecejointe.CheminValidator(v); err != nil {
			return &ValidationError{Name: "chemin", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.chemin": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TypeMime(); ok {
		if err := piecejointe.TypeMimeValidator(v); err != nil {
			return &ValidationError{Name: "type_mime", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.type_mime": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Categorie(); ok {
		if err := piecejointe.CategorieValidator(v); err != nil {
			return &ValidationError{Name: "categorie", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.categorie": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "PieceJointe.objet"`)
	}
	return nil
}

func (_u *PieceJointeUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(piecejointe.Table, piecejointe.Columns, sqlgraph.NewFieldSpec(piecejointe.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.NomFichier(); ok {
		_spec.SetField(piecejointe.FieldNomFichier, field.TypeString, value)
	}
	if value, ok := _u.mutation.Chemin(); ok {
		_spec.SetField(piecejointe.FieldChemin, field.TypeString, value)
	}
	if value, ok := _u.mutation.TypeMime(); ok {
		_spec.SetField(piecejointe.FieldTypeMime, field.TypeString, value)
	}
	if value, ok := _u.mutation.EstPrincipale(); ok {
		_spec.SetField(piecejointe.FieldEstPrincipale, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Categorie(); ok {
		_spec.SetField(piecejointe.FieldCategorie, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(piecejointe.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(piecejointe.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   piecejointe.ObjetTable,
			Columns: []string{piecejointe.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:   piecejointe.ObjetTable,
			Columns: []string{piecejointe.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 _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{piecejointe.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// PieceJointeUpdateOne is the builder for updating a single PieceJointe entity.
type PieceJointeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PieceJointeMutation
}

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

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

// SetNomFichier sets the "nom_fichier" field.
func (_u *PieceJointeUpdateOne) SetNomFichier(v string) *PieceJointeUpdateOne {
	_u.mutation.SetNomFichier(v)
	return _u
}

// SetNillableNomFichier sets the "nom_fichier" field if the given value is not nil.
func (_u *PieceJointeUpdateOne) SetNillableNomFichier(v *string) *PieceJointeUpdateOne {
	if v != nil {
		_u.SetNomFichier(*v)
	}
	return _u
}

// SetChemin sets the "chemin" field.
func (_u *PieceJointeUpdateOne) SetChemin(v string) *PieceJointeUpdateOne {
	_u.mutation.SetChemin(v)
	return _u
}

// SetNillableChemin sets the "chemin" field if the given value is not nil.
func (_u *PieceJointeUpdateOne) SetNillableChemin(v *string) *PieceJointeUpdateOne {
	if v != nil {
		_u.SetChemin(*v)
	}
	return _u
}

// SetTypeMime sets the "type_mime" field.
func (_u *PieceJointeUpdateOne) SetTypeMime(v string) *PieceJointeUpdateOne {
	_u.mutation.SetTypeMime(v)
	return _u
}

// SetNillableTypeMime sets the "type_mime" field if the given value is not nil.
func (_u *PieceJointeUpdateOne) SetNillableTypeMime(v *string) *PieceJointeUpdateOne {
	if v != nil {
		_u.SetTypeMime(*v)
	}
	return _u
}

// SetEstPrincipale sets the "est_principale" field.
func (_u *PieceJointeUpdateOne) SetEstPrincipale(v bool) *PieceJointeUpdateOne {
	_u.mutation.SetEstPrincipale(v)
	return _u
}

// SetNillableEstPrincipale sets the "est_principale" field if the given value is not nil.
func (_u *PieceJointeUpdateOne) SetNillableEstPrincipale(v *bool) *PieceJointeUpdateOne {
	if v != nil {
		_u.SetEstPrincipale(*v)
	}
	return _u
}

// SetCategorie sets the "categorie" field.
func (_u *PieceJointeUpdateOne) SetCategorie(v piecejointe.Categorie) *PieceJointeUpdateOne {
	_u.mutation.SetCategorie(v)
	return _u
}

// SetNillableCategorie sets the "categorie" field if the given value is not nil.
func (_u *PieceJointeUpdateOne) SetNillableCategorie(v *piecejointe.Categorie) *PieceJointeUpdateOne {
	if v != nil {
		_u.SetCategorie(*v)
	}
	return _u
}

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

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

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *PieceJointeUpdateOne) check() error {
	if v, ok := _u.mutation.NomFichier(); ok {
		if err := piecejointe.NomFichierValidator(v); err != nil {
			return &ValidationError{Name: "nom_fichier", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.nom_fichier": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Chemin(); ok {
		if err := piecejointe.CheminValidator(v); err != nil {
			return &ValidationError{Name: "chemin", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.chemin": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TypeMime(); ok {
		if err := piecejointe.TypeMimeValidator(v); err != nil {
			return &ValidationError{Name: "type_mime", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.type_mime": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Categorie(); ok {
		if err := piecejointe.CategorieValidator(v); err != nil {
			return &ValidationError{Name: "categorie", err: fmt.Errorf(`ent: validator failed for field "PieceJointe.categorie": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "PieceJointe.objet"`)
	}
	return nil
}

func (_u *PieceJointeUpdateOne) sqlSave(ctx context.Context) (_node *PieceJointe, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(piecejointe.Table, piecejointe.Columns, sqlgraph.NewFieldSpec(piecejointe.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PieceJointe.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, piecejointe.FieldID)
		for _, f := range fields {
			if !piecejointe.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != piecejointe.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.NomFichier(); ok {
		_spec.SetField(piecejointe.FieldNomFichier, field.TypeString, value)
	}
	if value, ok := _u.mutation.Chemin(); ok {
		_spec.SetField(piecejointe.FieldChemin, field.TypeString, value)
	}
	if value, ok := _u.mutation.TypeMime(); ok {
		_spec.SetField(piecejointe.FieldTypeMime, field.TypeString, value)
	}
	if value, ok := _u.mutation.EstPrincipale(); ok {
		_spec.SetField(piecejointe.FieldEstPrincipale, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Categorie(); ok {
		_spec.SetField(piecejointe.FieldCategorie, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(piecejointe.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(piecejointe.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   piecejointe.ObjetTable,
			Columns: []string{piecejointe.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:   piecejointe.ObjetTable,
			Columns: []string{piecejointe.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)
	}
	_node = &PieceJointe{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{piecejointe.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
