"""欧洲国家代码映射：ISO 3166-1 alpha-2 代码（大写）→ 中文名。"""
from pathlib import Path

from app.core.config import settings

COUNTRY_MAP: dict[str, str] = {
    "AT": "奥地利",
    "NL": "荷兰",
    "FR": "法国",
    "GB": "英国",
    "DE": "德国",
    # 后续新增国家在此追加
}


def country_name(code: str) -> str | None:
    """按代码返回中文名，未知代码返回 None。"""
    return COUNTRY_MAP.get(code.strip().upper())


def resolve_country(template_rel_path: str) -> tuple[str | None, str | None]:
    """从模板相对路径首段解析国家，返回 (代码, 中文名)；无匹配返回 (None, None)。"""
    first = template_rel_path.split("/", 1)[0].strip().upper()
    name = COUNTRY_MAP.get(first)
    return (first, name) if name else (None, None)


def find_templates(country_code: str) -> list[Path]:
    """返回某国家模板目录下按文件名排序的 .docx / .xlsx / .xlsm / .pdf 模板列表。"""
    template_dir = Path(settings.template_dir) / country_code.lower()
    if not template_dir.exists():
        return []
    files = [
        *template_dir.glob("*.docx"),
        *template_dir.glob("*.xlsx"),
        *template_dir.glob("*.xlsm"),
        *template_dir.glob("*.pdf"),
    ]
    return sorted(p for p in files if p.is_file())
