"""Chat API Routes — Mediated Messaging between Recruiters and Talents. Endpoints: POST /v1/chat/send — Recruiter sends message to talent POST /v1/chat/reply — Talent replies to conversation POST /v1/chat/unlock — Unlock identity (5 coins) GET /v1/chat/conversations — List conversations (recruiter view) GET /v1/chat/talent/conversations — List conversations (talent view) GET /v1/chat/history — Get message history All endpoints use crash-proof AI mediation via Gemini 3 series LOW tier. When AI is unavailable, algorithmic fallback kicks in — never crashes. """ from fastapi import APIRouter, Depends, Query, Request from loguru import logger from sqlalchemy.ext.asyncio import AsyncSession from app.database import get_db from app.schemas.chat import ( ChatHistoryResponse, ConversationListResponse, SendMessageRequest, SendMessageResponse, TalentReplyRequest, TalentReplyResponse, UnlockIdentityRequest, UnlockIdentityResponse, ) from app.services.chat_service import ( get_chat_history, get_recruiter_conversations, get_talent_conversations, send_recruiter_message, send_talent_reply, unlock_chat_identity, ) from app.utils.errors import error_response, ErrorCode, raise_error router = APIRouter(prefix="/v1/chat", tags=["Chat"]) @router.post("/send", response_model=SendMessageResponse) async def send_message( req: Request, body: SendMessageRequest, db: AsyncSession = Depends(get_db), ): """Recruiter sends a message to a talent. Message is AI-mediated via Gemini 3.5 Flash (LOW tier). """ recruiter_email = getattr(req.state, "user_email", None) if not recruiter_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) result = await send_recruiter_message( db=db, recruiter_email=recruiter_email, talent_id=body.talent_id, raw_message=body.message, existing_conversation_id=body.conversation_id, ) return result @router.post("/reply", response_model=TalentReplyResponse) async def reply_message( req: Request, body: TalentReplyRequest, db: AsyncSession = Depends(get_db), ): """Talent replies to a conversation. Reply is AI-summarized for the recruiter via Gemini 3.5 Flash (LOW tier). """ talent_email = getattr(req.state, "user_email", None) if not talent_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) result = await send_talent_reply( db=db, talent_email=talent_email, conversation_id=body.conversation_id, raw_reply=body.message, ) return result @router.post("/unlock", response_model=UnlockIdentityResponse) async def unlock_identity( req: Request, body: UnlockIdentityRequest, db: AsyncSession = Depends(get_db), ): """Unlock talent identity in chat. Costs 5 coins. After unlock, all future messages show real names. """ recruiter_email = getattr(req.state, "user_email", None) if not recruiter_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) result = await unlock_chat_identity( db=db, recruiter_email=recruiter_email, talent_id=body.talent_id, ) return result @router.get("/conversations", response_model=ConversationListResponse) async def list_conversations( req: Request, db: AsyncSession = Depends(get_db), ): """List conversations for the authenticated recruiter. Returns latest message per partner, sorted by time. """ recruiter_email = getattr(req.state, "user_email", None) if not recruiter_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) return await get_recruiter_conversations(db=db, recruiter_email=recruiter_email) @router.get("/talent/conversations", response_model=ConversationListResponse) async def list_talent_conversations( req: Request, db: AsyncSession = Depends(get_db), ): """List conversations for the authenticated talent. Returns latest message per conversation, sorted by time. """ talent_email = getattr(req.state, "user_email", None) if not talent_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) return await get_talent_conversations(db=db, talent_email=talent_email) @router.get("/history", response_model=ChatHistoryResponse) async def chat_history( req: Request, partner_id: str = Query(..., description="Partner ID (talent email for recruiter, conversation ID for talent)"), role: str = Query("RECRUITER", description="Role: RECRUITER or TALENT"), db: AsyncSession = Depends(get_db), ): """Get full message history for a conversation. For recruiters: partner_id = talent email For talents: partner_id = conversation ID """ user_email = getattr(req.state, "user_email", None) if not user_email: raise_error("Authentication required.", ErrorCode.TOKEN_INVALID, 401) return await get_chat_history( db=db, email=user_email, partner_id=partner_id, role=role, )