
"""
RAG Configuration Module
Centralizes Qdrant and LLM configuration.
ADAPTED: Uses Anthropic for LLM and FastEmbed for Embeddings.
"""

import os
import yaml
from dataclasses import dataclass
from typing import Optional, List

# Global fastembed model (lazy loaded)
_embedding_model = None

@dataclass
class QdrantConfig:
    url: str
    api_key: str
    collection_name: str
    vector_size: int = 384 # FastEmbed default

@dataclass
class LLMConfig:
    api_key: str
    model: str = "claude-3-haiku-20240307"

EMBEDDING_MODEL = "BAAI/bge-small-en-v1.5"

class RAGConfig:
    def __init__(self, config_path: str = "E:/genesis-system/config/qdrant_config.yaml"):
        self.config_path = config_path
        self.qdrant: Optional[QdrantConfig] = None
        self.llm: Optional[LLMConfig] = None
        self._load_config()

    def _load_config(self):
        # Load from YAML if exists
        config_data = {}
        if os.path.exists(self.config_path):
            with open(self.config_path, 'r') as f:
                config_data = yaml.safe_load(f)

        # Parse Qdrant Config
        q_data = config_data.get('qdrant', {})
        self.qdrant = QdrantConfig(
            url=q_data.get('url', os.getenv('QDRANT_URL', "https://qdrant-b3knu-u50607.a.elestio.app:6333")),
            api_key=q_data.get('api_key', os.getenv('QDRANT_API_KEY', "7b74e6621bd0e6650789f6662bca4cbf4143d3d1d710a0002b3b563973ca6876")),
            collection_name="rag_mastery_test_fastembed", # NEW TEST COLLECTION
            vector_size=384
        )

        # Parse LLM Config (Anthropic)
        q_emb_data = config_data.get('embeddings', {})
        yaml_key = q_emb_data.get('api_key', "")
        
        if yaml_key.startswith("sk-ant"):
            api_key = yaml_key
        else:
            # Updated to use the specific key found in Genesis Credentials additions.txt
            api_key = "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"

        self.llm = LLMConfig(
            api_key=api_key
        )

    def get_qdrant_client(self):
        from qdrant_client import QdrantClient
        return QdrantClient(
            url=self.qdrant.url,
            api_key=self.qdrant.api_key,
            timeout=10
        )

    def get_anthropic_client(self):
        from anthropic import Anthropic
        # Ensure we have a key
        if not self.llm.api_key:
            # Fallback for testing execution flow if key missing (mocking)
            print("WARNING: No Anthropic Key found. LLM calls will fail.")
        return Anthropic(api_key=self.llm.api_key)

    def get_embedding(self, text: str) -> List[float]:
        """Helper to get embedding from FastEmbed."""
        global _embedding_model
        if _embedding_model is None:
            from fastembed import TextEmbedding
            _embedding_model = TextEmbedding()
        
        # FastEmbed returns a generator of numpy arrays
        embedding = list(_embedding_model.embed([text]))[0]
        return embedding.tolist()

# Global singleton
config = RAGConfig()
