from fastapi import HTTPException

from app.api.models.provider_default_model import ProviderDefaultModelCredential
from app.constants.enums import LLMType
from app.core.llm.models import ChatModel, TTSModel


class TenantLLMService:
    @classmethod
    def model_instance(cls, model_config: ProviderDefaultModelCredential, **kwargs):
        provider = model_config.provider
        model_type = model_config.model_type
        model_name = model_config.model_name
        api_key = model_config.credential.api_key
        base_url = model_config.credential.base_url

        kwargs.update({"provider": provider})
        if model_type == LLMType.CHAT:
            assert provider in ChatModel, f"Chat model from {provider} is not supported yet."
            return ChatModel[provider](api_key, model_name, base_url, **kwargs)
        elif model_type == LLMType.TTS:
            assert provider in TTSModel, f"TTS model from {provider} is not supported yet."
            return TTSModel[provider](api_key, model_name, base_url, **kwargs)

        raise HTTPException(status_code=404, detail=f"Model for {provider} is not supported yet.")
