"""``/api/training/mentor`` — AI Mentor endpoint. Students inside a TrainingSession can ask the AI mentor for hints, concept explanations, or resource suggestions. The mentor guides without giving away the full solution. """ from typing import Optional from fastapi import APIRouter, Depends, HTTPException, Request from pydantic import BaseModel from slowapi import Limiter from slowapi.util import get_remote_address from app.core.auth import get_current_user from app.services.mentor_service import get_mentor_response from app.services.supabase_service import fetch_scenario_by_id router = APIRouter() limiter = Limiter(key_func=get_remote_address) class MentorRequest(BaseModel): challengeId: str challengeType: str userQuestion: str userAnswer: str = "" conversationHistory: Optional[list[dict[str, str]]] = None @router.post("/api/training/mentor") @limiter.limit("10/minute") async def ask_mentor( req: MentorRequest, request: Request, user: dict = Depends(get_current_user), ): if not req.challengeId or not req.userQuestion: raise HTTPException(status_code=400, detail="challengeId و userQuestion مطلوبان") challenge_data = await fetch_scenario_by_id( "blue" if req.challengeType in ("code-fixing", "log-analysis", "vulnerability-hunter") else "red", req.challengeId, challenge_type=req.challengeType, ) if not challenge_data: raise HTTPException(status_code=404, detail="التحدي غير موجود") result = await get_mentor_response( challenge_type=req.challengeType, challenge_data=challenge_data, user_question=req.userQuestion, user_answer=req.userAnswer, conversation_history=req.conversationHistory, ) return result