"""
向量存储模块

提供可插拔的向量存储后端，支持 Qdrant 等专业向量数据库。
与业务数据库完全解耦，segment 表只存业务数据，向量数据存在向量数据库中。
"""
from ai_platform.knowledge.vector_store.base import BaseVectorStore, VectorPoint, VectorSearchResult
from ai_platform.knowledge.vector_store.qdrant_store import QdrantVectorStore

__all__ = [
    'BaseVectorStore',
    'VectorPoint',
    'VectorSearchResult',
    'QdrantVectorStore',
    'get_vector_store',
]

# 单例缓存
_vector_store_instance: BaseVectorStore | None = None


def get_vector_store() -> BaseVectorStore:
    """
    工厂函数：根据配置获取向量存储实例（单例）
    """
    global _vector_store_instance
    if _vector_store_instance is not None:
        return _vector_store_instance

    from app.config import settings

    store_type = getattr(settings, 'VECTOR_STORE_TYPE', 'qdrant')

    if store_type == 'qdrant':
        _vector_store_instance = QdrantVectorStore(
            host=getattr(settings, 'QDRANT_HOST', 'localhost'),
            port=getattr(settings, 'QDRANT_PORT', 6333),
            api_key=getattr(settings, 'QDRANT_API_KEY', None),
            grpc_port=getattr(settings, 'QDRANT_GRPC_PORT', 6334),
            prefer_grpc=getattr(settings, 'QDRANT_PREFER_GRPC', False),
        )
    else:
        raise ValueError(f'不支持的向量存储类型: {store_type}')

    return _vector_store_instance
