""" Mail API — AI analysis and response suggestion endpoints. """ import logging from typing import List from fastapi import APIRouter, HTTPException, Query from .models import ( EmailAnalysisResult, ResponseSuggestion, ) from .mail_db import get_email from .ai_service import get_ai_email_service logger = logging.getLogger(__name__) router = APIRouter(prefix="/api/v1/mail", tags=["Mail"]) @router.post("/analyze/{email_id}", response_model=EmailAnalysisResult) async def analyze_email( email_id: str, user_id: str = Query(..., description="User ID"), ): """Run AI analysis on an email.""" email_data = await get_email(email_id, user_id) if not email_data: raise HTTPException(status_code=404, detail="Email not found") ai_service = get_ai_email_service() result = await ai_service.analyze_email( email_id=email_id, sender_email=email_data.get("sender_email", ""), sender_name=email_data.get("sender_name"), subject=email_data.get("subject", ""), body_text=email_data.get("body_text"), body_preview=email_data.get("body_preview"), ) return result @router.get("/suggestions/{email_id}", response_model=List[ResponseSuggestion]) async def get_response_suggestions( email_id: str, user_id: str = Query(..., description="User ID"), ): """Get AI-generated response suggestions for an email.""" email_data = await get_email(email_id, user_id) if not email_data: raise HTTPException(status_code=404, detail="Email not found") ai_service = get_ai_email_service() # Use stored analysis if available from .models import SenderType, EmailCategory as EC sender_type = SenderType(email_data.get("sender_type", "unbekannt")) category = EC(email_data.get("category", "sonstiges")) suggestions = await ai_service.suggest_response( subject=email_data.get("subject", ""), body_text=email_data.get("body_text", ""), sender_type=sender_type, category=category, ) return suggestions