
    [Gj
              
          d Z ddlmZmZ ddlmZmZ ddlmZ  e       Z	e	j                  d       ee      fdedefd       Ze	j                  d	       ee      fdedefd
       ZddlZddlZd Z e       \  ZZe	j                  d       ee      fdedefd       Ze	j                  d       ee      fdededefd       Ze	j                  d       ee      fdedededefd       Zy)uK  
GET /api/v1/crops and GET /api/v1/districts

Lets the client's frontend populate dropdowns dynamically rather than
hardcoding the supported crop/district list in their own codebase —
when the project coordinator updates data/lookup/*.json and redeploys,
the frontend picks up the change automatically on next load.

Accepts either a logged-in customer (JWT, via the SaaS dashboard) or a
service API key — same dual-auth pattern as /analyze (see
app/core/dual_auth.py). These endpoints only return public reference
data (no user-specific content), so either auth mode is sufficient.
    )	APIRouterDepends)AuthContextget_auth_context)reference_dataz/cropsauthreturnc                 6   K   dt        j                         iS w)Ncrops)r   
list_cropsr   s    KC:\Crop_Prediction\Backend\crop-ai-system\app\api\v1\endpoints\reference.pyr   r      s     ^..011   z
/districtsc                 6   K   dt        j                         iS w)N	districts)r   list_districtsr   s    r   r   r      s     66899r   Nc                     t         j                  j                  t         j                  j                  t              ddddd      } 	 t        t         j                  j                  t         j                  j                  | d                  5 }t        j                  |      }d d d        t        t         j                  j                  t         j                  j                  | d                  5 }t        j                  |      }d d d        fS # 1 sw Y   qxY w# 1 sw Y   xY w# t        $ r}i i fcY d }~S d }~ww xY w)Nz..datalookupzstate_districts.jsonzbanana_varieties.json)
_ospathjoindirname__file__opennormpath_jsonload	Exception)basefstates	varietieses        r   _load_banana_datar%   #   s    88==))(3T4vxXD#((##CHHMM$8N$OPQ 	#UVZZ]F	##((##CHHMM$8O$PQR 	&VW

1I	&y  		# 	#	& 	&  2vsP   AD+ D#AD+ 1DD+ DD+ D($D+ +	E4D=7E=Ez/banana/statesc                 H   K   dt        t        j                               iS w)Nr"   )sorted_STATE_DISTRICTSkeysr   s    r   list_statesr*   0   s      f-224566s    "z/banana/districts/{state}statec                 |   K   | j                         j                         }t        j                  |g       }||dS w)N)r+   r   )lowerstripr(   get)r+   r   state_lowerr   s       r   list_districts_by_stater1   4   s9     
 ++-%%'K $$["5I y99s   :<z$/banana/varieties/{state}/{district}districtc                   K   | j                         j                         }|j                         j                         }t        j                  |i       j                  |g       }|D cg c]  }|d   dv s| }}|||dS c c}w w)Nsuitability)suitablerisky)r+   r2   r#   )r-   r.   _BANANA_VARIETIESr/   )r+   r2   r   r0   district_lowerr#   v	availables           r   list_banana_varietiesr;   =   s      KKM'')K^^%++-N!%%k26::>2NI%Sq=)9=R)RSIS"  Ts   A'B)B6B:B)__doc__fastapir   r   app.core.dual_authr   r   app.services.reference_datar   routerr/   dictr   r   jsonr   osr   r%   r(   r7   r*   strr1   r;        r   <module>rG      se   ' < 6	 H)01A)B 2; 2t 2 2 L-45E-F :{ :4 : :  	 '8&9 # #*12B*C 7K 7 7 7 '(   01::
: 
: ): 23   01  
	 4rF   