import os
import tomllib
from pathlib import Path

datadir: Path = Path(os.getenv("UNWIND_DATA") or "./data")
cachedir: Path = Path(p) if (p := os.getenv("UNWIND_CACHEDIR")) else datadir / ".cache"
debug: bool = os.getenv("DEBUG") == "1"
loglevel: str = os.getenv("UNWIND_LOGLEVEL") or ("DEBUG" if debug else "INFO")
storage_path: Path = (
    Path(p) if (p := os.getenv("UNWIND_STORAGE")) else datadir / "db.sqlite"
)
config_path: Path = (
    Path(p) if (p := os.getenv("UNWIND_CONFIG")) else datadir / "config.toml"
)

with open(config_path, "rb") as fd:
    _config = tomllib.load(fd)

api_base: str = _config["api"].get("base", "/api/")
api_cors: str = _config["api"].get("cors", "*")
api_credentials: dict[str, str] = _config["api"].get("credentials", {})
