from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path


@dataclass(frozen=True)
class Settings:
    root_aliases: dict[str, str]
    task_db_path: str


DEFAULT_ROOT_ALIASES = {
    "storage1": "/Volumes/8TB",
    "storage2": "/Volumes/8TB_RAID1",
}


def _load_root_aliases() -> dict[str, str]:
    # Minimal env override format: storage1=/path1,storage2=/path2
    raw = os.getenv("WEBMANAGER_ROOT_ALIASES", "").strip()
    if not raw:
        return dict(DEFAULT_ROOT_ALIASES)

    parsed: dict[str, str] = {}
    for entry in raw.split(","):
        if "=" not in entry:
            continue
        alias, root = entry.split("=", 1)
        alias = alias.strip()
        root = root.strip()
        if alias and root:
            parsed[alias] = root
    return parsed or dict(DEFAULT_ROOT_ALIASES)


def get_settings() -> Settings:
    default_task_db_path = str(Path(__file__).resolve().parents[1] / "data" / "tasks.db")
    task_db_path = os.getenv("WEBMANAGER_TASK_DB_PATH", default_task_db_path).strip()
    if not task_db_path:
        task_db_path = default_task_db_path
    return Settings(root_aliases=_load_root_aliases(), task_db_path=task_db_path)
