
    q(i'                      Z    d dl mZmZ d dlZd dlmZ  e       Z G d d      ZdadefdZy)    )DictListN)
get_configc                      e Zd ZdZdZdZi ddddddd	d
dddddd	ddddddd	ddddddd	ddddddd	dd d!d"d#d$d	d%d&dd'd(d)d	d*d+d,dd,d-d	d.d/d0dd1d2d	d3d4ddd5d0d	d6d7ddd8d9d	d:d;d<d=ddd	d>d?d@d,dAdd	dBdCd5d-dDdd	dEdFddGd'dd	dHdIdJdKddd	dLdd-ddMd9d	dNd=dOdPdQd	dRddQddSd	dTdd'dUdd	dVdddWdd	dXdYdOd8dZd	dKddd(d0d	d7d[d"d"dd	d\Zd] Zdkd^ed_e	d`e
fdaZdldbee   dced`e
fddZdmdee
dfed`e
fdgZdhe
d`efdiZyj)nNutritionServicez:Nutrition analysis service using USDA FoodData Central APIz)https://fdc.nal.usda.gov/api/foods/searchDEMO_KEYchicken      g@r   caloriesproteinfatcarbsfibertomato   g?g?g333333@g333333?pasta      g?   g?rice   g@g333333?   g?z	olive oilit  d   garlic   g@g      ?!   g @onion(   g?	   g333333?mushroom   gffffff
@   broccoli"   gffffff@   g333333@carrot)   
   lettuce         egg         milk@   g	@g333333@flouril  L   butteri  Q   parmesani  &      zbell pepper   =   g333333?gffffff@g?P   g@i  C      %      g@g?g      ?)z	soy saucegingerzmaple syrupzbaking powderbasillemonbrothc                 &    | j                   | _        y)zInitialize nutrition serviceN)NUTRITION_DATABASEdb)selfs    L/Users/apple/Desktop/TU/AI/AI-Recipe-Generator/services/nutrition_service.py__init__zNutritionService.__init__*   s    ))    
ingredientquantityreturnc                    |j                         j                         }|| j                  v r@| j                  |   j                         }|dz  }|D ]  }t	        ||   |z  d      ||<    |S | j                  j                         D ]L  }||v s||v s| j                  |   j                         }|dz  }|D ]  }t	        ||   |z  d      ||<    |c S  dddddddS )a#  
        Get nutrition info for an ingredient
        
        Args:
            ingredient (str): Ingredient name
            quantity (float): Quantity in grams (default 100g)
        
        Returns:
            dict: Nutrition information (calories, protein, fat, carbs, fiber)
        r   r.   r   estimate)r   r   r   r   r   source)lowerstriprI   copyroundkeys)rJ   rN   rO   ingredient_lower	nutritionscalekeyks           rK   get_nutrition_infoz#NutritionService.get_nutrition_info.   s    &++-335tww& 01668IsNE  B!&y~'=q!A	#B ww||~ %**.>#.E $ 1 1 3I$sNE& F',Yq\E-A1'E	!F$$% $ rM   ingredientsservingsc           	      d   dddddd}g }|D ]  }|j                         }d}|}t        |      D ]]  \  }	}
|
j                  ddd      j                         s(t	        |
      }|	dz   t        |      k  rdj                  ||	dz   d       n|} n | j                  ||      }|j                  ||d	       |D ]  }||xx   |j                  |d      z  cc<   !  i }|D ]  }t        ||   |z  d
      ||<    |j                         D ci c]  \  }}|t        |d
       c}}|||dS c c}}w )a:  
        Analyze nutrition for a complete recipe
        
        Args:
            ingredients (list): List of ingredients (can include quantities, e.g., "100g chicken")
            servings (int): Number of servings
        
        Returns:
            dict: Total and per-serving nutrition information
        r   r   r   . r$    N)rN   rZ   r.   )total_nutritionper_servingr`   ingredients_nutrition)split	enumeratereplaceisdigitfloatlenjoinr^   appendgetrW   items)rJ   r_   r`   re   ingredient_detailsrN   partsrO   ingredient_nameipartrZ   r\   rf   r]   vs                   rK   analyze_recipezNutritionService.analyze_recipeV   s|    
  % 	>J$$&EH(O %U+ 4<<R+335$T{H?@sSZ?OchhuQqST{&;UdO	 //JI%%(&'  ' >$	c1(==$>)	>0 " 	IC$_S%9H%DaHK	I <K;P;P;RS41a5A;S& %7	
 	
Ss   D,rZ   serving_typec                     |j                  ||      }dddddd}i }|j                         D ]/  \  }}||v r!||   dkD  rt        |||   z  dz  d	      ||<   +d||<   1 |S )
a  
        Calculate daily value percentages (based on 2000 calorie diet)
        
        Args:
            nutrition (dict): Nutrition information
            serving_type (str): "total" or "per_serving"
        
        Returns:
            dict: Daily value percentages
        i  2   N   i,  r   r   r   r   r$   )rp   rq   rW   )rJ   rZ   ry   values	dv_valuespercentagesr\   values           rK   get_daily_value_percentagesz,NutritionService.get_daily_value_percentages   s     |Y7 
	  ,,. 	%JCiIcNQ$6#(%)C.*@C)G#KC #$C 		% rM   nutrition_analysisc                     |d   }|d   }d| d}|d|d    dz  }|d|d	    d
z  }|d|d    d
z  }|d|d    d
z  }|d|d    d
z  }|S )z
        Generate a human-readable nutrition summary
        
        Args:
            nutrition_analysis (dict): Output from analyze_recipe()
        
        Returns:
            str: Formatted nutrition summary
        rf   r`   zNutrition per serving (z servings):
u   • Calories: r   z kcal
u   • Protein: r   zg
u	   • Fat: r   u   • Carbohydrates: r   u   • Fiber: r    )rJ   r   rf   r`   summarys        rK   get_nutrition_summaryz&NutritionService.get_nutrition_summary   s     )7%j1+H:]C^K
$;#<GDD];y#9":#>>Y{512#66(W)=(>cBB[W!5 6c::rM   N)r   )r$   )total)__name__
__module____qualname____doc__USDA_API_URLAPI_KEYrH   rL   strrl   r   r^   r   intrx   r   r   r   rM   rK   r   r      s   D ?LG3UVWrc#VYZ 	cabSVW 	SSrTWX	
 	#!C!VWX 	ss3VYZ 	bSqSVW 	C#XYZ 	C!VYZ 	rc#UXY 	s3UVW 	CBrCRST 	RCcTUV 	cbRRUV 	ss2VWX  	BWXY!" 	B1S1WXY#$ #%C#X[\!c#UXY$'AcBYZ[&)aBYZ[ SqSVW SqSVW SsUVW16*&S &E &D &P6
$s) 6
s 6
4 6
pT  [_ <  rM   r   rP   c                  .    t         
t               a t         S )z(Get or create nutrition service instance)nutrition_servicer   r   rM   rK   get_nutrition_servicer      s      ,.rM   )	typingr   r   requestsconfigr   r   r   r   r   rM   rK   <module>r      s7      	y yz  / rM   