import httpx
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Optional, Any, Dict, List

router = APIRouter()

class ProxyRequest(BaseModel):
    url: str
    headers: Dict[str, str] = {}
    body: Dict[str, Any] = {}

import json

@router.post("/proxy")
async def proxy_llm(req: ProxyRequest):
    try:
        async with httpx.AsyncClient(timeout=180.0) as client:
            resp = await client.post(
                req.url,
                headers={k: v for k, v in req.headers.items() if k.lower() not in ('host', 'origin', 'referer')},
                json=req.body
            )
            raw = resp.text
            try:
                return resp.json()
            except json.JSONDecodeError:
                try:
                    return json.loads(raw[:raw.find('\n')])
                except (json.JSONDecodeError, ValueError):
                    return {"content": raw}
    except httpx.TimeoutException:
        raise HTTPException(status_code=504, detail="AI provider timeout")
    except httpx.ConnectError as e:
        msg = f"Cannot connect to AI provider: {e}"
        if 'localhost' in req.url or '127.0.0.1' in req.url:
            msg += "\nNếu app chạy trong Docker, localhost trỏ vào container, không ra host.\nHãy thay localhost bằng host.docker.internal hoặc IP bridge Docker (172.17.0.1)."
        raise HTTPException(status_code=502, detail=msg)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
