from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8', case_sensitive=False)

    app_name: str = 'ai-call-quality-rag'
    app_env: str = 'development'
    log_level: str = 'INFO'
    api_host: str = '0.0.0.0'
    api_port: int = 8080

    # Qdrant
    qdrant_host: str = 'qdrant'
    qdrant_port: int = 6333
    qdrant_collection: str = 'call_chunks'
    qdrant_https: bool = False
    qdrant_api_key: str | None = None

    # Redis
    redis_url: str = 'redis://redis:6379/0'
    embedding_cache_ttl_seconds: int = 86400
    retrieval_cache_ttl_seconds: int = 300

    # MySQL
    mysql_host: str = 'mysql'
    mysql_port: int = 3306
    mysql_user: str = 'appuser'
    mysql_password: str = 'apppassword'
    mysql_db: str = 'call_quality'

    # ClickHouse
    clickhouse_host: str = 'clickhouse'
    clickhouse_port: int = 8123
    clickhouse_user: str = 'default'
    clickhouse_password: str = ''
    clickhouse_db: str = 'analytics'

    # Embeddings / reranking
    embedding_model_name: str = 'intfloat/e5-large-v2'
    reranker_model_name: str = 'BAAI/bge-reranker-large'
    embedding_device: str = 'cpu'

    # LLM (OpenAI-compatible)
    llm_base_url: str = 'https://api.openai.com/v1'
    llm_api_key: str = 'change-me'
    llm_model: str = 'gpt-4o-mini'
    llm_timeout_seconds: int = 60

    # Retrieval defaults
    retrieval_top_k: int = 8
    retrieval_candidate_k: int = 40
    hybrid_keyword_weight: float = 0.2


@lru_cache
def get_settings() -> Settings:
    return Settings()
