from typing import Any

from fastapi import APIRouter, HTTPException, status

from app.services.web_search import (
    BochaSearchAPIError,
    BochaSearchConfigError,
    WebSearchRequest,
    search_bocha_web,
)

router = APIRouter()


@router.post(
    "/web-search",
    summary="Web Search",
    description="调用 Bocha Web Search API 获取实时网页搜索结果",
)
async def web_search(request: WebSearchRequest) -> dict[str, Any]:
    try:
        return await search_bocha_web(request)
    except BochaSearchConfigError as exc:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail=str(exc),
        ) from exc
    except BochaSearchAPIError as exc:
        raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
