from hvac import Client
from pydantic import Field
from dotenv import load_dotenv
from pydantic_settings import BaseSettings


client = Client(url="https://vault.ia-innovacion.work")

if not client.is_authenticated():
    raise Exception("Vault authentication failed")

secret_map = client.secrets.kv.v2.read_secret_version(
    path="banortegpt", mount_point="secret"
)["data"]["data"]

class Settings(BaseSettings):
    """
    Esta clase obtiene sus valores de variables de ambiente.
    Si no estan en el ambiente, los jala de nuestra Vault.
    """

    # Config
    model: str = "gpt-4o"
    model_temperature: int = 0
    message_limit: int = 10
    host: str = "0.0.0.0"
    port: int = 8000

    # API Keys
    azure_endpoint: str = Field(default_factory=lambda: secret_map["azure_endpoint"])
    openai_api_key: str = Field(default_factory=lambda: secret_map["openai_api_key"])
    openai_api_version: str = Field(
        default_factory=lambda: secret_map["openai_api_version"]
    )
    mongodb_url: str = Field(
        default_factory=lambda: secret_map["cosmosdb_connection_string"]
    )

    async def init_mongo_db(self):
        """Este helper inicia la conexion enter el MongoDB ORM y nuestra instancia"""

        from beanie import init_beanie
        from motor.motor_asyncio import AsyncIOMotorClient

        from banortegpt.database.mongo_memory.models import Conversation

        await init_beanie(
            database=AsyncIOMotorClient(self.mongodb_url).voz_del_cliente,
            document_models=[Conversation],
        )


config = Settings()
