from fastapi import APIRouter, Depends, Request
from ...core.permission import require_login
import httpx

router = APIRouter()


@router.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
async def langgraph_proxy(
    path: str,
    request: Request,
    user: dict = Depends(require_login),
):
    from ...core.config import settings
    deerflow_base = settings.DEERFLOW_LANGGRAPH_URL

    body = None
    if request.method in ("POST", "PUT", "PATCH"):
        body = await request.json()

    async with httpx.AsyncClient() as client:
        try:
            url = f"{deerflow_base}/{path}"
            headers = {"Content-Type": "application/json"}
            res = await client.request(
                method=request.method,
                url=url,
                json=body,
                headers=headers,
                timeout=60.0,
            )
            return res.json()
        except Exception:
            return {"error": "DeerFlow服务不可用", "detail": "DeerFlow service unavailable"}
