from __future__ import annotations

import httpx

from .models import (
    OCRRequestRequest,
    OCRResponse,
    VisionAnalyzeRequestRequest,
    VisionAnalyzeResponse,
    VisionModelsResponse,
)


class VisionVisionAPI:
    """API endpoints for Vision."""

    def __init__(self, client: httpx.AsyncClient):
        """Initialize sub-client with shared httpx client."""
        self._client = client

    async def analyze_create(
        self,
        data: VisionAnalyzeRequestRequest,
    ) -> VisionAnalyzeResponse:
        """
        Analyze image

        Analyze an image with vision model.
        """
        url = "/api/vision/analyze/"
        response = await self._client.post(url, json=data.model_dump(mode="json", exclude_unset=True, exclude_none=True))
        if not response.is_success:
            try:
                error_body = response.json()
            except Exception:
                error_body = response.text
            msg = f"{response.status_code}: {error_body}"
            raise httpx.HTTPStatusError(
                msg, request=response.request, response=response
            )
        return VisionAnalyzeResponse.model_validate(response.json())


    async def models_retrieve(self) -> VisionModelsResponse:
        """
        Get vision models

        Get supported vision models.
        """
        url = "/api/vision/models/"
        response = await self._client.get(url)
        if not response.is_success:
            try:
                error_body = response.json()
            except Exception:
                error_body = response.text
            msg = f"{response.status_code}: {error_body}"
            raise httpx.HTTPStatusError(
                msg, request=response.request, response=response
            )
        return VisionModelsResponse.model_validate(response.json())


    async def ocr_create(self, data: OCRRequestRequest) -> OCRResponse:
        """
        OCR extraction

        Extract text from image using OCR.
        """
        url = "/api/vision/ocr/"
        response = await self._client.post(url, json=data.model_dump(mode="json", exclude_unset=True, exclude_none=True))
        if not response.is_success:
            try:
                error_body = response.json()
            except Exception:
                error_body = response.text
            msg = f"{response.status_code}: {error_body}"
            raise httpx.HTTPStatusError(
                msg, request=response.request, response=response
            )
        return OCRResponse.model_validate(response.json())


