from hvac import Client
from pydantic import Field
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):
    # Config
    log_level: str = "warning"
    service_name: str = "MayaOCP"
    model: str = "gpt-4o"
    model_temperature: int = 0
    embedding_model: str = "text-embedding-3-large"
    message_limit: int = 10
    storage_bucket: str = "ocpreferences"
    vector_index: str = "MayaOCP"
    search_limit: int = 3
    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"]
    )
    azure_blob_connection_string: str = Field(
        default_factory=lambda: secret_map["azure_blob_connection_string"]
    )
    qdrant_url: str = Field(default_factory=lambda: secret_map["qdrant_api_url"])
    qdrant_api_key: str | None = Field(
        default_factory=lambda: secret_map["qdrant_api_key"]
    )
    mongodb_url: str = Field(
        default_factory=lambda: secret_map["cosmosdb_connection_string"]
    )

    async def init_mongo_db(self):
        from banortegpt.database.mongo_memory.models import Conversation
        from beanie import init_beanie
        from motor.motor_asyncio import AsyncIOMotorClient

        client = AsyncIOMotorClient(self.mongodb_url)

        await init_beanie(
            database=client.banortegptdos,
            document_models=[Conversation],
        )


config = Settings()  # type: ignore
