from typing import Protocol


class BaseQdrant:
    def __init__(
        self, *, url: str, api_key: str | None, collection: str | None = None
    ) -> None:
        self.collection = collection

    class Config(Protocol):
        qdrant_url: str
        qdrant_api_key: str | None

    @classmethod
    def from_config(cls, c: Config):
        return cls(url=c.qdrant_url, api_key=c.qdrant_api_key)

    @classmethod
    def from_vault(
        cls,
        vault: str,
        *,
        collection: str | None = None,
        url: str | None = None,
        token: str | None = None,
        mount_point: str = "secret",
    ):
        from hvac import Client

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

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

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

        return cls(
            url=secret_map["qdrant_api_url"],
            api_key=secret_map["qdrant_api_key"],
            collection=collection,
        )
