from fastapi import APIRouter, HTTPException from pydantic import BaseModel from app.services.judge import judge_service router = APIRouter() class LanguageResponse(BaseModel): id: int name: str # Popular languages to show at the top POPULAR_LANGUAGE_IDS = [71, 54, 62, 63, 50, 51, 52, 60, 68, 78] # Python, C++, Java, JS, C, C#, etc. @router.get("/", response_model=list[LanguageResponse]) async def get_languages(): try: languages = await judge_service.get_languages() # Sort: popular first, then alphabetically def sort_key(lang): lang_id = lang.get("id", 0) if lang_id in POPULAR_LANGUAGE_IDS: return (0, POPULAR_LANGUAGE_IDS.index(lang_id)) return (1, lang.get("name", "")) sorted_languages = sorted(languages, key=sort_key) return [ LanguageResponse(id=lang["id"], name=lang["name"]) for lang in sorted_languages ] except Exception as e: raise HTTPException( status_code=503, detail=f"Judge service unavailable: {str(e)}" )