from janito.llm.provider import LLMProvider
from janito.llm.model import LLMModelInfo
from janito.llm.auth import LLMAuthManager
from janito.llm.driver_config import LLMDriverConfig
from janito.drivers.openai.driver import OpenAIModelDriver
from janito.tools import get_local_tools_adapter
from janito.providers.registry import LLMProviderRegistry
from .model_info import MODEL_SPECS
from queue import Queue

available = OpenAIModelDriver.available
unavailable_reason = OpenAIModelDriver.unavailable_reason


class GroqProvider(LLMProvider):
    name = "groq"
    NAME = "groq"
    MAINTAINER = "Groq Inc. <support@groq.com>"
    MODEL_SPECS = MODEL_SPECS
    DEFAULT_MODEL = "moonshotai/kimi-k2-instruct"

    def __init__(
        self, auth_manager: LLMAuthManager = None, config: LLMDriverConfig = None
    ):
        if not self.available:
            self._tools_adapter = get_local_tools_adapter()
            self._driver = None
        else:
            self.auth_manager = auth_manager or LLMAuthManager()
            self._api_key = self.auth_manager.get_credentials(type(self).NAME)
            self._tools_adapter = get_local_tools_adapter()
            self._driver_config = config or LLMDriverConfig(model=self.DEFAULT_MODEL)
            if not self._driver_config.model:
                self._driver_config.model = self.DEFAULT_MODEL

            if not self._driver_config.api_key:
                self._driver_config.api_key = self._api_key
            self._driver_config.base_url = "https://api.groq.com/openai/v1"
            self.fill_missing_device_info(self._driver_config)
            self._driver = None

    @property
    def driver(self) -> OpenAIModelDriver:
        if not self.available:
            raise ImportError(f"GroqProvider unavailable: {self.unavailable_reason}")
        return self._driver

    @property
    def available(self):
        return available

    @property
    def unavailable_reason(self):
        return unavailable_reason

    def create_driver(self):
        driver = OpenAIModelDriver(
            tools_adapter=self._tools_adapter, provider_name=self.NAME
        )
        driver.config = self._driver_config
        return driver

    @property
    def model_name(self):
        return self._driver_config.model

    @property
    def driver_config(self):
        return self._driver_config

    def execute_tool(self, tool_name: str, event_bus, *args, **kwargs):
        self._tools_adapter.event_bus = event_bus
        return self._tools_adapter.execute_by_name(tool_name, *args, **kwargs)


LLMProviderRegistry.register(GroqProvider.NAME, GroqProvider)
