"""Summarize released wearable/reference pairs by activity and participant.

Install NumPy and pyreadstat and download the four SAVs from Zenodo 13767292
and hr_dataset.csv from Zenodo 18992173 (each CC BY 4.0). Run:
    python analyze-recordings.py /path/to/recordings /path/to/recordings-analysis.json
Only aggregate statistics are written.
"""

import argparse
import csv
import hashlib
import json
from pathlib import Path

import numpy as np
import pyreadstat


DAY_FILES = {
    "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"),
}
DAY_CONDITIONS = {
    "Free living": "free_living",
    "Treadmill running": "treadmill_running",
    "Daily tasks (lab)": "ADL_day1",
    "Light cycling": "ergometer_cycling_light_day1",
    "Moderate cycling": "ergometer_cycling_moderate_day1",
    "Vigorous cycling": "ergometer_cycling_vigorous_day1",
}
SWIM_CONDITIONS = {
    "Dry rest": "dry_rest", "Running": "running",
    "Rest in water": "rest_in_water", "Swimming": "swimming",
}
MIN_PAIRS = 30
MIN_IN_MATCHED_BAND = 5
MIN_BAND_PEOPLE = 5
BOOTSTRAP_DRAWS = 10_000


def checked_file(directory, filename, checksum):
    path = directory / filename
    with path.open("rb") as handle:
        if hashlib.file_digest(handle, "md5").hexdigest() != checksum:
            raise ValueError(f"Checksum mismatch for {filename}")
    return path


def valid(values):
    return np.isfinite(values) & (values > 0)


def number(value):
    return round(float(value), 2)


def distribution(person_values, draws):
    quartiles = np.quantile(person_values, [0.25, 0.75])
    interval = np.quantile(np.median(person_values[draws], axis=1), [0.025, 0.975])
    return {
        "middle50": [number(value) for value in quartiles],
        "medianInterval95": [number(value) for value in interval],
    }


def resample(people):
    return np.random.default_rng(20261118).integers(people, size=(BOOTSTRAP_DRAWS, people))


def summarize(device, condition, people, reference, watch):
    reference_ok = valid(reference)
    paired = reference_ok & valid(watch)
    groups = []
    for person in np.unique(people):
        own = person == people
        usable = paired & own
        if usable.sum() < MIN_PAIRS:
            continue
        actual = reference[usable]
        error = watch[usable] - actual
        groups.append((len(error), np.abs(error).mean(), error.mean(),
                       (np.abs(error) / actual).mean() * 100,
                       (error / actual).mean() * 100,
                       (np.abs(error) > 5).mean() * 100))
    if not groups:
        return None
    counts, mae, bias, mape, percent_bias, outside = np.array(groups).T
    draws = resample(len(groups))
    return {
        "device": device, "condition": condition, "people": len(groups),
        "points": int(counts.sum()), "error": number(np.median(mae)),
        "bias": number(np.median(bias)), "percentError": number(np.median(mape)),
        "percentBias": number(np.median(percent_bias)), "outsideFive": number(np.median(outside)),
        "bpmSpread": distribution(mae, draws), "percentSpread": distribution(mape, draws),
    }


def rate_bands(study, device, condition, people, reference, watch):
    rows = []
    for lower, upper in ((0, 80), (80, 110), (110, 140), (140, np.inf)):
        mask = (reference >= lower) & (reference < upper) & valid(reference) & valid(watch)
        participants = people[mask]
        signed = watch[mask] - reference[mask]
        per_person = [signed[participants == person] for person in np.unique(participants)]
        usable = [errors for errors in per_person if len(errors) >= MIN_PAIRS]
        if len(usable) < MIN_BAND_PEOPLE:
            continue
        # A person with a longer recording still contributes one summary.
        tail = [np.quantile(np.abs(errors), 0.9) for errors in usable]
        rows.append({
            "study": study, "device": device, "condition": condition, "lowerBpm": lower,
            "upperBpmExclusive": None if np.isinf(upper) else upper,
            "people": len(usable), "points": sum(len(errors) for errors in usable),
            "typicalMeanBiasBpm": number(np.median([errors.mean() for errors in usable])),
            "typicalMaeBpm": number(np.median([np.abs(errors).mean() for errors in usable])),
            "typicalP90AbsoluteErrorBpm": number(np.median(tail)),
        })
    return rows


def matched_treadmill(device, people, seconds, reference, watch):
    if len(set(zip(people, seconds))) != len(seconds):
        raise ValueError(f"Duplicate treadmill timestamps for {device}")
    ref_ok = valid(reference)
    connected = (np.diff(seconds) == 1) & (people[1:] == people[:-1]) & ref_ok[1:] & ref_ok[:-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)
    paired = ref_ok & valid(watch)
    steady = paired & (change <= 2)
    rapid = paired & (change >= 8)
    absolute = np.abs(watch - reference)
    percent = np.full(len(reference), np.nan)
    np.divide(100 * absolute, reference, out=percent, where=paired)
    bands = np.floor(reference / 10)
    metrics = []
    unadjusted = []
    for person in np.unique(people):
        own = people == person
        slow = own & steady
        fast = own & rapid
        if min(slow.sum(), fast.sum()) < MIN_PAIRS:
            continue
        rows = []
        for band in np.intersect1d(bands[slow], bands[fast]):
            a = slow & (bands == band)
            b = fast & (bands == band)
            overlap = min(a.sum(), b.sum())
            if overlap >= MIN_IN_MATCHED_BAND:
                rows.append((overlap, absolute[a].mean(), absolute[b].mean(), percent[a].mean(), percent[b].mean()))
        if rows:
            weights, *estimates = np.asarray(rows).T
            if weights.sum() >= MIN_PAIRS:
                metrics.append(np.average(estimates, weights=weights, axis=1))
                unadjusted.append(absolute[fast].mean() - absolute[slow].mean())
    steady_bpm, rapid_bpm, steady_percent, rapid_percent = np.asarray(metrics).T
    draws = resample(len(metrics))
    bpm_delta = rapid_bpm - steady_bpm
    percent_delta = rapid_percent - steady_percent
    return {
        "device": device, "people": len(metrics), "worse": int((bpm_delta > 0).sum()),
        "steadyBpm": number(np.median(steady_bpm)), "rapidBpm": number(np.median(rapid_bpm)),
        "steadyPercent": number(np.median(steady_percent)), "rapidPercent": number(np.median(rapid_percent)),
        "unadjustedChangeBpm": number(np.median(unadjusted)),
        "changeBpm": number(np.median(bpm_delta)), "changePercentPoints": number(np.median(percent_delta)),
        "bpmSpread": distribution(bpm_delta, draws), "percentSpread": distribution(percent_delta, draws),
    }


def analyze_day(directory):
    rows = []
    matched = []
    bands = []
    for device, (filename, checksum) in DAY_FILES.items():
        path = checked_file(directory, filename, checksum)
        data, _ = pyreadstat.read_sav(path, usecols=["Source.Name", "second", "activities", "ecg", "device"], output_format="dict")
        activity = np.asarray(data["activities"])
        people = np.asarray(data["Source.Name"])
        seconds = np.asarray(data["second"], dtype=float)
        reference = np.asarray(data["ecg"], dtype=float)
        watch = np.asarray(data["device"], dtype=float)
        del data
        for label, code in DAY_CONDITIONS.items():
            mask = activity == code
            values = people[mask], reference[mask], watch[mask]
            row = summarize(device, label, *values)
            if row is not None:
                rows.append({"study": "all-day", **row})
                bands.extend(rate_bands("all-day", device, label, *values))
            if code == "treadmill_running":
                matched.append(matched_treadmill(device, people[mask], seconds[mask], reference[mask], watch[mask]))
    return rows, matched, bands


def analyze_swim(directory):
    path = checked_file(directory, "hr_dataset.csv", "5ef843ff2fe44a164d6f1d01d9f8db6a")
    with path.open(encoding="utf-8-sig", newline="") as handle:
        data = list(csv.DictReader(handle))
    people = np.array([row["subject_id"] for row in data])
    activity = np.array([row["trial_label"] for row in data])
    reference = np.array([float(row["hr_polar_h10_bpm"]) for row in data])
    rows = []
    bands = []
    for device, column in (("Garmin Venu Sq", "hr_garmin_bpm"), ("Polar Vantage V2", "hr_polar_watch_bpm")):
        watch = np.array([float(row[column]) for row in data])
        for label, code in SWIM_CONDITIONS.items():
            mask = activity == code
            values = people[mask], reference[mask], watch[mask]
            summary = summarize(device, label, *values)
            rows.append({"study": "swimmer", **summary})
            bands.extend(rate_bands("swimmer", device, label, *values))
    return rows, len(data), bands


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("records", type=Path)
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    day, matched, day_bands = analyze_day(args.records)
    swim, shared, swim_bands = analyze_swim(args.records)
    result = {
        "cohorts": [
            {"study": "all-day", "source": "https://doi.org/10.5281/zenodo.13767292", "peopleEnrolled": 32},
            {"study": "swimmer", "source": "https://doi.org/10.5281/zenodo.18992173", "peopleEnrolled": 10, "sharedTimestampsInRelease": shared},
        ],
        "pairedReadings": sum(row["points"] for row in day + swim),
        "minimumPairsPerPersonCondition": MIN_PAIRS,
        "bootstrapDraws": BOOTSTRAP_DRAWS,
        "rows": day + swim,
        "matchedTreadmill": matched,
        "referenceRateBands": day_bands + swim_bands,
    }
    args.output.write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")


if __name__ == "__main__":
    main()
