from __future__ import annotations

from functools import lru_cache
from types import SimpleNamespace
from typing import Any

from app.core.logging import InstrumentedGeminiModelAdapter
from app.llm.providers.gemini import GeminiAdapter


class GeminiModelAdapter:
    """Compat adapter that keeps the old generate_content(...) shape."""

    def __init__(self, api_key: str, model_name: str) -> None:
        self._provider = GeminiAdapter(api_key=api_key, default_model=model_name)
        self._model_name = model_name

    def generate_content(self, contents: Any, generation_config: Any = None):
        text_response = self._provider.generate_content_legacy(
            contents,
            generation_config=generation_config,
            model=self._model_name,
        )
        return SimpleNamespace(text=text_response.text)


@lru_cache(maxsize=16)
def get_gemini_model(api_key: str, model_name: str) -> Any:
    adapter = GeminiModelAdapter(api_key=api_key, model_name=model_name)
    return InstrumentedGeminiModelAdapter(adapter, provider="gemini", service="gemini_adapter")
