from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Mapping

APP_DIR_NAME = "IuVeConnectAgent"


@dataclass(slots=True)
class MacOSAdapterPaths:
    home_dir: Path
    application_support_root: Path
    logs_root: Path
    runtime_dir: Path
    config_dir: Path

    def to_safe_dict(self) -> dict[str, str]:
        return {
            "home_dir": str(self.home_dir),
            "application_support_root": str(self.application_support_root),
            "logs_root": str(self.logs_root),
            "runtime_dir": str(self.runtime_dir),
            "config_dir": str(self.config_dir),
        }


def _expand(raw: str) -> Path:
    return Path(raw).expanduser().resolve()


def build_macos_paths(env: Mapping[str, str] | None = None) -> MacOSAdapterPaths:
    source = dict(env) if env is not None else {}

    home_raw = source.get("HOME") or "~"
    home_dir = _expand(home_raw)

    application_support_root = home_dir / "Library" / "Application Support" / APP_DIR_NAME
    logs_root = home_dir / "Library" / "Logs" / APP_DIR_NAME
    runtime_dir = application_support_root / "runtime"
    config_dir = application_support_root / "config"

    return MacOSAdapterPaths(
        home_dir=home_dir,
        application_support_root=application_support_root,
        logs_root=logs_root,
        runtime_dir=runtime_dir,
        config_dir=config_dir,
    )
