from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks from app.services.auth_service import get_current_user from app.models.user import User from app.models.task import Task from app.services.llm_service import llm_service from app.services.memory_service import memory_service from typing import List from datetime import datetime router = APIRouter() @router.post("/tasks/generate", response_model=List[Task]) async def generate_tasks(patient_id: str, num_messages: int = 20, current_user: User = Depends(get_current_user)): """ Analyze recent chat history of the patient to generate caregiver tasks. """ if current_user.role != "caregiver": raise HTTPException(status_code=403, detail="Only caregivers can generate tasks") # Fetch chat history from Qdrant session # Note: Assuming session_id matches patient_id or is retrieved via user lookup # For now, we'll try to get the 'latest' session or a specific session if provided. # Simplifying: We'll search session collection for this patient's ID if stored, # OR we just rely on the frontend passing the session_id. # Since we don't have a direct 'get_chat_history(patient_id)' yet, we will mock this # OR use the 'sessions' collection if we indexed it by patient_id. # improved approach: The chat endpoint stores sessions by session_id. # We need a way to link patient -> session. # For this prototype, we will accept `history` in body OR just fetch the last known session from Qdrant if posssible. # FALLBACK: We will assume the Frontend sends the chat history for analysis # OR we just use a dummy history for testing if not connected. # Let's try to fetch the session from Qdrant using the patient_id as key (if we used that). session_data = memory_service.get_session(patient_id) history = session_data.get("history", []) if not history: return [] tasks_data = llm_service.analyze_conversation_for_tasks(history[-num_messages:]) created_tasks = [] for t in tasks_data: task = Task( patient_id=patient_id, description=t.get("description"), source_message=t.get("source_message"), urgency=t.get("urgency", "medium").lower(), category=t.get("category", "other").lower(), status="pending" ) await task.insert() created_tasks.append(task) return created_tasks @router.get("/tasks", response_model=List[Task]) async def get_tasks(patient_id: str = None, current_user: User = Depends(get_current_user)): """Get all tasks for a patient (or all if admin).""" if patient_id: return await Task.find(Task.patient_id == patient_id).sort("-created_at").to_list() return await Task.find_all().sort("-created_at").to_list() @router.patch("/tasks/{task_id}") async def update_task_status(task_id: str, status: str, current_user: User = Depends(get_current_user)): task = await Task.get(task_id) if not task: raise HTTPException(status_code=404, detail="Task not found") task.status = status await task.save() return task @router.delete("/tasks/{task_id}") async def delete_task(task_id: str, current_user: User = Depends(get_current_user)): task = await Task.get(task_id) if not task: raise HTTPException(status_code=404, detail="Task not found") await task.delete() return {"status": "deleted"}