from __future__ import annotations

from fastapi_admin.config._settings import llm


def _get_str(name: str, default: str = "") -> str:
    import os
    return os.getenv(name, default)


def _get_bool(name: str, default: bool = False) -> bool:
    import os
    return os.getenv(name, str(default).lower()).lower() == "true"


def _get_int(name: str, default: int) -> int:
    import os
    try:
        return int(os.getenv(name, str(default)))
    except ValueError:
        return default


def _get_float(name: str, default: float) -> float:
    import os
    try:
        return float(os.getenv(name, str(default)))
    except ValueError:
        return default


RAG_CONFIG = {
    "USE_SELF_HOSTED": True,
    "CHROMA_PERSIST_DIR": _get_str("RAG_CHROMA_PERSIST_DIR", ".chromadb_rag"),
    "CHROMA_HOST": _get_str("RAG_CHROMA_HOST", ""),
    "CHROMA_PORT": _get_int("RAG_CHROMA_PORT", 8010),
    "CHROMA_TOKEN": _get_str("RAG_CHROMA_TOKEN", ""),
    "CHROMA_AUTH_HEADER": _get_str("RAG_CHROMA_AUTH_HEADER", "X-Chroma-Token"),
    "EMBED_URL": _get_str("RAG_EMBED_URL", _get_str("GRAPH_RAG_EMBED_URL", "")),
    "EMBED_KEY": _get_str("RAG_EMBED_KEY", _get_str("GRAPH_RAG_EMBED_KEY", "")),
    "EMBED_MODEL": _get_str("RAG_EMBED_MODEL", _get_str("GRAPH_RAG_EMBED_MODEL", "")),
    "EMBED_DIM": _get_int("RAG_EMBED_DIM", 1024),
    "EMBED_BATCH_SIZE": _get_int("RAG_EMBED_BATCH_SIZE", 10),
    "RERANKER_URL": _get_str("RAG_RERANKER_URL", _get_str("GRAPH_RAG_RERANKER_URL", "")),
    "RERANKER_KEY": _get_str("RAG_RERANKER_KEY", _get_str("GRAPH_RAG_RERANKER_KEY", "")),
    "RERANKER_MODEL": _get_str("RAG_RERANKER_MODEL", _get_str("GRAPH_RAG_RERANKER_MODEL", "")),
    "LLM_BASE_URL": _get_str("LLM_BASE_URL", llm.LLM_BASE_URL),
    "LLM_MODEL": _get_str("LLM_MODEL", llm.LLM_MODEL),
    "LLM_API_KEY": _get_str("LLM_API_KEY", llm.LLM_API_KEY),
    "VECTOR_TOP_K": _get_int("RAG_VECTOR_TOP_K", 15),
    "RERANK_TOP_K": _get_int("RAG_RERANK_TOP_K", 5),
    "BM25_TOP_K": _get_int("RAG_BM25_TOP_K", 15),
    "RRF_K": _get_int("RAG_RRF_K", 60),
    "LLM_TEMPERATURE": _get_float("RAG_LLM_TEMPERATURE", 0.3),
    "LLM_MAX_TOKENS": _get_int("RAG_LLM_MAX_TOKENS", 2048),
    "LLM_TIMEOUT": _get_int("RAG_LLM_TIMEOUT", 120),
    "QUERY_REWRITING": _get_bool("RAG_QUERY_REWRITING", False),
    "HYBRID_SEARCH": _get_bool("RAG_HYBRID_SEARCH", True),
    "RERANKING": _get_bool("RAG_RERANKING", True),
}
