"""Analyze an explicitly synthetic learning exercise, not a real study."""
import csv
import sys
from collections import defaultdict
from pathlib import Path


def summarize(path):
    groups = defaultdict(list)
    seen = set()
    with Path(path).open(newline="", encoding="utf-8-sig") as handle:
        reader = csv.DictReader(handle)
        expected = ["participant_id", "condition", "comprehension_score_out_of_10"]
        if reader.fieldnames != expected:
            raise ValueError("Unexpected columns; consult the codebook.")
        for row in reader:
            ident = row["participant_id"].strip()
            condition = row["condition"].strip()
            if not ident or ident in seen:
                raise ValueError("Missing or duplicate ID.")
            if condition not in {"A", "B"}:
                raise ValueError("Unexpected condition.")
            score = int(row["comprehension_score_out_of_10"])
            if not 0 <= score <= 10:
                raise ValueError("Score outside 0 through 10.")
            seen.add(ident)
            groups[condition].append(score)
    if not all(groups[k] for k in ("A", "B")):
        raise ValueError("Both groups need observations.")
    return {k: (len(v), sum(v), sum(v)/len(v)) for k, v in sorted(groups.items())}


if __name__ == "__main__":
    result = summarize(sys.argv[1] if len(sys.argv) > 1 else "research-careers-practice.csv")
    print("Synthetic learning data only; no real study or inferential test.")
    for group, (n, total, mean) in result.items():
        print(f"{group}: n={n}, sum={total}, mean={mean:.2f}")
    print(f"B minus A mean={result['B'][2]-result['A'][2]:.2f} points")
