from __future__ import annotations

from pathlib import Path
from typing import NamedTuple

FONTS_DIR = Path(__file__).parent / "assets" / "fonts"

_INTER_REGULAR = str(FONTS_DIR / "Inter-Regular.ttf")
_INTER_BOLD = str(FONTS_DIR / "Inter-Bold.ttf")

_NOTO_KR = str(FONTS_DIR / "NotoSansKR.ttf")
_NOTO_JP = str(FONTS_DIR / "NotoSansJP.ttf")
_NOTO_SC = str(FONTS_DIR / "NotoSansSC.ttf")
_NOTO_AR = str(FONTS_DIR / "NotoSansArabic.ttf")
_NOTO_HE = str(FONTS_DIR / "NotoSansHebrew.ttf")


class FontSpec(NamedTuple):
    regular: str
    bold: str
    is_rtl: bool = False
    is_cjk: bool = False


_LOCALE_FONTS: dict[str, FontSpec] = {
    "ko": FontSpec(regular=_NOTO_KR, bold=_NOTO_KR, is_cjk=True),
    "ja": FontSpec(regular=_NOTO_JP, bold=_NOTO_JP, is_cjk=True),
    "zh": FontSpec(regular=_NOTO_SC, bold=_NOTO_SC, is_cjk=True),
    "ar": FontSpec(regular=_NOTO_AR, bold=_NOTO_AR, is_rtl=True),
    "he": FontSpec(regular=_NOTO_HE, bold=_NOTO_HE, is_rtl=True),
}

_DEFAULT_FONT = FontSpec(regular=_INTER_REGULAR, bold=_INTER_BOLD)


def get_font_spec(locale: str) -> FontSpec:
    """Return FontSpec for the given BCP 47 locale tag."""
    lang = locale.split("-")[0].lower()
    return _LOCALE_FONTS.get(lang, _DEFAULT_FONT)


def get_all_fallback_fonts(primary: str) -> list[str]:
    """Return all bundled fonts except the primary — for Canvas.font_fallbacks()."""
    all_fonts = [_NOTO_KR, _NOTO_JP, _NOTO_SC, _NOTO_AR, _NOTO_HE]
    return [f for f in all_fonts if f != primary]
