"""Reproduce the two dust-event scores from the same constructed report.

Check out https://github.com/harbor-framework/terminal-bench-science at
d4f6c91be7eba71a9fc279c3809317489a8dd374. Save dust-counterexample.csv
next to this script, then run:

    python3 -m pip install numpy==2.1.3 scipy==1.14.1
    python3 reproduce_dust_tie.py /path/to/terminal-bench-science

The script loads the upstream grader locally and prints both results.
"""

import argparse
import importlib.util
import json
from pathlib import Path


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("benchmark_checkout", type=Path)
    args = parser.parse_args()
    tests = (
        args.benchmark_checkout
        / "tasks/earth-sciences/atmospheric-sciences/dust-source-events/tests"
    )
    spec = importlib.util.spec_from_file_location("verifier", tests / "verifier.py")
    verifier = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(verifier)

    windows = verifier.load_window_ids(tests / "windows.csv")
    reference = verifier.load_activations(tests / "reference_activations.csv")
    edges = verifier.load_activations(tests / "edge_activations.csv")
    predictions = verifier.parse_predictions(
        Path(__file__).with_name("dust-counterexample.csv"), windows
    )
    thresholds = json.loads((tests / "thresholds.json").read_text())
    original = verifier.score(windows, predictions, reference, edges)
    original_match = verifier.match
    window_predictions = verifier._sorted(predictions["W004"])
    window_reference = verifier._sorted(reference["W004"])

    def match_with_alternative(reported, expected):
        pairs, reported, expected = original_match(reported, expected)
        if reported != window_predictions or expected != window_reference:
            return pairs, reported, expected

        ((reference_index, original_index),) = pairs
        alternative_index = reported.index(
            dict(onset_hour=5, cell_row=4, cell_col=4, category="cloud_associated")
        )
        target = expected[reference_index]

        def pair_priority(index):
            event = reported[index]
            offsets = [abs(event[key] - target[key]) for key in verifier.KEYS]
            assert max(offsets) <= 1
            return sum(offsets), event["category"] == target["category"]

        assert original_index != alternative_index
        assert pair_priority(original_index) == pair_priority(alternative_index)
        return [(reference_index, alternative_index)], reported, expected

    verifier.match = match_with_alternative
    try:
        alternative = verifier.score(windows, predictions, reference, edges)
    finally:
        verifier.match = original_match

    for label, scores in [("Original grader", original), ("Alternative pairing", alternative)]:
        passed = verifier.verdict(scores, thresholds)
        print(f"{label}: {scores['event_f1']:.2%} event F1 ({'pass' if passed else 'fail'})")


if __name__ == "__main__":
    main()
