"""One place to pick an ML interatomic potential for every tutorial.

    from models import get_calc
    calc = get_calc("mace-small")      # default, no account needed
    calc = get_calc("uma-s-1p1")       # Meta UMA via fairchem (needs a Hugging Face token)

MACE-MP-0 downloads its weights on first use (about 20-90 MB).
UMA is gated: accept the license at https://huggingface.co/facebook/UMA and run
`huggingface-cli login` (or set HF_TOKEN) before using it.
"""
import os


def device():
    try:
        import torch
        return "cuda" if torch.cuda.is_available() else "cpu"
    except ImportError:
        return "cpu"


def get_calc(name: str = "mace-small", task: str = "omat", dtype: str = "float64"):
    """dtype: float64 for geometry optimisation, float32 (about 2x faster) for molecular dynamics."""
    if os.path.exists(name):  # a fine-tuned MACE model file
        from mace.calculators import MACECalculator
        return MACECalculator(model_paths=name, device=device(), default_dtype=dtype)
    name = name.lower()
    if name.startswith("mace"):
        from mace.calculators import mace_mp
        size = name.split("-", 1)[1] if "-" in name else "small"
        return mace_mp(model=size, device=device(), default_dtype=dtype)
    if name.startswith("uma"):
        from fairchem.core import pretrained_mlip, FAIRChemCalculator
        predictor = pretrained_mlip.get_predict_unit(name, device=device())
        return FAIRChemCalculator(predictor, task_name=task)
    raise ValueError(f"unknown model {name!r}; use mace-small, mace-medium or uma-s-1p1")


if __name__ == "__main__":
    print("device:", device(), "| HF_TOKEN set:", bool(os.environ.get("HF_TOKEN")))
