from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")

    DATABASE_URL: str
    JWT_SECRET: str
    JWT_EXPIRY_MINUTES: int = 30
    MAX_CONCURRENT_SESSIONS: int = 5
    SESSION_LOCK_TTL_MINUTES: int = 30
    DATA_RETENTION_HOURS: int = 48
    BCRYPT_ROUNDS: int = 12

    # LiteLLM proxy
    LITELLM_API_KEY: str
    LITELLM_PROVIDER_BASE_URL: str
    LITELLM_PROVIDER_MODEL_NAME: str
    LITELLM_TIMEOUT_SECONDS: int = 30

    SARVAM_API_KEY: str

    # Free-conversation mode: Sarvam's diarized batch queue has been unreliable in testing
    # (jobs stuck mid-processing, then stuck queued on a later attempt) — default to the
    # plain (non-diarized) transcription + LLM text-segmentation path so the endpoint
    # actually works. Flip to true in .env to re-test diarization; no code change needed.
    CONVERSATION_DIARIZATION_ENABLED: bool = False

    # Real-time WebSocket relay for free-conversation mode instead of the VAD-segmented
    # batch pipeline above. Sarvam's streaming STT API does not support diarization, so
    # streaming mode always runs single-speaker (speakerId "0"), same as the non-diarized
    # branch of the batch path. One flag flips every session between the two — no frontend
    # rebuild needed, since the frontend reads this off the session-start response.
    CONVERSATION_STREAMING_ENABLED: bool = False

    # How many times a Gemini classify call retries before giving up (batch endpoint and
    # conversation-stream WebSocket both use this) — kept tunable via .env rather than a
    # hardcoded constant so it can be adjusted without a code change/deploy.
    CLASSIFY_MAX_ATTEMPTS: int = 3

    # Max seconds one streaming utterance can run without Sarvam signaling END_SPEECH before
    # we force a flush anyway — guards against a continuous talker with no detectable pause.
    STREAM_MAX_UTTERANCE_SECONDS: int = 6

    # Optional — personal Gemini API key for local testing
    GEMINI_API_KEY: str | None = None

    ALLOWED_ORIGINS: list[str] = ["http://localhost:3000"]


settings = Settings()
