"""Run DuckHunter's actual synthetic detector; no RF/device/API path."""
import hashlib
import importlib.util
import json
from pathlib import Path
import sys


def replay(checkout):
    path = Path(checkout) / "detection" / "detector.py"
    spec = importlib.util.spec_from_file_location("duck_detector", path)
    detector = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(detector)
    train_x, train_y = detector.generate(n_per_class=100, seed=1337)
    test_x, test_y = detector.generate(n_per_class=100, seed=2026)
    model = detector.GaussianNB().fit(train_x, train_y, idx=detector.FUSION_IDX)
    radio = detector.detection_report(test_y, detector.radio_only_predict(test_x))
    fusion = detector.detection_report(test_y, model.predict(test_x))
    assert len(train_x) == len(test_x) == 400
    assert len(train_x[0]) == 8
    assert radio["dark_detect"] == 0.0
    assert fusion["dark_detect"] > radio["dark_detect"]
    return {"source_sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
            "training_seed": 1337, "test_seed": 2026, "samples_each": 400,
            "radio": radio, "fusion": fusion,
            "scope": "Synthetic distributions encoded by this generator; not hardware or field accuracy."}


if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise SystemExit("Usage: python3 duckhunter_detector_replay.py /path/to/duck_hunter")
    print(json.dumps(replay(sys.argv[1]), indent=2))
