"""Swapper vision provider for node-worker."""
import logging
from typing import Any, Dict, List, Optional

import httpx

from config import SWAPPER_URL, DEFAULT_VISION

logger = logging.getLogger("provider.swapper_vision")


async def infer(
    images: Optional[List[str]] = None,
    prompt: str = "",
    model: str = "",
    system: str = "",
    max_tokens: int = 1024,
    temperature: float = 0.2,
    timeout_s: float = 60.0,
) -> Dict[str, Any]:
    model = model or DEFAULT_VISION

    payload: Dict[str, Any] = {
        "model": model,
        "prompt": prompt or "Describe this image.",
        "max_tokens": max_tokens,
        "temperature": temperature,
    }
    if images:
        payload["images"] = images
    if system:
        payload["system"] = system

    async with httpx.AsyncClient(timeout=timeout_s) as c:
        resp = await c.post(f"{SWAPPER_URL}/vision", json=payload)
        resp.raise_for_status()
        data = resp.json()
        return {
            "text": data.get("text", data.get("response", "")),
            "model": model,
            "provider": "swapper_vision",
        }
