"""Copy Cyrillic-capable fonts to fonts/.

Run once after cloning: python migrations/setup_fonts.py

Windows: copies Arial from C:/Windows/Fonts (full Cyrillic support).
Linux:   copies DejaVuSans from /usr/share/fonts/truetype/dejavu/.
         If not installed: sudo apt install fonts-dejavu-core
"""
from __future__ import annotations

import platform
import shutil
from pathlib import Path

DEST = Path(__file__).parent.parent / "fonts"

# Platform-specific font sources
_WIN_MAP = {
    Path("C:/Windows/Fonts/arial.ttf"): "Arial.ttf",
    Path("C:/Windows/Fonts/arialbd.ttf"): "Arial-Bold.ttf",
}

_LINUX_CANDIDATES = [
    Path("/usr/share/fonts/truetype/dejavu"),
    Path("/usr/share/fonts/dejavu"),
    Path("/usr/share/fonts/TTF"),
]

_LINUX_MAP = {
    "DejaVuSans.ttf": "Arial.ttf",        # registered as "Arial" in pdf.py
    "DejaVuSans-Bold.ttf": "Arial-Bold.ttf",
}


def _copy_windows() -> None:
    for src, dst_name in _WIN_MAP.items():
        dst = DEST / dst_name
        if dst.exists():
            print(f"  {dst_name} already present — skipping")
            continue
        if not src.exists():
            print(f"  WARNING: {src} not found — skipping")
            continue
        shutil.copy2(src, dst)
        print(f"  Copied {src.name} → fonts/{dst_name}")


def _copy_linux() -> None:
    found_dir: Path | None = None
    for candidate in _LINUX_CANDIDATES:
        if candidate.exists():
            found_dir = candidate
            break

    if not found_dir:
        print(
            "  ERROR: DejaVu fonts not found.\n"
            "  Install with: sudo apt install fonts-dejavu-core\n"
            "  Then re-run: python migrations/setup_fonts.py"
        )
        return

    for src_name, dst_name in _LINUX_MAP.items():
        src = found_dir / src_name
        dst = DEST / dst_name
        if dst.exists():
            print(f"  {dst_name} already present — skipping")
            continue
        if not src.exists():
            print(f"  WARNING: {src} not found — skipping")
            continue
        shutil.copy2(src, dst)
        print(f"  Copied {src_name} → fonts/{dst_name}")


def main() -> None:
    DEST.mkdir(exist_ok=True)
    system = platform.system()
    print(f"Platform: {system}")
    if system == "Windows":
        _copy_windows()
    else:
        _copy_linux()
    print("Done.")


if __name__ == "__main__":
    main()
