from fastapi import APIRouter, Request, HTTPException, Depends from fastapi.responses import JSONResponse from typing import Dict, Any import config from llm.models import LLMResponse from database.database import get_db_session # Removed Session import here from sqlalchemy.orm import Session # Added correct SQLAlchemy import from database.crud import get_all_analysis_records, delete_all_analysis_records, get_analysis_by_id router = APIRouter( prefix="/api", tags=["API"] ) @router.get("/requests") async def get_analysis_records_endpoint(db: Session = Depends(get_db_session)): """Get analysis records""" try: records = get_all_analysis_records(db) return JSONResponse( status_code=200, content={"data": records} ) except Exception as e: return JSONResponse( status_code=500, content={"error": str(e)} ) @router.post("/test-llm") async def test_llm_endpoint(db: Session = Depends(get_db_session)): """Test endpoint for LLM integration""" try: from llm.chains import llm test_prompt = "What is 1 + 1? Respond only with the number." response = llm.invoke(test_prompt) return { "status": "success", "message": "LLM integration test successful", "response": str(response) } except Exception as e: return JSONResponse( status_code=500, content={ "status": "error", "message": f"LLM test failed: {str(e)}" } ) @router.delete("/requests") async def delete_analysis_records_endpoint(db: Session = Depends(get_db_session)): """Delete analysis records""" try: deleted_count = delete_all_analysis_records(db) return JSONResponse( status_code=200, content={"message": f"Successfully deleted {deleted_count} records", "deleted_count": deleted_count} ) except Exception as e: return JSONResponse( status_code=500, content={"error": str(e)}) @router.get("/requests/{record_id}") async def get_analysis_record_endpoint(record_id: int, db: Session = Depends(get_db_session)): """Get specific analysis record by ID""" record = get_analysis_by_id(db, record_id) if not record: raise HTTPException(status_code=404, detail="Analysis record not found") return JSONResponse( status_code=200, content=record.dict() # Ensure proper data serialization )