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

// Categorie is the model entity for the Categorie schema.
type Categorie struct {
	config `json:"-"`
	// ID of the ent.
	// Identifiant unique de la categorie
	ID uuid.UUID `json:"id,omitempty"`
	// Nom de la categorie
	Nom string `json:"nom,omitempty"`
	// Identifiant du parent
	ParentID *uuid.UUID `json:"parent_id,omitempty"`
	// Slug URL-friendly
	Slug *string `json:"slug,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 CategorieQuery when eager-loading is set.
	Edges        CategorieEdges `json:"edges"`
	selectValues sql.SelectValues
}

// CategorieEdges holds the relations/edges for other nodes in the graph.
type CategorieEdges struct {
	// Lien parent/enfants pour l'arbre des categories
	Parent *Categorie `json:"parent,omitempty"`
	// Enfants holds the value of the enfants edge.
	Enfants []*Categorie `json:"enfants,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]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 CategorieEdges) ParentOrErr() (*Categorie, error) {
	if e.Parent != nil {
		return e.Parent, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: categorie.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 CategorieEdges) EnfantsOrErr() ([]*Categorie, error) {
	if e.loadedTypes[1] {
		return e.Enfants, nil
	}
	return nil, &NotLoadedError{edge: "enfants"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Categorie) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case categorie.FieldParentID:
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case categorie.FieldNom, categorie.FieldSlug, categorie.FieldIcone:
			values[i] = new(sql.NullString)
		case categorie.FieldCreatedAt, categorie.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case categorie.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 Categorie fields.
func (_m *Categorie) 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 categorie.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 categorie.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 categorie.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 categorie.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 categorie.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 categorie.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 categorie.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 Categorie.
// This includes values selected through modifiers, order, etc.
func (_m *Categorie) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryParent queries the "parent" edge of the Categorie entity.
func (_m *Categorie) QueryParent() *CategorieQuery {
	return NewCategorieClient(_m.config).QueryParent(_m)
}

// QueryEnfants queries the "enfants" edge of the Categorie entity.
func (_m *Categorie) QueryEnfants() *CategorieQuery {
	return NewCategorieClient(_m.config).QueryEnfants(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Categorie) String() string {
	var builder strings.Builder
	builder.WriteString("Categorie(")
	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.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()
}

// Categories is a parsable slice of Categorie.
type Categories []*Categorie
