#!/usr/bin/env python3
"""Print average metrics from eval.json and compare with previous run."""

import json
from datetime import datetime
from pathlib import Path


def mean(values):
    return sum(values) / len(values) if values else None


def parse_last_row(history_path: Path):
    if not history_path.exists():
        return None
    rows = history_path.read_text(encoding="utf-8").strip().splitlines()
    if len(rows) < 2:
        return None
    for line in reversed(rows[1:]):
        parts = line.split(",")
        if len(parts) < 4:
            continue
        try:
            return {
                "avg_ks": float(parts[1]),
                "avg_jsd": float(parts[2]),
                "avg_lag1_diff": float(parts[3]),
            }
        except Exception:
            continue
    return None


def main():
    base_dir = Path(__file__).resolve().parent
    eval_path = base_dir / "results" / "eval.json"
    if not eval_path.exists():
        raise SystemExit(f"missing eval.json: {eval_path}")

    obj = json.loads(eval_path.read_text(encoding="utf-8"))
    ks = list(obj.get("continuous_ks", {}).values())
    jsd = list(obj.get("discrete_jsd", {}).values())
    lag = list(obj.get("continuous_lag1_diff", {}).values())

    avg_ks = mean(ks)
    avg_jsd = mean(jsd)
    avg_lag1 = mean(lag)

    history_path = base_dir / "results" / "metrics_history.csv"
    prev = parse_last_row(history_path)

    if not history_path.exists():
        history_path.write_text("timestamp,avg_ks,avg_jsd,avg_lag1_diff\n", encoding="utf-8")
    with history_path.open("a", encoding="utf-8") as f:
        f.write(f"{datetime.utcnow().isoformat()},{avg_ks},{avg_jsd},{avg_lag1}\n")

    print("avg_ks", avg_ks)
    print("avg_jsd", avg_jsd)
    print("avg_lag1_diff", avg_lag1)

    if prev is not None:
        print("delta_avg_ks", avg_ks - prev["avg_ks"])
        print("delta_avg_jsd", avg_jsd - prev["avg_jsd"])
        print("delta_avg_lag1_diff", avg_lag1 - prev["avg_lag1_diff"])


if __name__ == "__main__":
    main()
