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

// ChampPersonnaliseUpdate is the builder for updating ChampPersonnalise entities.
type ChampPersonnaliseUpdate struct {
	config
	hooks    []Hook
	mutation *ChampPersonnaliseMutation
}

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

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

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

// SetNomChamp sets the "nom_champ" field.
func (_u *ChampPersonnaliseUpdate) SetNomChamp(v string) *ChampPersonnaliseUpdate {
	_u.mutation.SetNomChamp(v)
	return _u
}

// SetNillableNomChamp sets the "nom_champ" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdate) SetNillableNomChamp(v *string) *ChampPersonnaliseUpdate {
	if v != nil {
		_u.SetNomChamp(*v)
	}
	return _u
}

// SetTypeChamp sets the "type_champ" field.
func (_u *ChampPersonnaliseUpdate) SetTypeChamp(v champpersonnalise.TypeChamp) *ChampPersonnaliseUpdate {
	_u.mutation.SetTypeChamp(v)
	return _u
}

// SetNillableTypeChamp sets the "type_champ" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdate) SetNillableTypeChamp(v *champpersonnalise.TypeChamp) *ChampPersonnaliseUpdate {
	if v != nil {
		_u.SetTypeChamp(*v)
	}
	return _u
}

// SetValeur sets the "valeur" field.
func (_u *ChampPersonnaliseUpdate) SetValeur(v string) *ChampPersonnaliseUpdate {
	_u.mutation.SetValeur(v)
	return _u
}

// SetNillableValeur sets the "valeur" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdate) SetNillableValeur(v *string) *ChampPersonnaliseUpdate {
	if v != nil {
		_u.SetValeur(*v)
	}
	return _u
}

// ClearValeur clears the value of the "valeur" field.
func (_u *ChampPersonnaliseUpdate) ClearValeur() *ChampPersonnaliseUpdate {
	_u.mutation.ClearValeur()
	return _u
}

// SetUnite sets the "unite" field.
func (_u *ChampPersonnaliseUpdate) SetUnite(v string) *ChampPersonnaliseUpdate {
	_u.mutation.SetUnite(v)
	return _u
}

// SetNillableUnite sets the "unite" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdate) SetNillableUnite(v *string) *ChampPersonnaliseUpdate {
	if v != nil {
		_u.SetUnite(*v)
	}
	return _u
}

// ClearUnite clears the value of the "unite" field.
func (_u *ChampPersonnaliseUpdate) ClearUnite() *ChampPersonnaliseUpdate {
	_u.mutation.ClearUnite()
	return _u
}

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *ChampPersonnaliseUpdate) check() error {
	if v, ok := _u.mutation.NomChamp(); ok {
		if err := champpersonnalise.NomChampValidator(v); err != nil {
			return &ValidationError{Name: "nom_champ", err: fmt.Errorf(`ent: validator failed for field "ChampPersonnalise.nom_champ": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TypeChamp(); ok {
		if err := champpersonnalise.TypeChampValidator(v); err != nil {
			return &ValidationError{Name: "type_champ", err: fmt.Errorf(`ent: validator failed for field "ChampPersonnalise.type_champ": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "ChampPersonnalise.objet"`)
	}
	return nil
}

func (_u *ChampPersonnaliseUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(champpersonnalise.Table, champpersonnalise.Columns, sqlgraph.NewFieldSpec(champpersonnalise.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.NomChamp(); ok {
		_spec.SetField(champpersonnalise.FieldNomChamp, field.TypeString, value)
	}
	if value, ok := _u.mutation.TypeChamp(); ok {
		_spec.SetField(champpersonnalise.FieldTypeChamp, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Valeur(); ok {
		_spec.SetField(champpersonnalise.FieldValeur, field.TypeString, value)
	}
	if _u.mutation.ValeurCleared() {
		_spec.ClearField(champpersonnalise.FieldValeur, field.TypeString)
	}
	if value, ok := _u.mutation.Unite(); ok {
		_spec.SetField(champpersonnalise.FieldUnite, field.TypeString, value)
	}
	if _u.mutation.UniteCleared() {
		_spec.ClearField(champpersonnalise.FieldUnite, field.TypeString)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   champpersonnalise.ObjetTable,
			Columns: []string{champpersonnalise.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:   champpersonnalise.ObjetTable,
			Columns: []string{champpersonnalise.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{champpersonnalise.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ChampPersonnaliseUpdateOne is the builder for updating a single ChampPersonnalise entity.
type ChampPersonnaliseUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ChampPersonnaliseMutation
}

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

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

// SetNomChamp sets the "nom_champ" field.
func (_u *ChampPersonnaliseUpdateOne) SetNomChamp(v string) *ChampPersonnaliseUpdateOne {
	_u.mutation.SetNomChamp(v)
	return _u
}

// SetNillableNomChamp sets the "nom_champ" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdateOne) SetNillableNomChamp(v *string) *ChampPersonnaliseUpdateOne {
	if v != nil {
		_u.SetNomChamp(*v)
	}
	return _u
}

// SetTypeChamp sets the "type_champ" field.
func (_u *ChampPersonnaliseUpdateOne) SetTypeChamp(v champpersonnalise.TypeChamp) *ChampPersonnaliseUpdateOne {
	_u.mutation.SetTypeChamp(v)
	return _u
}

// SetNillableTypeChamp sets the "type_champ" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdateOne) SetNillableTypeChamp(v *champpersonnalise.TypeChamp) *ChampPersonnaliseUpdateOne {
	if v != nil {
		_u.SetTypeChamp(*v)
	}
	return _u
}

// SetValeur sets the "valeur" field.
func (_u *ChampPersonnaliseUpdateOne) SetValeur(v string) *ChampPersonnaliseUpdateOne {
	_u.mutation.SetValeur(v)
	return _u
}

// SetNillableValeur sets the "valeur" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdateOne) SetNillableValeur(v *string) *ChampPersonnaliseUpdateOne {
	if v != nil {
		_u.SetValeur(*v)
	}
	return _u
}

// ClearValeur clears the value of the "valeur" field.
func (_u *ChampPersonnaliseUpdateOne) ClearValeur() *ChampPersonnaliseUpdateOne {
	_u.mutation.ClearValeur()
	return _u
}

// SetUnite sets the "unite" field.
func (_u *ChampPersonnaliseUpdateOne) SetUnite(v string) *ChampPersonnaliseUpdateOne {
	_u.mutation.SetUnite(v)
	return _u
}

// SetNillableUnite sets the "unite" field if the given value is not nil.
func (_u *ChampPersonnaliseUpdateOne) SetNillableUnite(v *string) *ChampPersonnaliseUpdateOne {
	if v != nil {
		_u.SetUnite(*v)
	}
	return _u
}

// ClearUnite clears the value of the "unite" field.
func (_u *ChampPersonnaliseUpdateOne) ClearUnite() *ChampPersonnaliseUpdateOne {
	_u.mutation.ClearUnite()
	return _u
}

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

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

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *ChampPersonnaliseUpdateOne) check() error {
	if v, ok := _u.mutation.NomChamp(); ok {
		if err := champpersonnalise.NomChampValidator(v); err != nil {
			return &ValidationError{Name: "nom_champ", err: fmt.Errorf(`ent: validator failed for field "ChampPersonnalise.nom_champ": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TypeChamp(); ok {
		if err := champpersonnalise.TypeChampValidator(v); err != nil {
			return &ValidationError{Name: "type_champ", err: fmt.Errorf(`ent: validator failed for field "ChampPersonnalise.type_champ": %w`, err)}
		}
	}
	if _u.mutation.ObjetCleared() && len(_u.mutation.ObjetIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "ChampPersonnalise.objet"`)
	}
	return nil
}

func (_u *ChampPersonnaliseUpdateOne) sqlSave(ctx context.Context) (_node *ChampPersonnalise, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(champpersonnalise.Table, champpersonnalise.Columns, sqlgraph.NewFieldSpec(champpersonnalise.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ChampPersonnalise.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, champpersonnalise.FieldID)
		for _, f := range fields {
			if !champpersonnalise.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != champpersonnalise.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.NomChamp(); ok {
		_spec.SetField(champpersonnalise.FieldNomChamp, field.TypeString, value)
	}
	if value, ok := _u.mutation.TypeChamp(); ok {
		_spec.SetField(champpersonnalise.FieldTypeChamp, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Valeur(); ok {
		_spec.SetField(champpersonnalise.FieldValeur, field.TypeString, value)
	}
	if _u.mutation.ValeurCleared() {
		_spec.ClearField(champpersonnalise.FieldValeur, field.TypeString)
	}
	if value, ok := _u.mutation.Unite(); ok {
		_spec.SetField(champpersonnalise.FieldUnite, field.TypeString, value)
	}
	if _u.mutation.UniteCleared() {
		_spec.ClearField(champpersonnalise.FieldUnite, field.TypeString)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ObjetCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   champpersonnalise.ObjetTable,
			Columns: []string{champpersonnalise.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:   champpersonnalise.ObjetTable,
			Columns: []string{champpersonnalise.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 = &ChampPersonnalise{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{champpersonnalise.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
