"""Reproduce Vora's exploratory treadmill analysis from Zenodo record 13767292.

Install pyreadstat and numpy, download the four .sav files, then run:
    python analyze-treadmill.py /path/to/records /path/to/vora-treadmill.json
Original files are CC BY 4.0, Neudorfer et al., https://doi.org/10.5281/zenodo.13767292.
Only aggregate values are written. The source study aligned the time series.
"""

import argparse
import hashlib
import json
from pathlib import Path

import numpy as np
import pyreadstat

DEVICES = {
    "Garmin Venu 2S": ("Garmin.sav", "4a6db8edbfa630c2cbba613c9ce02bb9"),
    "Polar Vantage M2": ("Polar_VantageM2.sav", "39199e4c2947663144931713564354c8"),
    "Polar Verity Sense": ("Polar_VeritySense.sav", "f3c40afa7d9b62537054885859b08776"),
    "Scosche Rhythm24": ("Scosche.sav", "ad584922ab1a831d11f3c0e6f4f24602"),
}
THRESHOLDS = (8, 10, 12)
MIN_SECONDS = 30
COLUMNS = ["Source.Name", "second", "activities", "ecg", "device"]


def summarize(device, condition, error, people, threshold):
    groups = [error[people == person] for person in np.unique(people)]
    return {
        "device": device,
        "condition": condition,
        "people": len(groups),
        "points": len(error),
        "error": round(float(np.median([np.mean(np.abs(group)) for group in groups])), 2),
        "bias": round(float(np.median([np.mean(group) for group in groups])), 2),
        "outsideFive": round(float(np.median([np.mean(np.abs(group) > 5) for group in groups])) * 100, 1),
        "threshold": threshold,
    }


def analyze(device, file_path):
    data, _ = pyreadstat.read_sav(file_path, usecols=COLUMNS, output_format="dict")
    treadmill = np.asarray(data["activities"]) == "treadmill_running"
    people = np.asarray(data["Source.Name"])[treadmill]
    seconds = np.asarray(data["second"], dtype=float)[treadmill]
    reference = np.asarray(data["ecg"], dtype=float)[treadmill]
    watch = np.asarray(data["device"], dtype=float)[treadmill]
    if len(set(zip(people, seconds))) != len(seconds):
        raise ValueError(f"Duplicate treadmill timestamps for {device}")

    valid_reference = np.isfinite(reference) & (reference > 0)
    paired = valid_reference & np.isfinite(watch) & (watch > 0)
    connected = (
        (np.diff(seconds) == 1)
        & (people[1:] == people[:-1])
        & valid_reference[1:]
        & valid_reference[:-1]
    )
    complete = np.convolve(connected, np.ones(10, dtype=int), "valid") == 10
    change = np.full(len(reference), np.nan)
    change[10:] = np.where(complete, np.abs(reference[10:] - reference[:-10]), np.nan)
    signed = watch - reference
    steady = paired & (change <= 2)
    output = []
    changes = []
    for threshold in THRESHOLDS:
        rapid = paired & (change >= threshold)
        eligible = [
            person for person in np.unique(people[steady])
            if np.sum(steady & (people == person)) >= MIN_SECONDS
            and np.sum(rapid & (people == person)) >= MIN_SECONDS
        ]
        if not eligible:
            continue
        included = np.isin(people, eligible)
        for label, mask in (("Steady", steady & included), ("Rapid change", rapid & included)):
            output.append(summarize(device, label, signed[mask], people[mask], threshold))
        differences = np.array([
            np.mean(np.abs(signed[rapid & (people == person)]))
            - np.mean(np.abs(signed[steady & (people == person)]))
            for person in eligible
        ])
        bootstrap = np.random.default_rng(20261118).choice(
            differences, size=(10_000, len(eligible)), replace=True
        )
        interval = np.quantile(np.median(bootstrap, axis=1), [0.025, 0.975])
        changes.append({
            "device": device,
            "threshold": threshold,
            "people": len(eligible),
            "worse": int(np.sum(differences > 0)),
            "medianChangeBpm": round(float(np.median(differences)), 2),
            "ciLowBpm": round(float(interval[0]), 2),
            "ciHighBpm": round(float(interval[1]), 2),
        })
    return len(set(people[paired])), output, changes


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("records", type=Path)
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    result = {"defaultThreshold": THRESHOLDS[0], "availableByDevice": {}, "rows": [], "pairedChanges": []}
    for device, (filename, checksum) in DEVICES.items():
        file_path = args.records / filename
        with file_path.open("rb") as data:
            if hashlib.file_digest(data, "md5").hexdigest() != checksum:
                raise ValueError(f"Checksum mismatch for {filename}")
        available, rows, changes = analyze(device, file_path)
        result["availableByDevice"][device] = available
        result["rows"].extend(rows)
        result["pairedChanges"].extend(changes)
    args.output.write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")


if __name__ == "__main__":
    main()
