// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/objet"
	"github.com/google/uuid"
)

// Objet is the model entity for the Objet schema.
type Objet struct {
	config `json:"-"`
	// ID of the ent.
	// Identifiant unique de l'objet
	ID uuid.UUID `json:"id,omitempty"`
	// Nom de l'objet
	Nom string `json:"nom,omitempty"`
	// Description libre
	Description *string `json:"description,omitempty"`
	// Quantite en stock
	Quantite int `json:"quantite,omitempty"`
	// Prix d'achat
	PrixAchat *float64 `json:"prix_achat,omitempty"`
	// Date d'achat
	DateAchat *time.Time `json:"date_achat,omitempty"`
	// Boutique ou fournisseur
	Boutique *string `json:"boutique,omitempty"`
	// Numero de serie
	NumeroSerie *string `json:"numero_serie,omitempty"`
	// Numero de modele
	NumeroModele *string `json:"numero_modele,omitempty"`
	// Fabricant
	Fabricant *string `json:"fabricant,omitempty"`
	// Statut de l'objet
	Statut objet.Statut `json:"statut,omitempty"`
	// Caracteristiques personnalisees
	Caracteristiques map[string]interface{} `json:"caracteristiques,omitempty"`
	// Date de creation
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Date de derniere mise a jour
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ObjetQuery when eager-loading is set.
	Edges        ObjetEdges `json:"edges"`
	selectValues sql.SelectValues
}

// ObjetEdges holds the relations/edges for other nodes in the graph.
type ObjetEdges struct {
	// LiensEmplacements holds the value of the liens_emplacements edge.
	LiensEmplacements []*LienObjetEmplacement `json:"liens_emplacements,omitempty"`
	// ChampsPersonnalises holds the value of the champs_personnalises edge.
	ChampsPersonnalises []*ChampPersonnalise `json:"champs_personnalises,omitempty"`
	// PiecesJointes holds the value of the pieces_jointes edge.
	PiecesJointes []*PieceJointe `json:"pieces_jointes,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// LiensEmplacementsOrErr returns the LiensEmplacements value or an error if the edge
// was not loaded in eager-loading.
func (e ObjetEdges) LiensEmplacementsOrErr() ([]*LienObjetEmplacement, error) {
	if e.loadedTypes[0] {
		return e.LiensEmplacements, nil
	}
	return nil, &NotLoadedError{edge: "liens_emplacements"}
}

// ChampsPersonnalisesOrErr returns the ChampsPersonnalises value or an error if the edge
// was not loaded in eager-loading.
func (e ObjetEdges) ChampsPersonnalisesOrErr() ([]*ChampPersonnalise, error) {
	if e.loadedTypes[1] {
		return e.ChampsPersonnalises, nil
	}
	return nil, &NotLoadedError{edge: "champs_personnalises"}
}

// PiecesJointesOrErr returns the PiecesJointes value or an error if the edge
// was not loaded in eager-loading.
func (e ObjetEdges) PiecesJointesOrErr() ([]*PieceJointe, error) {
	if e.loadedTypes[2] {
		return e.PiecesJointes, nil
	}
	return nil, &NotLoadedError{edge: "pieces_jointes"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Objet) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case objet.FieldCaracteristiques:
			values[i] = new([]byte)
		case objet.FieldPrixAchat:
			values[i] = new(sql.NullFloat64)
		case objet.FieldQuantite:
			values[i] = new(sql.NullInt64)
		case objet.FieldNom, objet.FieldDescription, objet.FieldBoutique, objet.FieldNumeroSerie, objet.FieldNumeroModele, objet.FieldFabricant, objet.FieldStatut:
			values[i] = new(sql.NullString)
		case objet.FieldDateAchat, objet.FieldCreatedAt, objet.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case objet.FieldID:
			values[i] = new(uuid.UUID)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Objet fields.
func (_m *Objet) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case objet.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				_m.ID = *value
			}
		case objet.FieldNom:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nom", values[i])
			} else if value.Valid {
				_m.Nom = value.String
			}
		case objet.FieldDescription:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field description", values[i])
			} else if value.Valid {
				_m.Description = new(string)
				*_m.Description = value.String
			}
		case objet.FieldQuantite:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field quantite", values[i])
			} else if value.Valid {
				_m.Quantite = int(value.Int64)
			}
		case objet.FieldPrixAchat:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field prix_achat", values[i])
			} else if value.Valid {
				_m.PrixAchat = new(float64)
				*_m.PrixAchat = value.Float64
			}
		case objet.FieldDateAchat:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field date_achat", values[i])
			} else if value.Valid {
				_m.DateAchat = new(time.Time)
				*_m.DateAchat = value.Time
			}
		case objet.FieldBoutique:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field boutique", values[i])
			} else if value.Valid {
				_m.Boutique = new(string)
				*_m.Boutique = value.String
			}
		case objet.FieldNumeroSerie:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field numero_serie", values[i])
			} else if value.Valid {
				_m.NumeroSerie = new(string)
				*_m.NumeroSerie = value.String
			}
		case objet.FieldNumeroModele:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field numero_modele", values[i])
			} else if value.Valid {
				_m.NumeroModele = new(string)
				*_m.NumeroModele = value.String
			}
		case objet.FieldFabricant:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field fabricant", values[i])
			} else if value.Valid {
				_m.Fabricant = new(string)
				*_m.Fabricant = value.String
			}
		case objet.FieldStatut:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field statut", values[i])
			} else if value.Valid {
				_m.Statut = objet.Statut(value.String)
			}
		case objet.FieldCaracteristiques:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field caracteristiques", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Caracteristiques); err != nil {
					return fmt.Errorf("unmarshal field caracteristiques: %w", err)
				}
			}
		case objet.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				_m.CreatedAt = value.Time
			}
		case objet.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				_m.UpdatedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Objet.
// This includes values selected through modifiers, order, etc.
func (_m *Objet) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryLiensEmplacements queries the "liens_emplacements" edge of the Objet entity.
func (_m *Objet) QueryLiensEmplacements() *LienObjetEmplacementQuery {
	return NewObjetClient(_m.config).QueryLiensEmplacements(_m)
}

// QueryChampsPersonnalises queries the "champs_personnalises" edge of the Objet entity.
func (_m *Objet) QueryChampsPersonnalises() *ChampPersonnaliseQuery {
	return NewObjetClient(_m.config).QueryChampsPersonnalises(_m)
}

// QueryPiecesJointes queries the "pieces_jointes" edge of the Objet entity.
func (_m *Objet) QueryPiecesJointes() *PieceJointeQuery {
	return NewObjetClient(_m.config).QueryPiecesJointes(_m)
}

// Update returns a builder for updating this Objet.
// Note that you need to call Objet.Unwrap() before calling this method if this Objet
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *Objet) Update() *ObjetUpdateOne {
	return NewObjetClient(_m.config).UpdateOne(_m)
}

// Unwrap unwraps the Objet entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *Objet) Unwrap() *Objet {
	_tx, ok := _m.config.driver.(*txDriver)
	if !ok {
		panic("ent: Objet is not a transactional entity")
	}
	_m.config.driver = _tx.drv
	return _m
}

// String implements the fmt.Stringer.
func (_m *Objet) String() string {
	var builder strings.Builder
	builder.WriteString("Objet(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("nom=")
	builder.WriteString(_m.Nom)
	builder.WriteString(", ")
	if v := _m.Description; v != nil {
		builder.WriteString("description=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("quantite=")
	builder.WriteString(fmt.Sprintf("%v", _m.Quantite))
	builder.WriteString(", ")
	if v := _m.PrixAchat; v != nil {
		builder.WriteString("prix_achat=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.DateAchat; v != nil {
		builder.WriteString("date_achat=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Boutique; v != nil {
		builder.WriteString("boutique=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.NumeroSerie; v != nil {
		builder.WriteString("numero_serie=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.NumeroModele; v != nil {
		builder.WriteString("numero_modele=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Fabricant; v != nil {
		builder.WriteString("fabricant=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("statut=")
	builder.WriteString(fmt.Sprintf("%v", _m.Statut))
	builder.WriteString(", ")
	builder.WriteString("caracteristiques=")
	builder.WriteString(fmt.Sprintf("%v", _m.Caracteristiques))
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Objets is a parsable slice of Objet.
type Objets []*Objet
