import os
import warnings
import logging
from dotenv import load_dotenv

load_dotenv()

# Third-party warnings known on Python 3.14 with current LangChain stack.
warnings.filterwarnings(
    "ignore",
    message=r"Core Pydantic V1 functionality isn't compatible with Python 3\.14 or greater\.",
    category=UserWarning,
)
warnings.filterwarnings(
    "ignore",
    message=r"Valid config keys have changed in V2:.*allow_population_by_field_name.*",
    category=UserWarning,
)

# Reduce noisy model startup output from HF/transformers stack.
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
os.environ.setdefault("HF_HUB_DISABLE_PROGRESS_BARS", "1")
os.environ.setdefault("TRANSFORMERS_VERBOSITY", "error")
logging.getLogger("transformers").setLevel(logging.ERROR)
logging.getLogger("sentence_transformers").setLevel(logging.ERROR)


def _normalize_gemini_env_vars() -> None:
    """Use a single Gemini API env var name to avoid duplicate-key warnings."""
    gemini_api_key = os.getenv("GEMINI_API_KEY")
    google_api_key = os.getenv("GOOGLE_API_KEY")

    if gemini_api_key and google_api_key:
        # The app uses GEMINI_API_KEY explicitly.
        os.environ.pop("GOOGLE_API_KEY", None)
        return
    if google_api_key and not gemini_api_key:
        os.environ["GEMINI_API_KEY"] = google_api_key


_normalize_gemini_env_vars()

class Config:
    DATABASE_URL = os.getenv("DATABASE_URL", "mongodb://localhost:27017")
    REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
    MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017")
    DB_NAME = os.getenv("DB_NAME", "Epicrisis")
    COLLECTION_NAME = os.getenv("COLLECTION_NAME", "HistoriaClinica")
    SECRET_KEY = os.getenv("SECRET_KEY", "cidti40") 
    GROQ_API_KEY = os.getenv("GROQ_API_KEY")
    GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
    GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

    # Modelos configurables por entorno
    LLM_PROVIDER = os.getenv("LLM_PROVIDER", "groq").lower()
    GEMINI_MODEL_DEFAULT = os.getenv("GEMINI_MODEL_DEFAULT", "gemini-2.5-flash")
    GEMINI_MODEL_HISTORIA = os.getenv("GEMINI_MODEL_HISTORIA", "gemini-2.5-flash")
    GROQ_MODEL_DEFAULT = os.getenv("GROQ_MODEL_DEFAULT", "openai/gpt-oss-120b")
    GROQ_MODEL_CLINICAL = os.getenv("GROQ_MODEL_CLINICAL", "openai/gpt-oss-120b")
    GROQ_MODEL_SOAT = os.getenv("GROQ_MODEL_SOAT", "openai/gpt-oss-120b")
    GROQ_MODEL_CIE10 = os.getenv("GROQ_MODEL_CIE10", "openai/gpt-oss-120b")
    GROQ_MODEL_CUPS = os.getenv("GROQ_MODEL_CUPS", "openai/gpt-oss-120b")
    GROQ_MODEL_RESUMEN = os.getenv("GROQ_MODEL_RESUMEN", "openai/gpt-oss-120b")
    GROQ_MODEL_HISTORIA = os.getenv("GROQ_MODEL_HISTORIA", "openai/gpt-oss-120b")
    GROQ_MODEL_HISTORIA_LIGHT = os.getenv("GROQ_MODEL_HISTORIA_LIGHT", "llama-3.1-8b-instant")
    GROQ_MODEL_LLM1 = os.getenv("GROQ_MODEL_LLM1", "openai/gpt-oss-120b")
    HISTORIA_MAX_CHARS = int(os.getenv("HISTORIA_MAX_CHARS", "20000"))
    HISTORIA_SUMMARY_THRESHOLD = int(os.getenv("HISTORIA_SUMMARY_THRESHOLD", "12000"))
    HISTORIA_SUMMARY_CHUNK = int(os.getenv("HISTORIA_SUMMARY_CHUNK", "4000"))
    HISTORIA_LARGE_THRESHOLD = int(os.getenv("HISTORIA_LARGE_THRESHOLD", "18000"))

config = Config()

def require_env(value: str, name: str):
    if not value:
        raise RuntimeError(f"Falta la variable de entorno requerida: {name}")
    return value
