from dishka import FromDishka
from dishka.integrations.fastapi import DishkaRoute
from fastapi import APIRouter
from pydantic import BaseModel, Field

from template_project.application.common.embedding import Embedder

router = APIRouter(route_class=DishkaRoute, tags=["Embedding"])


class GetEmbeddingRequest(BaseModel):
    text: str = Field(
        ..., min_length=1, description="Text to encode", examples=["python backend developer with django"]
    )

    model_config = {"json_schema_extra": {"example": {"text": "python backend developer with django"}}}


class GetEmbeddingResponse(BaseModel):
    embedding: list[float] = Field(..., description="Embedding vector")

    model_config = {
        "json_schema_extra": {
            "example": {
                "embedding": [0.1, 0.2, 0.3, 0.4, 0.5],
            }
        }
    }


@router.post(
    "/get_embedding",
    summary="Get embedding",
    description="Encode text into embedding vector",
    responses={
        200: {"description": "Embedding generated successfully", "model": GetEmbeddingResponse},
    },
)
async def get_embedding(
    request: GetEmbeddingRequest,
    embedder: FromDishka[Embedder],
) -> GetEmbeddingResponse:
    embedding = await embedder.encode(request.text)
    return GetEmbeddingResponse(embedding=embedding)
