"""
CogniQ — Next-generation hybrid RAG framework.

pip install cogniq

────────────────────────────────────────
 QUICKSTART (3 lines)
────────────────────────────────────────
    from cogniq import RAG

    r = RAG()
    r += "Your document text here"
    print(r("Your question?"))

────────────────────────────────────────
 FULL POWER
────────────────────────────────────────
    r = RAG(embedder="sentence", graph=True)
    r.add("doc.txt")                  # add file
    r.use_ollama("llama3.2")          # attach local LLM
    result = r("What is this about?")
    result.show()                     # pretty print

────────────────────────────────────────
 CUSTOM PLUGIN
────────────────────────────────────────
    from cogniq.plugins import Plugin, register_plugin

    @register_plugin("my_plugin")
    class MyPlugin(Plugin):
        def post_search(self, results, query, **kw):
            return results[:3]  # keep top 3 only

    r = RAG(plugins=["my_plugin"])
────────────────────────────────────────
"""

__version__ = "0.2.0"
__author__ = "cogniq contributors"

from .rag import RAG, AskResult, SearchResult
from .embedders import (
    BaseEmbedder,
    TFIDFEmbedder,
    SentenceEmbedder,
    OpenAIEmbedder,
    OllamaEmbedder,
    CustomEmbedder,
    AutoEmbedder,
)
from .stores import BaseStore, MemoryStore, FAISSStore, AutoStore
from .chunkers import BaseChunker, FixedChunker, SentenceChunker, SmartChunker, AutoChunker
from .graph import KnowledgeGraph
from .plugins import Plugin, register_plugin, list_plugins, PluginChain

__all__ = [
    # Core
    "RAG",
    "AskResult",
    "SearchResult",
    # Embedders
    "BaseEmbedder",
    "TFIDFEmbedder",
    "SentenceEmbedder",
    "OpenAIEmbedder",
    "OllamaEmbedder",
    "CustomEmbedder",
    "AutoEmbedder",
    # Stores
    "BaseStore",
    "MemoryStore",
    "FAISSStore",
    "AutoStore",
    # Chunkers
    "BaseChunker",
    "FixedChunker",
    "SentenceChunker",
    "SmartChunker",
    "AutoChunker",
    # Graph
    "KnowledgeGraph",
    # Plugins
    "Plugin",
    "register_plugin",
    "list_plugins",
    "PluginChain",
]

# ─── Global convenience API ─────────────────────────────────
# from cogniq import add, ask, search  (no class needed)

_global_rag: RAG | None = None

def _get_global() -> RAG:
    global _global_rag
    if _global_rag is None:
        _global_rag = RAG()
    return _global_rag

def add(text, source=None, **meta):
    """Global add — uses a shared RAG instance."""
    return _get_global().add(text, source=source, **meta)

def ask(question: str, **kwargs) -> AskResult:
    """Global ask — uses a shared RAG instance."""
    return _get_global().ask(question, **kwargs)

def search(query: str, top_k: int = 5) -> list:
    """Global search — uses a shared RAG instance."""
    return _get_global().search(query, top_k=top_k)

def reset():
    """Reset the global RAG instance."""
    global _global_rag
    _global_rag = None
