// 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/objet"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/piecejointe"
	"github.com/google/uuid"
)

// PieceJointe is the model entity for the PieceJointe schema.
type PieceJointe struct {
	config `json:"-"`
	// ID of the ent.
	// Identifiant unique de la piece jointe
	ID uuid.UUID `json:"id,omitempty"`
	// Identifiant de l'objet
	ObjetID uuid.UUID `json:"objet_id,omitempty"`
	// Nom du fichier
	NomFichier string `json:"nom_fichier,omitempty"`
	// Chemin de stockage
	Chemin string `json:"chemin,omitempty"`
	// Type MIME
	TypeMime string `json:"type_mime,omitempty"`
	// Piece jointe principale
	EstPrincipale bool `json:"est_principale,omitempty"`
	// Categorie de la piece jointe
	Categorie piecejointe.Categorie `json:"categorie,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 PieceJointeQuery when eager-loading is set.
	Edges        PieceJointeEdges `json:"edges"`
	selectValues sql.SelectValues
}

// PieceJointeEdges holds the relations/edges for other nodes in the graph.
type PieceJointeEdges 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 PieceJointeEdges) 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 (*PieceJointe) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case piecejointe.FieldEstPrincipale:
			values[i] = new(sql.NullBool)
		case piecejointe.FieldNomFichier, piecejointe.FieldChemin, piecejointe.FieldTypeMime, piecejointe.FieldCategorie:
			values[i] = new(sql.NullString)
		case piecejointe.FieldCreatedAt, piecejointe.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case piecejointe.FieldID, piecejointe.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 PieceJointe fields.
func (_m *PieceJointe) 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 piecejointe.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 piecejointe.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 piecejointe.FieldNomFichier:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nom_fichier", values[i])
			} else if value.Valid {
				_m.NomFichier = value.String
			}
		case piecejointe.FieldChemin:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field chemin", values[i])
			} else if value.Valid {
				_m.Chemin = value.String
			}
		case piecejointe.FieldTypeMime:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type_mime", values[i])
			} else if value.Valid {
				_m.TypeMime = value.String
			}
		case piecejointe.FieldEstPrincipale:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field est_principale", values[i])
			} else if value.Valid {
				_m.EstPrincipale = value.Bool
			}
		case piecejointe.FieldCategorie:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field categorie", values[i])
			} else if value.Valid {
				_m.Categorie = piecejointe.Categorie(value.String)
			}
		case piecejointe.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 piecejointe.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 PieceJointe.
// This includes values selected through modifiers, order, etc.
func (_m *PieceJointe) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

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

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

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

// String implements the fmt.Stringer.
func (_m *PieceJointe) String() string {
	var builder strings.Builder
	builder.WriteString("PieceJointe(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("objet_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.ObjetID))
	builder.WriteString(", ")
	builder.WriteString("nom_fichier=")
	builder.WriteString(_m.NomFichier)
	builder.WriteString(", ")
	builder.WriteString("chemin=")
	builder.WriteString(_m.Chemin)
	builder.WriteString(", ")
	builder.WriteString("type_mime=")
	builder.WriteString(_m.TypeMime)
	builder.WriteString(", ")
	builder.WriteString("est_principale=")
	builder.WriteString(fmt.Sprintf("%v", _m.EstPrincipale))
	builder.WriteString(", ")
	builder.WriteString("categorie=")
	builder.WriteString(fmt.Sprintf("%v", _m.Categorie))
	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()
}

// PieceJointes is a parsable slice of PieceJointe.
type PieceJointes []*PieceJointe
