

class Ingredient:
    def __init__(self, data: dict):
        for key, value in data.items():
            # Nettoyage du nom d'attribut (remplace les caractères problématiques)
            clean_key = key.replace("-", "_").replace(")", "").replace("(", "")
            
            # Conversion des valeurs numériques
            if isinstance(value, str):
                value = value.replace(",", ".")
                try:
                    value = float(value)
                except ValueError:
                    if value == "-":
                        value = None
            
            setattr(self, clean_key, value)
        self.description = "Pour 100g"
    
    # def __init__(self, nom: str):
    #     from app.services.data_ingredients import get_ingredient
    #     data = get_ingredient(nom)

    #     for key, value in data.items():
    #         # Nettoyage du nom d'attribut (remplace les caractères problématiques)
    #         clean_key = key.replace("-", "_").replace(")", "").replace("(", "")
            
    #         # Conversion des valeurs numériques
    #         if isinstance(value, str):
    #             value = value.replace(",", ".")
    #             try:
    #                 value = float(value)
    #             except ValueError:
    #                 if value == "-":
    #                     value = None
            
    #         setattr(self, clean_key, value)
    #     self.description = "Pour 100g"

    def __str__(self):
        return f"Ingredient(Nom={self.alim_nom_fr},description={self.description})"
    
    def info(self):
        info_str = f"Information sur l'ingredient: {self.alim_nom_fr}\n"
        for attr, value in self.__dict__.items():
            unite = attr.split("__")[-1] if "__" in attr else ""
            nom = attr.split("__")[0] if "__" in attr else attr
            info_str += f"{nom}: {value} {unite}\n"
        print(info_str)
        return info_str
    
    def get_all_values(self):
        return self.__dict__.copy()

    def get_name(self):
        return self.alim_nom_fr.split('__')[0] if '__' in self.alim_nom_fr else self.alim_nom_fr
    
    def get_value(self, attribute_name):
        return getattr(self, attribute_name, None)
    
    def set_description(self, description):
        self.description = description

    def get_description(self):
        return self.description