from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.ext.asyncio import AsyncSession from typing import Optional from app.database import get_db from app.services.followup_engine import FollowupEngine from app.services.credit import CreditService from app.api.v1.deps import get_current_user_id router = APIRouter() @router.get("/strategies") async def list_strategies( user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): engine = FollowupEngine(db) await engine.ensure_default_strategies() return {"strategies": await engine.get_strategies()} @router.get("/pending") async def get_pending_followups( page: int = Query(1, ge=1), size: int = Query(20, ge=1, le=100), user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): engine = FollowupEngine(db) return await engine.get_pending_followups(user_id, page, size) @router.get("/logs") async def get_followup_logs( page: int = Query(1, ge=1), size: int = Query(20, ge=1, le=100), user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): engine = FollowupEngine(db) return await engine.get_followup_logs(user_id, page, size) @router.post("/{log_id}/send") async def mark_followup_sent( log_id: str, user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): engine = FollowupEngine(db) success = await engine.mark_sent(user_id, log_id) if not success: raise HTTPException(status_code=404, detail="Followup log not found") return {"status": "ok"} @router.post("/{log_id}/edit") async def edit_and_send_followup( log_id: str, body: dict, user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): edited_text = body.get("edited_text", "") if not edited_text: raise HTTPException(status_code=400, detail="edited_text is required") engine = FollowupEngine(db) success = await engine.mark_edited(user_id, log_id, edited_text) if not success: raise HTTPException(status_code=404, detail="Followup log not found") return {"status": "ok"} @router.get("/stats") async def get_followup_stats( user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): engine = FollowupEngine(db) return await engine.get_stats(user_id) @router.post("/scan") async def trigger_followup_scan( user_id: str = Depends(get_current_user_id), db: AsyncSession = Depends(get_db), ): credit_svc = CreditService(db) ok, balance = await credit_svc.deduct(user_id, "followup_scan") if not ok: raise HTTPException( status_code=402, detail=f"次数不足 (剩余 {balance:.1f}, 需要 2)" ) engine = FollowupEngine(db) result = await engine.scan_and_followup() return result