// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"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"
	"github.com/google/uuid"
)

// ChampPersonnaliseCreate is the builder for creating a ChampPersonnalise entity.
type ChampPersonnaliseCreate struct {
	config
	mutation *ChampPersonnaliseMutation
	hooks    []Hook
}

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

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

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

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

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

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

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

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

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

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

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *ChampPersonnaliseCreate) SetNillableUpdatedAt(v *time.Time) *ChampPersonnaliseCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

// SetID sets the "id" field.
func (_c *ChampPersonnaliseCreate) SetID(v uuid.UUID) *ChampPersonnaliseCreate {
	_c.mutation.SetID(v)
	return _c
}

// SetNillableID sets the "id" field if the given value is not nil.
func (_c *ChampPersonnaliseCreate) SetNillableID(v *uuid.UUID) *ChampPersonnaliseCreate {
	if v != nil {
		_c.SetID(*v)
	}
	return _c
}

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

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

// Save creates the ChampPersonnalise in the database.
func (_c *ChampPersonnaliseCreate) Save(ctx context.Context) (*ChampPersonnalise, error) {
	_c.defaults()
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (_c *ChampPersonnaliseCreate) SaveX(ctx context.Context) *ChampPersonnalise {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (_c *ChampPersonnaliseCreate) defaults() {
	if _, ok := _c.mutation.TypeChamp(); !ok {
		v := champpersonnalise.DefaultTypeChamp
		_c.mutation.SetTypeChamp(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := champpersonnalise.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := champpersonnalise.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.ID(); !ok {
		v := champpersonnalise.DefaultID()
		_c.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *ChampPersonnaliseCreate) check() error {
	if _, ok := _c.mutation.ObjetID(); !ok {
		return &ValidationError{Name: "objet_id", err: errors.New(`ent: missing required field "ChampPersonnalise.objet_id"`)}
	}
	if _, ok := _c.mutation.NomChamp(); !ok {
		return &ValidationError{Name: "nom_champ", err: errors.New(`ent: missing required field "ChampPersonnalise.nom_champ"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.TypeChamp(); !ok {
		return &ValidationError{Name: "type_champ", err: errors.New(`ent: missing required field "ChampPersonnalise.type_champ"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ChampPersonnalise.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ChampPersonnalise.updated_at"`)}
	}
	if len(_c.mutation.ObjetIDs()) == 0 {
		return &ValidationError{Name: "objet", err: errors.New(`ent: missing required edge "ChampPersonnalise.objet"`)}
	}
	return nil
}

func (_c *ChampPersonnaliseCreate) sqlSave(ctx context.Context) (*ChampPersonnalise, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *ChampPersonnaliseCreate) createSpec() (*ChampPersonnalise, *sqlgraph.CreateSpec) {
	var (
		_node = &ChampPersonnalise{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(champpersonnalise.Table, sqlgraph.NewFieldSpec(champpersonnalise.FieldID, field.TypeUUID))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := _c.mutation.NomChamp(); ok {
		_spec.SetField(champpersonnalise.FieldNomChamp, field.TypeString, value)
		_node.NomChamp = value
	}
	if value, ok := _c.mutation.TypeChamp(); ok {
		_spec.SetField(champpersonnalise.FieldTypeChamp, field.TypeEnum, value)
		_node.TypeChamp = value
	}
	if value, ok := _c.mutation.Valeur(); ok {
		_spec.SetField(champpersonnalise.FieldValeur, field.TypeString, value)
		_node.Valeur = &value
	}
	if value, ok := _c.mutation.Unite(); ok {
		_spec.SetField(champpersonnalise.FieldUnite, field.TypeString, value)
		_node.Unite = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(champpersonnalise.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if nodes := _c.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)
		}
		_node.ObjetID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// ChampPersonnaliseCreateBulk is the builder for creating many ChampPersonnalise entities in bulk.
type ChampPersonnaliseCreateBulk struct {
	config
	err      error
	builders []*ChampPersonnaliseCreate
}

// Save creates the ChampPersonnalise entities in the database.
func (_c *ChampPersonnaliseCreateBulk) Save(ctx context.Context) ([]*ChampPersonnalise, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*ChampPersonnalise, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*ChampPersonnaliseMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (_c *ChampPersonnaliseCreateBulk) SaveX(ctx context.Context) []*ChampPersonnalise {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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