from pathlib import Path
from typing import Optional

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    data_path_: Optional[str] = None
    raw_path_: Optional[str] = None
    processed_path_: Optional[str] = None
    models_path_: Optional[str] = None
    external_path_: Optional[str] = None
    interim_path_: Optional[str] = None
    kubeconfig_path_: Optional[str] = None
    postgres_url: str
    elasticsearch_url: str
    elasticsearch_local_url: str
    elasticsearch_index: str
    ollama_url: str
    ollama_local_url: str
    ollama_model_name: str
    ollama_emb_model_name: str
    langfuse_host: str
    langfuse_public_key: str
    langfuse_secret_key: str
    hf_token: str
    hf_emb_model_name: str

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
    )

    @property
    def proj_root(self) -> Path:
        return Path(__file__).resolve().parents[1]

    def _resolve_path(self, path: Optional[str]) -> Optional[Path]:
        if path is None:
            return None
        return self.proj_root / path

    @property
    def data_path(self) -> Optional[Path]:
        return self._resolve_path(self.data_path_)

    @property
    def raw_path(self) -> Optional[Path]:
        return self._resolve_path(self.raw_path_)

    @property
    def processed_path(self) -> Optional[Path]:
        return self._resolve_path(self.processed_path_)

    @property
    def models_path(self) -> Optional[Path]:
        return self._resolve_path(self.models_path_)

    @property
    def external_path(self) -> Optional[Path]:
        return self._resolve_path(self.external_path_)

    @property
    def interim_path(self) -> Optional[Path]:
        return self._resolve_path(self.interim_path_)

    @property
    def kubeconfig_path(self) -> Optional[Path]:
        return self._resolve_path(self.kubeconfig_path_)

settings = Settings()