"""mail_summary backend configuration."""

from __future__ import annotations

import os
from pathlib import Path

from dotenv import load_dotenv

BASE_DIR = Path(__file__).resolve().parent

DEFAULT_DATABASE_URL = "postgresql://mail_summary:strongpassword@localhost:5437/mail_summary"


def _load_backend_env() -> None:
    """Load backend/.env. Docker Compose also injects the same file via env_file."""
    path = BASE_DIR / ".env"
    if path.is_file():
        load_dotenv(path)


_load_backend_env()

DATABASE_URL = os.getenv("DATABASE_URL", DEFAULT_DATABASE_URL).strip()

_cors = os.getenv("CORS_ORIGINS", "http://localhost:5173,http://localhost:4888")
CORS_ORIGINS = [origin.strip() for origin in _cors.split(",") if origin.strip()]

LLM_BASE_URL = os.getenv("LLM_BASE_URL", "http://host.docker.internal:11434").rstrip("/")
LLM_MODEL = os.getenv("LLM_MODEL", "qwen3:30b-a3b-instruct-2507-q4_K_M").strip()
LLM_TIMEOUT_SECONDS = float(os.getenv("LLM_TIMEOUT_SECONDS", "300"))
LLM_MAX_MAILS = int(os.getenv("LLM_MAX_MAILS", "80"))
LLM_BODY_CHARS = int(os.getenv("LLM_BODY_CHARS", "3000"))

UPLOAD_DIR = Path(os.getenv("UPLOAD_DIR", str(BASE_DIR / "uploads"))).resolve()
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
