from fastapi import APIRouter, Depends, HTTPException from backend.core.auth import get_current_user from backend.models.practice import PracticeRequest, PracticeResponse, PracticeProblem from backend.services import claude_service, prompt_templates router = APIRouter(prefix="/api", tags=["practice"]) @router.post("/practice", response_model=PracticeResponse) async def generate_practice( body: PracticeRequest, user: dict = Depends(get_current_user), ): """Generate 3 adaptive practice problems for the given topic and grade.""" system = prompt_templates.practice_system(body.grade) user_msg = prompt_templates.practice_user(body.topic, body.count) try: raw = await claude_service.complete_json(system, user_msg) except ValueError as e: raise HTTPException(status_code=502, detail=str(e)) problems = [] for item in (raw if isinstance(raw, list) else []): problems.append( PracticeProblem( question=item.get("question", ""), answer=item.get("answer", ""), hint=item.get("hint", ""), difficulty=item.get("difficulty", "medium"), ) ) return PracticeResponse(problems=problems, session_id=body.session_id)