# retoor <retoor@molodetz.nl>

import logging
from fastapi import APIRouter, Request, HTTPException
from devplacepy.services.manager import service_manager

logger = logging.getLogger(__name__)
router = APIRouter()


def _service():
    svc = service_manager.get_service("openai")
    if svc is None:
        raise HTTPException(status_code=503, detail="Gateway is not available")
    return svc


@router.post("/v1/chat/completions")
async def chat_completions(request: Request):
    return await _service().handle(request, "chat/completions")


@router.post("/v1/embeddings")
async def embeddings(request: Request):
    return await _service().handle(request, "embeddings")


@router.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
async def passthrough(request: Request, path: str):
    return await _service().handle(request, path)
