
    }Gj              
          d Z ddlZddlZddlZej
                  j                  ej
                  j                  ej
                  j                  e	      ddddd            Z
ej
                  j                  ej
                  j                  ej
                  j                  e	      ddddd            Zd	 Z e       \  ZZd
edededz  fdZy)u   
app/services/market_price_service.py

Direct market price lookup from Agmarknet data.
Returns current price per kg and per quintal for a crop in a district.
No SARIMAX forecasting — uses actual recorded prices directly.
    Nz..data	processedzmarket_prices_clean.csvlookupzcommodity_name_map.jsonc                     	 t        j                  t              } | d   j                  j	                         j                  j                         | d<   | d   j                  j	                         j                  j                         | d<   t        t              5 }t        j                  |      }d d d        | fS # 1 sw Y   xY w# t        $ r }t        j                         i fcY d }~S d }~ww xY w)Ndistrict	commodity)pdread_csv_MARKET_CSVstrlowerstripopen	_MAP_PATHjsonload	Exception	DataFrame)dffname_mapes       NC:\Crop_Prediction\Backend\crop-ai-system\app\services\market_price_service.py_loadr      s    "[[%Z.,,22488>>@:[/--33599??A;)_ 	$yy|H	$8|	$ 	$  "||~r!!"s6   BC B<0C <CC 	C1C,&C1,C1cropr   returnc                 f   t         j                  ry| j                         j                         }t        j                  ||      }|j                         j                         }t         t         d   |k(     }|j                  ry||d   |k(     }|j                  r|}|j                  d   }t        t        |d         d      t        t        |d         d      t        t        |d         d      t        t        |d	         d      t        |d         d
t        |j                  dd            dS )z
    Returns price data for a crop in a district.
    Tries district-specific price first, falls back to state-wide average.
    Returns None if no price data available for this crop.
    Nr   r   r   modal_price_per_kg   min_price_per_kgmax_price_per_kgmodal_price_per_quintal	Agmarknetarrivaldate )r   r    r!   r"   district_usedsourcedate)
_dfemptyr   r   	_name_mapgetilocroundfloatr   )r   r   
crop_lowerr   
dist_lowerrows	dist_rowsrows           r   	get_pricer5   %   s    yy##%Jz:6I!'')J s;9,-Dzz T*%34I	
..
C $)s3G/H)I1#M#(s3E/F)G#K#(s3E/F)G#K#(s3L/M)NPQ#R#&s:#7#.#&sww}b'A#B     )__doc__r   ospandasr	   pathnormpathjoindirname__file__r   r   r   r)   r+   r   dictr5    r6   r   <module>rA      s     	 ggGGLL*D$Mfg GGGGLL*D$Jcd	
	" Y"C "3 "4$; "r6   