from fastapi import APIRouter, HTTPException, Query from app.core.dependencies import get_history_service router = APIRouter(prefix="/api/v1/history", tags=["history"]) @router.get("") def list_history( limit: int = Query(50, ge=0), offset: int = Query(0, ge=0), ): svc = get_history_service() queries = svc.list(limit=limit, offset=offset) total_row = _get_total_count(svc) return { "queries": queries, "total": total_row, "limit": limit, "offset": offset, } def _get_total_count(svc) -> int: stats = svc.get_stats() return stats["total_queries"] @router.get("/stats") def get_stats(): svc = get_history_service() return svc.get_stats() @router.get("/{query_id}") def get_history_detail(query_id: int): svc = get_history_service() record = svc.get(query_id) if record is None: raise HTTPException(status_code=404, detail="Query not found") return record @router.delete("/{query_id}") def delete_history(query_id: int): svc = get_history_service() deleted = svc.delete(query_id) if not deleted: raise HTTPException(status_code=404, detail="Query not found") return {"status": "ok", "deleted_id": query_id} @router.delete("") def clear_all_history(): svc = get_history_service() count = svc.clear_all() return {"status": "ok", "deleted_count": count}