"""
Configuration management for AI Service.
[AC-AISVC-01] Centralized configuration with environment variable support.
"""

from functools import lru_cache

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix="AI_SERVICE_", env_file=".env", extra="ignore")

    app_name: str = "AI Service"
    app_version: str = "0.1.0"
    debug: bool = False

    host: str = "0.0.0.0"
    port: int = 8080

    request_timeout_seconds: int = 20
    sse_ping_interval_seconds: int = 15

    log_level: str = "INFO"

    llm_provider: str = "openai"
    llm_api_key: str = ""
    llm_base_url: str = "https://api.openai.com/v1"
    llm_model: str = "gpt-4o-mini"
    llm_max_tokens: int = 2048
    llm_temperature: float = 0.7
    llm_timeout_seconds: int = 30
    llm_max_retries: int = 3

    database_url: str = "postgresql+asyncpg://postgres:postgres@localhost:5432/ai_service"
    database_pool_size: int = 10
    database_max_overflow: int = 20

    qdrant_url: str = "http://localhost:6333"
    qdrant_collection_prefix: str = "kb_"
    qdrant_vector_size: int = 768

    ollama_base_url: str = "http://localhost:11434"
    ollama_embedding_model: str = "nomic-embed-text"

    rag_top_k: int = 5
    rag_score_threshold: float = 0.01
    rag_min_hits: int = 1
    rag_max_evidence_tokens: int = 2000

    rag_two_stage_enabled: bool = True
    rag_two_stage_expand_factor: int = 10
    rag_hybrid_enabled: bool = True
    rag_rrf_k: int = 60
    rag_vector_weight: float = 0.7
    rag_bm25_weight: float = 0.3

    confidence_low_threshold: float = 0.5
    confidence_high_threshold: float = 0.8
    confidence_insufficient_penalty: float = 0.3
    max_history_tokens: int = 4000

    redis_url: str = "redis://localhost:6379/0"
    redis_enabled: bool = True
    dashboard_cache_ttl: int = 60
    stats_counter_ttl: int = 7776000
    slot_state_cache_ttl: int = 1800

    frontend_base_url: str = "http://localhost:3000"

    # Share link base URL (for container deployment, use external domain)
    share_link_base_url: str = ""


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