Ë
    KµCjµ  ã                   ó†   — d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
  e
e«      Z ed«      Z G d„ d	«      Z e«       Zy)
u^  
Reference data service.

Loads data/lookup/crops.json and data/lookup/districts.json ONCE at
application startup and keeps them in memory. This is deliberately not
a database table for v1 â€” the list of supported crops/districts changes
rarely, and a JSON file the project coordinator (or client) can edit
without touching code is the right amount of flexibility for now.

If the client later wants self-service crop/district management through
an admin UI, this becomes a database table with the exact same interface
(get_crop, get_district, list_crops, list_districts) â€” calling code
doesn't change.
é    N)ÚPath)ÚOptional)ÚUnsupportedCropErrorÚUnsupportedDistrictError)Ú
get_loggerzdata/lookupc                   óˆ   — e Zd Zdd„Zdd„Zdd„Zdedefd„Zdedefd	„Z	de
e   fd
„Zde
e   fd„Zdedefd„Zdedefd„Zy)ÚReferenceDataServiceÚreturnNc                 ó.   — i | _         i | _        d| _        y )NF)Ú_cropsÚ
_districtsÚ_loaded©Úselfs    úHC:\Crop_Prediction\Backend\crop-ai-system\app\services\reference_data.pyÚ__init__zReferenceDataService.__init__   s   € Ø')ˆŒØ+-ˆŒØˆ�ó    c                 ó‚  — t         dz  }t         dz  }|j                  «       st        d|› d�«      ‚|j                  «       st        d|› d�«      ‚t        |d¬«      5 }t	        j
                  |«      | _        d d d «       t        |d¬«      5 }t	        j
                  |«      | _        d d d «       | j                  st        d«      ‚| j                  st        d	«      ‚d
| _	        t        j                  dt        | j                  «      › dt        | j                  «      › d�«       y # 1 sw Y   Œ±xY w# 1 sw Y   Œ�xY w)Nz
crops.jsonzdistricts.jsonzRequired lookup file missing: z3. The system cannot validate crop input without it.z7. The system cannot validate location input without it.z	utf-8-sig)Úencodingz'crops.json loaded but contains no cropsz/districts.json loaded but contains no districtsTzReference data loaded: z crops, z
 districts)Ú
LOOKUP_DIRÚexistsÚFileNotFoundErrorÚopenÚjsonÚloadr   r   Ú
ValueErrorr   ÚloggerÚinfoÚlen)r   Ú
crops_pathÚdistricts_pathÚfs       r   r   zReferenceDataService.load"   s9  € Ü ,Ñ.ˆ
Ü#Ð&6Ñ6ˆà× Ñ Ô"Ü#Ø0°°ð =Dð Eóð ð ×$Ñ$Ô&Ü#Ø0°Ð0@ð AHð Ióð ô �* {Ô3ð 	'°qÜŸ)™) A›,ˆDŒK÷	'ä�.¨;Ô7ð 	+¸1Ü"Ÿi™i¨›lˆDŒO÷	+ð �{Š{ÜÐFÓGÐGØ�ŠÜÐNÓOÐOàˆŒÜ�‰Ø%¤c¨$¯+©+Ó&6Ð%7°xÜ�4—?‘?Ó#Ð$ Jð0õ	
÷	'ð 	'ú÷	+ð 	+ús   ÁD)ÂD5Ä)D2Ä5D>c                 ó2   — | j                   st        d«      ‚y )NzWReferenceDataService used before load() was called. This indicates a startup-order bug.)r   ÚRuntimeErrorr   s    r   Ú_ensure_loadedz#ReferenceDataService._ensure_loadedE   s    € Ø�|Š|Üð6óð ð r   Úcropc                 ó  — | j                  «        |j                  «       j                  «       }|| j                  vrBt	        d|› ddj                  t        | j                  j                  «       «      «      › �¬«      ‚| j                  |   S )Nú'z,' is not a supported crop. Supported crops: ú, ©Úmessage)r%   ÚstripÚlowerr   r   ÚjoinÚsortedÚkeys©r   r&   s     r   Úget_cropzReferenceDataService.get_cropL   s   € Ø×ÑÔØ�z‰z‹|×!Ñ!Ó#ˆØ�t—{‘{Ñ"Ü&à˜�vð (Ø(,¯	©	´&¸¿¹×9IÑ9IÓ9KÓ2LÓ(MÐ'NðPôð ð �{‰{˜4Ñ Ð r   Údistrictc                 ó  — | j                  «        |j                  «       j                  «       }|| j                  vrBt	        d|› ddj                  t        | j                  j                  «       «      «      › �¬«      ‚| j                  |   S )Nr(   z4' is not a supported district. Supported districts: r)   r*   )r%   r,   r-   r   r   r.   r/   r0   ©r   r3   s     r   Úget_districtz!ReferenceDataService.get_districtX   s€   € Ø×ÑÔØ—>‘>Ó#×)Ñ)Ó+ˆØ˜4Ÿ?™?Ñ*Ü*à˜�zð ",Ø,0¯I©I´f¸T¿_¹_×=QÑ=QÓ=SÓ6TÓ,UÐ+VðXôð ð �‰˜xÑ(Ð(r   c                 óh   — | j                  «        t        | j                  j                  «       «      S ©N)r%   r/   r   r0   r   s    r   Ú
list_cropszReferenceDataService.list_cropsd   s&   € Ø×ÑÔÜ�d—k‘k×&Ñ&Ó(Ó)Ð)r   c                 óh   — | j                  «        t        | j                  j                  «       «      S r8   )r%   r/   r   r0   r   s    r   Úlist_districtsz#ReferenceDataService.list_districtsh   s&   € Ø×ÑÔÜ�d—o‘o×*Ñ*Ó,Ó-Ð-r   c                 óv   — | j                  «        |j                  «       j                  «       | j                  v S r8   )r%   r,   r-   r   r1   s     r   Úis_valid_cropz"ReferenceDataService.is_valid_cropl   s,   € Ø×ÑÔØ�z‰z‹|×!Ñ!Ó# t§{¡{Ð2Ð2r   c                 óv   — | j                  «        |j                  «       j                  «       | j                  v S r8   )r%   r,   r-   r   r5   s     r   Úis_valid_districtz&ReferenceDataService.is_valid_districtp   s-   € Ø×ÑÔØ�~‰~Ó×%Ñ%Ó'¨4¯?©?Ð:Ð:r   )r
   N)Ú__name__Ú
__module__Ú__qualname__r   r   r%   ÚstrÚdictr2   r6   Úlistr9   r;   Úboolr=   r?   © r   r   r	   r	      s~   „ óó
!
óFð
!˜Sð 
! Tó 
!ð
) Sð 
)¨Tó 
)ð*˜D ™Ió *ð.  S¡	ó .ð3 #ð 3¨$ó 3ð;¨#ð ;°$ô ;r   r	   )Ú__doc__r   Úpathlibr   Útypingr   Úapp.core.exceptionsr   r   Úapp.core.logging_configr   r@   r   r   r	   Úreference_datarG   r   r   ú<module>rN      sF   ðñó Ý Ý ç NÝ .á	�HÓ	€á�-Ó €
÷V;ñ V;ñt &Ó'�r   