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

// Emplacement is the model entity for the Emplacement schema.
type Emplacement struct {
	config `json:"-"`
	// ID of the ent.
	// Identifiant unique de l'emplacement
	ID uuid.UUID `json:"id,omitempty"`
	// Nom de l'emplacement
	Nom string `json:"nom,omitempty"`
	// Identifiant du parent
	ParentID *uuid.UUID `json:"parent_id,omitempty"`
	// Slug URL-friendly
	Slug *string `json:"slug,omitempty"`
	// Piece associee
	Piece *string `json:"piece,omitempty"`
	// Meuble associe
	Meuble *string `json:"meuble,omitempty"`
	// Numero de boite
	NumeroBoite *string `json:"numero_boite,omitempty"`
	// Nom ou code d'icone
	Icone *string `json:"icone,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 EmplacementQuery when eager-loading is set.
	Edges        EmplacementEdges `json:"edges"`
	selectValues sql.SelectValues
}

// EmplacementEdges holds the relations/edges for other nodes in the graph.
type EmplacementEdges struct {
	// Lien parent/enfants pour l'arbre des emplacements
	Parent *Emplacement `json:"parent,omitempty"`
	// Enfants holds the value of the enfants edge.
	Enfants []*Emplacement `json:"enfants,omitempty"`
	// Liens entre emplacements et objets
	LiensObjets []*LienObjetEmplacement `json:"liens_objets,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

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

// EnfantsOrErr returns the Enfants value or an error if the edge
// was not loaded in eager-loading.
func (e EmplacementEdges) EnfantsOrErr() ([]*Emplacement, error) {
	if e.loadedTypes[1] {
		return e.Enfants, nil
	}
	return nil, &NotLoadedError{edge: "enfants"}
}

// LiensObjetsOrErr returns the LiensObjets value or an error if the edge
// was not loaded in eager-loading.
func (e EmplacementEdges) LiensObjetsOrErr() ([]*LienObjetEmplacement, error) {
	if e.loadedTypes[2] {
		return e.LiensObjets, nil
	}
	return nil, &NotLoadedError{edge: "liens_objets"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Emplacement) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case emplacement.FieldParentID:
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case emplacement.FieldNom, emplacement.FieldSlug, emplacement.FieldPiece, emplacement.FieldMeuble, emplacement.FieldNumeroBoite, emplacement.FieldIcone:
			values[i] = new(sql.NullString)
		case emplacement.FieldCreatedAt, emplacement.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case emplacement.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 Emplacement fields.
func (_m *Emplacement) 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 emplacement.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 emplacement.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 emplacement.FieldParentID:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field parent_id", values[i])
			} else if value.Valid {
				_m.ParentID = new(uuid.UUID)
				*_m.ParentID = *value.S.(*uuid.UUID)
			}
		case emplacement.FieldSlug:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field slug", values[i])
			} else if value.Valid {
				_m.Slug = new(string)
				*_m.Slug = value.String
			}
		case emplacement.FieldPiece:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field piece", values[i])
			} else if value.Valid {
				_m.Piece = new(string)
				*_m.Piece = value.String
			}
		case emplacement.FieldMeuble:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field meuble", values[i])
			} else if value.Valid {
				_m.Meuble = new(string)
				*_m.Meuble = value.String
			}
		case emplacement.FieldNumeroBoite:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field numero_boite", values[i])
			} else if value.Valid {
				_m.NumeroBoite = new(string)
				*_m.NumeroBoite = value.String
			}
		case emplacement.FieldIcone:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field icone", values[i])
			} else if value.Valid {
				_m.Icone = new(string)
				*_m.Icone = value.String
			}
		case emplacement.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 emplacement.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 Emplacement.
// This includes values selected through modifiers, order, etc.
func (_m *Emplacement) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryParent queries the "parent" edge of the Emplacement entity.
func (_m *Emplacement) QueryParent() *EmplacementQuery {
	return NewEmplacementClient(_m.config).QueryParent(_m)
}

// QueryEnfants queries the "enfants" edge of the Emplacement entity.
func (_m *Emplacement) QueryEnfants() *EmplacementQuery {
	return NewEmplacementClient(_m.config).QueryEnfants(_m)
}

// QueryLiensObjets queries the "liens_objets" edge of the Emplacement entity.
func (_m *Emplacement) QueryLiensObjets() *LienObjetEmplacementQuery {
	return NewEmplacementClient(_m.config).QueryLiensObjets(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Emplacement) String() string {
	var builder strings.Builder
	builder.WriteString("Emplacement(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("nom=")
	builder.WriteString(_m.Nom)
	builder.WriteString(", ")
	if v := _m.ParentID; v != nil {
		builder.WriteString("parent_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.Slug; v != nil {
		builder.WriteString("slug=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Piece; v != nil {
		builder.WriteString("piece=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Meuble; v != nil {
		builder.WriteString("meuble=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.NumeroBoite; v != nil {
		builder.WriteString("numero_boite=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Icone; v != nil {
		builder.WriteString("icone=")
		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()
}

// Emplacements is a parsable slice of Emplacement.
type Emplacements []*Emplacement
