// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

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

// ChampPersonnalise is the model entity for the ChampPersonnalise schema.
type ChampPersonnalise struct {
	config `json:"-"`
	// ID of the ent.
	// Identifiant unique du champ
	ID uuid.UUID `json:"id,omitempty"`
	// Identifiant de l'objet
	ObjetID uuid.UUID `json:"objet_id,omitempty"`
	// Nom du champ
	NomChamp string `json:"nom_champ,omitempty"`
	// Type du champ
	TypeChamp champpersonnalise.TypeChamp `json:"type_champ,omitempty"`
	// Valeur stockee en texte
	Valeur *string `json:"valeur,omitempty"`
	// Unite (si applicable)
	Unite *string `json:"unite,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 ChampPersonnaliseQuery when eager-loading is set.
	Edges        ChampPersonnaliseEdges `json:"edges"`
	selectValues sql.SelectValues
}

// ChampPersonnaliseEdges holds the relations/edges for other nodes in the graph.
type ChampPersonnaliseEdges struct {
	// Objet holds the value of the objet edge.
	Objet *Objet `json:"objet,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// ObjetOrErr returns the Objet value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ChampPersonnaliseEdges) ObjetOrErr() (*Objet, error) {
	if e.Objet != nil {
		return e.Objet, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: objet.Label}
	}
	return nil, &NotLoadedError{edge: "objet"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ChampPersonnalise) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case champpersonnalise.FieldNomChamp, champpersonnalise.FieldTypeChamp, champpersonnalise.FieldValeur, champpersonnalise.FieldUnite:
			values[i] = new(sql.NullString)
		case champpersonnalise.FieldCreatedAt, champpersonnalise.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case champpersonnalise.FieldID, champpersonnalise.FieldObjetID:
			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 ChampPersonnalise fields.
func (_m *ChampPersonnalise) 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 champpersonnalise.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 champpersonnalise.FieldObjetID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field objet_id", values[i])
			} else if value != nil {
				_m.ObjetID = *value
			}
		case champpersonnalise.FieldNomChamp:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nom_champ", values[i])
			} else if value.Valid {
				_m.NomChamp = value.String
			}
		case champpersonnalise.FieldTypeChamp:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type_champ", values[i])
			} else if value.Valid {
				_m.TypeChamp = champpersonnalise.TypeChamp(value.String)
			}
		case champpersonnalise.FieldValeur:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field valeur", values[i])
			} else if value.Valid {
				_m.Valeur = new(string)
				*_m.Valeur = value.String
			}
		case champpersonnalise.FieldUnite:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field unite", values[i])
			} else if value.Valid {
				_m.Unite = new(string)
				*_m.Unite = value.String
			}
		case champpersonnalise.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 champpersonnalise.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 ChampPersonnalise.
// This includes values selected through modifiers, order, etc.
func (_m *ChampPersonnalise) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryObjet queries the "objet" edge of the ChampPersonnalise entity.
func (_m *ChampPersonnalise) QueryObjet() *ObjetQuery {
	return NewChampPersonnaliseClient(_m.config).QueryObjet(_m)
}

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

// Unwrap unwraps the ChampPersonnalise 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 *ChampPersonnalise) Unwrap() *ChampPersonnalise {
	_tx, ok := _m.config.driver.(*txDriver)
	if !ok {
		panic("ent: ChampPersonnalise is not a transactional entity")
	}
	_m.config.driver = _tx.drv
	return _m
}

// String implements the fmt.Stringer.
func (_m *ChampPersonnalise) String() string {
	var builder strings.Builder
	builder.WriteString("ChampPersonnalise(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("objet_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.ObjetID))
	builder.WriteString(", ")
	builder.WriteString("nom_champ=")
	builder.WriteString(_m.NomChamp)
	builder.WriteString(", ")
	builder.WriteString("type_champ=")
	builder.WriteString(fmt.Sprintf("%v", _m.TypeChamp))
	builder.WriteString(", ")
	if v := _m.Valeur; v != nil {
		builder.WriteString("valeur=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Unite; v != nil {
		builder.WriteString("unite=")
		builder.WriteString(*v)
	}
	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()
}

// ChampPersonnalises is a parsable slice of ChampPersonnalise.
type ChampPersonnalises []*ChampPersonnalise
