from __future__ import annotations

from pathlib import Path

FORBIDDEN_WINDOWS_CHARS = '<>:"/\\|?*'


def project_root() -> Path:
    return Path(__file__).resolve().parents[3]


def safe_filename(name: str) -> str:
    cleaned = "".join(" " if char in FORBIDDEN_WINDOWS_CHARS else char for char in name)
    cleaned = " ".join(cleaned.split()).strip()
    return cleaned.replace(" .", ".")


def output_stem(path: Path) -> str:
    return safe_filename(path.name[:-4] if path.name.lower().endswith(".pdf") else path.stem)


def ensure_dir(path: Path) -> Path:
    path.mkdir(parents=True, exist_ok=True)
    return path
