"""OL3-consistent AMBER parameters for modified RNA residues (via modXNA)."""
import os
from pathlib import Path

from ._data import (
    RESIDUES, TERMINI, MODXNA_BASE, NET_CHARGE, unit_name, expected_atom_count,
)

__all__ = [
    "RESIDUES", "TERMINI", "MODXNA_BASE", "NET_CHARGE",
    "unit_name", "expected_atom_count",
    "frcmod_path", "lib_path", "lib_paths", "leaprc_lines",
]

# Data files must resolve to real on-disk paths (tleap/parmed read them by
# path), so the package is always installed unzipped.
_DATA = Path(__file__).resolve().parent / "data"


def frcmod_path() -> str:
    """Absolute path to the shared frcmod.modxna."""
    return os.fspath(_DATA / "frcmod.modxna")


def lib_path(resname: str, terminus: str = "internal") -> str:
    """Absolute path to the .lib for a residue form.

    Raises KeyError for an unsupported resname or terminus.
    """
    name = unit_name(resname, terminus)
    return os.fspath(_DATA / "lib" / f"{name}.lib")


def lib_paths() -> dict[tuple[str, str], str]:
    """{(resname, terminus): absolute .lib path} for all 27 forms."""
    return {(r, t): lib_path(r, t) for r in RESIDUES for t in TERMINI}


def leaprc_lines(residues: list[str] | None = None,
                 termini: list[str] | None = None) -> list[str]:
    """tleap-ready lines: one loadamberparams plus a loadoff per form."""
    residues = list(RESIDUES) if residues is None else residues
    termini = list(TERMINI) if termini is None else termini
    lines = [f"loadamberparams {frcmod_path()}"]
    for r in residues:
        for t in termini:
            lines.append(f"loadoff {lib_path(r, t)}")
    return lines
