"""Clairic's Transformers loader and System One extraction interface."""

from __future__ import annotations

import asyncio
import base64
import io
import json
from pathlib import Path
from typing import Any, Mapping

from clairic_runtime.api import PredictRequest, serialize_response
from clairic_runtime.backend import ClairicFlashModel
from clairic_runtime.errors import ClairRequestError
from clairic_runtime.large import Clairic27BModel
from clairic_runtime.settings import RuntimeSettings


def _request(value: Mapping[str, Any] | PredictRequest) -> PredictRequest:
    if isinstance(value, PredictRequest):
        return value
    from PIL import Image, ImageOps

    request = dict(value)
    if "state" in request and not isinstance(request["state"], str):
        request["state"] = json.dumps(request["state"], ensure_ascii=False)
    images = request.get("images")
    if isinstance(images, (list, tuple)):
        images = list(images)
        for index, image in enumerate(images):
            if isinstance(image, Image.Image):
                buffer = io.BytesIO()
                ImageOps.exif_transpose(image).convert("RGBA").save(
                    buffer, format="PNG"
                )
                images[index] = "data:image/png;base64," + base64.b64encode(
                    buffer.getvalue()
                ).decode("ascii")
        request["images"] = images
    return PredictRequest.model_validate(request)


class ClairicModel:
    """A model and its typed extraction head, with synchronous and async APIs."""

    def __init__(self, runtime: ClairicFlashModel, public_name: str) -> None:
        self.runtime = runtime
        self.model_id = public_name
        names = {public_name, runtime.model_id}
        self._model_selectors = names | {"@cf/cloudflare/" + name for name in names}
        self.processor = runtime.processor
        self.backbone = runtime.engine.backbone
        self.head = runtime.engine.head
        self.device = runtime.engine.device

    async def predict_async(
        self, request: Mapping[str, Any] | PredictRequest
    ) -> dict[str, Any]:
        validated = _request(request)
        if validated.model is not None and validated.model not in self._model_selectors:
            raise ClairRequestError(
                f"Unsupported model '{validated.model}'. Use '{self.model_id}'."
            )
        output = await self.runtime.predict_async(validated)
        return serialize_response(validated, output, self.model_id)

    def predict(self, request: Mapping[str, Any] | PredictRequest) -> dict[str, Any]:
        """Return structured values and scores for a System One: Extract request."""
        try:
            asyncio.get_running_loop()
        except RuntimeError:
            return asyncio.run(self.predict_async(request))
        raise RuntimeError(
            "Use await model.predict_async(request) inside an active event loop"
        )

    def eval(self) -> ClairicModel:
        self.backbone.eval()
        self.head.eval()
        return self

    def close(self) -> None:
        self.runtime.shutdown()


def load_release_model(
    model_path: str | Path,
    device: Any = "cuda",
    dtype: Any = None,
    max_length: int = 65_273,
    **from_pretrained_kwargs: Any,
) -> tuple[ClairicModel, Any]:
    """Load a local snapshot or a Hugging Face repository with Transformers."""
    path = Path(model_path)
    if not path.is_dir():
        from huggingface_hub import snapshot_download

        path = Path(snapshot_download(str(model_path)))
    configuration = json.loads((path / "clair_head_config.json").read_text())
    large = int(configuration["hidden_size"]) == 5120
    model_id = "clairic-27b" if large else "clairic-flash"
    settings = RuntimeSettings(
        model_path=str(path),
        version="1",
        model_id=model_id,
        max_length=max_length,
        max_concurrency=1,
    )
    implementation = Clairic27BModel if large else ClairicFlashModel
    runtime = implementation.load(
        settings, device=str(device), dtype=dtype, **from_pretrained_kwargs
    )
    return ClairicModel(
        runtime, "clairic" if large else "clairic-flash"
    ), runtime.processor


def systemone(
    model: ClairicModel,
    processor: Any,
    request: Mapping[str, Any] | PredictRequest,
    max_length: int | None = None,
) -> dict[str, Any]:
    """Answer a System One: Extract request with typed fields and confidence."""
    if processor is not model.processor:
        raise ValueError("Use the processor returned with this model")
    if max_length is not None and max_length != model.runtime.settings.max_length:
        raise ValueError("Set max_length when calling load_release_model")
    return model.predict(request)
