from pathlib import Path

from pydantic_settings import BaseSettings, SettingsConfigDict

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


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=str(BACKEND_ROOT / ".env"),
        env_file_encoding="utf-8",
        extra="ignore",
    )

    llm_base_url: str = "https://api.novita.ai/openai/v1"
    llm_api_key: str = ""
    llm_model: str = "qwen/qwen3.6-27b"
    llm_temperature: float = 0.0
    llm_max_retries: int = 1

    data_dir: str = "data"
    output_dir: str = "outputs"
    upload_dir: str = "uploads"
    skills_dir: str = "../../skills"
    cors_origins: str = "http://localhost:5173,http://127.0.0.1:5173"

    @property
    def data_path(self) -> Path:
        return (BACKEND_ROOT / self.data_dir).resolve()

    @property
    def output_path(self) -> Path:
        p = (BACKEND_ROOT / self.output_dir).resolve()
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def upload_path(self) -> Path:
        p = (BACKEND_ROOT / self.upload_dir).resolve()
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def skills_path(self) -> Path:
        return (BACKEND_ROOT / self.skills_dir).resolve()

    @property
    def cors_origin_list(self) -> list[str]:
        return [o.strip() for o in self.cors_origins.split(",") if o.strip()]


settings = Settings()
