from fastapi import APIRouter, HTTPException, Query, Path, status import urllib.parse from api.services.scheme_service import ( get_all_schemes, get_schemes_by_state, get_scheme_details_by_title, search_schemes_in_cache, get_cache_loading_status ) router = APIRouter() # ------------------------- # Schemes endpoints with language # ------------------------- @router.get("/{lang}/schemes", summary="Get all schemes grouped by state") def get_all_schemes_grouped_by_state_endpoint(lang: str = Path(..., description="Language code, e.g., en, hi")): """ Returns all schemes grouped by state from the in-memory cache. """ if get_cache_loading_status(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Schemes cache is currently loading. Please try again shortly." ) schemes = get_all_schemes(lang=lang) if not schemes: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No schemes found in cache.") return schemes @router.get("/{lang}/schemes/{state}", summary="Get schemes for a specific state") def get_scheme_titles_by_state_endpoint( lang: str = Path(..., description="Language code, e.g., en, hi"), state: str = Path(..., description="State name") ): """ Returns all schemes for a specific state from the in-memory cache. """ if get_cache_loading_status(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Schemes cache is currently loading. Please try again shortly." ) schemes_for_state = get_schemes_by_state(state, lang=lang) if not schemes_for_state: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"No schemes found under '{state.capitalize()}' in cache." ) return { "state": state.capitalize(), "count": len(schemes_for_state), "schemes": schemes_for_state } @router.get("/{lang}/schemes/{state}/scheme_titles/{title}", summary="Get details for a single scheme by title") def get_scheme_details_endpoint( lang: str = Path(..., description="Language code, e.g., en, hi"), state: str = Path(..., description="State name"), title: str = Path(..., description="Scheme title") ): """ Returns details for a single scheme by title within a specific state from the in-memory cache. """ if get_cache_loading_status(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Schemes cache is currently loading. Please try again shortly." ) decoded_title = urllib.parse.unquote(title) scheme_details = get_scheme_details_by_title(state, decoded_title, lang=lang) if not scheme_details: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Scheme '{decoded_title}' not found in state '{state.capitalize()}' in cache." ) return scheme_details @router.get("/{lang}/searchscheme", summary="Search schemes by keyword across all states") def search_schemes_endpoint( lang: str = Path(..., description="Language code, e.g., en, hi"), query: str = Query(..., description="Search across all schemes") ): """ Searches schemes across all states using the in-memory cache for smooth performance. """ if get_cache_loading_status(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Schemes cache is currently loading. Please try again shortly." ) matched_schemes = search_schemes_in_cache(query, lang=lang) if not matched_schemes: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No schemes found matching '{query}'") return { "lang": lang, "query": query, "matched_count": len(matched_schemes), "results": matched_schemes }