#!/usr/bin/env python3
"""Reproduce the project's first-10-second fits from Todd et al.'s raw ZIP.

Usage:
  python3 todd_refit.py /path/to/todd_wogan_catling_ferrocyanide_formation_2024_raw_data.zip todd_refit_results.json

The source ZIP is publisher supporting data for DOI 10.1021/acsearthspacechem.3c00213.
This script checks its SHA-256 before reading it. It doesn't alter the source.
"""
from pathlib import Path
import csv
import hashlib
import io
import json
import math
import re
import statistics
import sys
import zipfile

EXPECTED_SOURCE_SHA256 = "d2a6f9c3c33c393904d31d0932214b1adbd244f8934fd3beef347babbdd78e5f"

if len(sys.argv) not in (2, 3):
    raise SystemExit("usage: python3 todd_refit.py SOURCE.zip [OUTPUT.json]")

source = Path(sys.argv[1])
output_path = Path(sys.argv[2]) if len(sys.argv) == 3 else Path("todd_refit_results.json")
source_bytes = source.read_bytes()
source_sha256 = hashlib.sha256(source_bytes).hexdigest()
assert source_sha256 == EXPECTED_SOURCE_SHA256, (EXPECTED_SOURCE_SHA256, source_sha256)


def fit(x, y):
    xm = statistics.mean(x)
    ym = statistics.mean(y)
    sxx = sum((value - xm) ** 2 for value in x)
    slope = sum((a - xm) * (b - ym) for a, b in zip(x, y)) / sxx
    intercept = ym - slope * xm
    syy = sum((value - ym) ** 2 for value in y)
    residual = sum((value - intercept - slope * point) ** 2 for point, value in zip(x, y))
    return {
        "slope": slope,
        "intercept": intercept,
        "r2": 1 - residual / syy if syy else None,
        "slope_stderr": math.sqrt(residual / (len(x) - 2) / sxx) if len(x) > 2 else None,
    }


records = []
unsupported = []
with zipfile.ZipFile(io.BytesIO(source_bytes)) as archive:
    for name in archive.namelist():
        if not name.endswith(".csv") or "temperature_dependence" in name:
            continue
        iron = re.search(r"_([0-9p.]+)mMFe", name)
        cyanide = re.search(r"_([0-9p.]+)mMCN", name)
        ph = re.search(r"_pH([0-9]+)", name)
        if not all((iron, cyanide, ph)):
            unsupported.append(name)
            continue
        fe_mM = float(iron[1].replace("p", "."))
        total_cyanide_mM = float(cyanide[1].replace("p", "."))
        ph_value = int(ph[1])
        round_value = int(re.search(r"ROUND([0-9]+)", name)[1])
        member = archive.read(name)
        pairs = [
            (float(time) * 60, (float(absorbance) - 0.15298) / 22.6876)
            for time, absorbance in csv.reader(io.StringIO(member.decode()))
        ]
        selected = [(time, value) for time, value in pairs if time <= 10]
        result = fit([row[0] for row in selected], [row[1] for row in selected])
        records.append({
            "member": name,
            "member_sha256": hashlib.sha256(member).hexdigest(),
            "fe_mM": fe_mM,
            "total_cyanide_mM": total_cyanide_mM,
            "pH": ph_value,
            "round": round_value,
            "sample_count": len(selected),
            "t_first_s": pairs[0][0],
            "t_last_s": pairs[-1][0],
            "v_mM_s": result["slope"],
            "first10_r2": result["r2"],
            "final_4min_mM": pairs[-1][1],
        })

grouped = {}
for record in records:
    grouped.setdefault((record["pH"], record["fe_mM"], record["total_cyanide_mM"]), []).append(record)

groups = []
for (ph_value, fe_mM, total_cyanide_mM), rows in sorted(grouped.items()):
    groups.append({
        "pH": ph_value,
        "fe_mM": fe_mM,
        "total_cyanide_mM": total_cyanide_mM,
        "n": len(rows),
        "mean_v_mM_s": statistics.mean(row["v_mM_s"] for row in rows),
        "sd_v_mM_s": statistics.stdev(row["v_mM_s"] for row in rows) if len(rows) > 1 else None,
        "mean_final_4min_mM": statistics.mean(row["final_4min_mM"] for row in rows),
    })

fits = []
for ph_value in (6, 7, 8, 9, 10):
    cyanide_set = [row for row in groups if row["pH"] == ph_value and row["fe_mM"] == 0.1 and row["total_cyanide_mM"] > 0]
    iron_set = [row for row in groups if row["pH"] == ph_value and row["total_cyanide_mM"] == 0.6]
    assert len(cyanide_set) == 4 and len(iron_set) == 5
    assert all(row["n"] == 3 for row in cyanide_set + iron_set)
    cyanide_fit = fit(
        [math.log10(row["total_cyanide_mM"]) for row in cyanide_set],
        [math.log10(row["mean_v_mM_s"]) for row in cyanide_set],
    )
    iron_fit = fit(
        [math.log10(row["fe_mM"]) for row in iron_set],
        [math.log10(row["mean_v_mM_s"]) for row in iron_set],
    )
    iron_power = iron_fit["slope"]
    cyanide_power = cyanide_fit["slope"]
    rate_set = [row for row in groups if row["pH"] == ph_value and row["total_cyanide_mM"] > 0]
    x = [row["fe_mM"] ** iron_power * row["total_cyanide_mM"] ** cyanide_power for row in rate_set]
    y = [row["mean_v_mM_s"] for row in rate_set]
    free_intercept = fit(x, y)
    zero_intercept = sum(a * b for a, b in zip(x, y)) / sum(a * a for a in x)
    fits.append({
        "pH": ph_value,
        "m": iron_fit,
        "n": cyanide_fit,
        "k_fit_free_intercept": free_intercept,
        "k_fit_zero_intercept": zero_intercept,
        "n_combinations": len(rate_set),
    })

output = {
    "source_file": source.name,
    "source_doi": "10.1021/acsearthspacechem.3c00213",
    "source_sha256": source_sha256,
    "method": "Raw column 1 is interpreted as minutes and multiplied by 60. The first 100 observations at no more than 10 seconds are used. The script applies ordinary unweighted linear regression to (A217 - 0.15298) / 22.6876 versus seconds, averages rates by experimental combination, and fits log10 mean rate versus log10 concentration for m and n. Both free-intercept and through-zero k fits are retained. This is a project reconstruction, not a corrected publisher table.",
    "records": records,
    "groups": groups,
    "fits": fits,
    "unsupported": unsupported,
}
output_path.write_text(json.dumps(output, indent=2) + "\n")
print(json.dumps({"records": len(records), "fits": len(fits), "output": str(output_path)}))
