# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Generate portable cases from fixed inputs and the reference."""

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parents[1] / "reference"))
import rir


def generate():
    cases = [
        ("unequal RIRs and relative lines in closure", "phase_fractions",
         [[120, 45, 10], [4, 1.5, 0.5], [0.5, 1, 1]]),
        ("zero peak has zero phase fraction", "phase_fractions", [[0, 30], [2, 3], [1, 1]]),
        ("one included phase exhausts closure", "phase_fractions", [[20], [2], [0.5]]),
        ("non-strongest lines for analyte and spike", "spiked_fractions",
         [[80, 18], [2, 0.75], [0.5, 0.8], 100, 0.5, 0.25]),
        ("spike differs from the RIR reference phase", "spiked_fractions",
         [[80, 18], [4, 1.5], [0.5, 0.8], 100, 0.5, 0.25, 2]),
        ("Davis Table 2 spiked fractions", "spiked_fractions",
         [[4661, 2298], [4.35, 2.98], [1, 1], 631, 1, 0.2144]),
        ("zero analyte areas return zero fractions", "spiked_fractions",
         [[0, 0], [2, 3], [1, 1], 100, 1, 0.1]),
    ]
    return {
        "format": 1,
        "method": "reference-intensity-ratio-xrd",
        "version": "0.1.0",
        "interface": (
            "phase_fractions(areas, rirs, relative_intensities) returns normalized mass fractions "
            "for included phases in input order. spiked_fractions(areas, rirs, relative_intensities, "
            "standard_area, standard_relative, standard_fraction, standard_rir=1.0) returns analyte mass fractions "
            "of the spiked mixture. Relative intensities and the measured standard mass fraction "
            "are fractions, not percentages. Measured signals are net areas or calibrated net heights on a common scale. "
            "standard_rir is the spike phase RIR against the common reference used for the analytes."
        ),
        "cases": [
            {"name": name, "operation": operation, "args": args,
             "expected": getattr(rir, operation)(*args), "tol": 1e-12}
            for name, operation, args in cases
        ],
    }


if __name__ == "__main__":
    print(json.dumps(generate(), indent=2))
