"""
Field Test 001 · the preregistered analysis
=========================================

Written 2026-11-05, BEFORE any public answer was opened, and tested only on
synthetic data. The rules it codes are in `edge_inquiry04_field_test.md`:
section 1 (frozen 2026-11-01), Amendment 1 (Prolific, 2026-11-05) and
Amendment 2 (early close and cutoff, 2026-11-05).

Usage (Python 3, standard library only):

    python analyze_001.py <netlify_export.csv> <001-key.json> [--cutoff ISO8601] [--out results_001.json]

What it does, in order:
  1. Reads the Netlify CSV export of the `field-test` form.
  2. Keeps only `version` 3, `phase` "public". (Trial submissions never count.)
  3. Splits on the cutoff: on or before it -> the analysis set; after it ->
     the "late" group (counted and reported, never in the main result).
  4. Applies the exclusions fixed in section 1: incomplete responses; completed
     in under 4 minutes (duration_s < 240); every rating identical.
  5. PRIMARY: for each rater, mean rating of pile A minus mean of pile B, with
     the pairs that rater recognised ("seen" = "yes") dropped. Averaged across
     raters. 95% interval by a two-way bootstrap that resamples raters and
     items (within each pile) together, 10,000 draws, seed 20261105.
     Classified by the preregistered thresholds.
  6. SECONDARY (all reported whatever they show): C - B and A - C; the result
     by lean; with recognised pairs kept; agreement between raters
     (Krippendorff's alpha, interval); by channel (all / without Prolific /
     Prolific only, Amendment 1); counts and exclusion rates by channel.

Decisions fixed here, before the data (also recorded in Amendment 2):
  - "Recognised" means the rater answered "yes". "not sure" is kept.
  - A rater who has no A pair or no B pair left after dropping recognised
    pairs can't give an A - B difference; they are left out of that measure
    and counted in the report.
  - The bootstrap resamples raters, and resamples item IDs within each pile;
    each rater's mean is then weighted by how often each of their items was
    drawn. A draw where a rater ends up with no A or no B item skips that
    rater for that draw.
  - Thresholds (section 1): A - B >= 1.0 with an interval excluding 0 ->
    "supports the Map"; 0.5 to 1.0 with an interval excluding 0 -> "small
    effect"; otherwise -> "against the Map".
"""

import csv
import json
import random
import sys
from collections import Counter, defaultdict
from datetime import datetime, timezone

SEED = 20261105
DRAWS = 10_000
MIN_SECONDS = 240
PER_RATER = 12


# ---------------------------------------------------------------- loading ---

def _col(row, *names):
    """Return the first matching column (case-insensitive), or ''."""
    low = {k.strip().lower(): v for k, v in row.items() if k is not None}
    for n in names:
        if n.lower() in low and low[n.lower()] not in (None, ""):
            return low[n.lower()]
    return ""


def _when(row):
    raw = _col(row, "created_at", "created at", "date", "submitted_at", "timestamp")
    if not raw:
        return None
    raw = raw.strip().replace("Z", "+00:00")
    for fmt in (None, "%Y-%m-%d %H:%M:%S %Z", "%Y-%m-%d %H:%M:%S"):
        try:
            d = datetime.fromisoformat(raw) if fmt is None else datetime.strptime(raw, fmt)
            return d if d.tzinfo else d.replace(tzinfo=timezone.utc)
        except ValueError:
            continue
    return None


def load(csv_path, key_path):
    key = json.load(open(key_path, encoding="utf-8"))
    piles = {iid: it["pile"] for iid, it in key["items"].items()}
    rows = list(csv.DictReader(open(csv_path, encoding="utf-8-sig", newline="")))
    subs = []
    for r in rows:
        resp_raw = _col(r, "responses")
        try:
            resp = json.loads(resp_raw) if resp_raw else []
        except json.JSONDecodeError:
            resp = None
        dur = _col(r, "duration_s")
        subs.append({
            "session": _col(r, "session"),
            "version": _col(r, "version"),
            "phase": _col(r, "phase"),
            "src": _col(r, "src") or "",
            "lean": _col(r, "lean") or "",
            "duration_s": float(dur) if dur not in ("", None) else None,
            "responses": resp,
            "when": _when(r),
        })
    return subs, piles


# ------------------------------------------------------------- filtering ---

def classify(subs, piles, cutoff):
    """Split into counted / late / excluded, with reasons."""
    out = {"not_v3_public": [], "late": [], "excluded": defaultdict(list), "kept": []}
    for s in subs:
        if str(s["version"]).strip() != "3" or s["phase"].strip() != "public":
            out["not_v3_public"].append(s)
            continue
        if cutoff and s["when"] and s["when"] > cutoff:
            out["late"].append(s)
            continue
        r = s["responses"]
        ok = (isinstance(r, list) and len(r) == PER_RATER
              and all(isinstance(x, dict) and x.get("id") in piles
                      and isinstance(x.get("rating"), (int, float)) for x in r))
        if not ok:
            out["excluded"]["incomplete"].append(s)
            continue
        if s["duration_s"] is None or s["duration_s"] < MIN_SECONDS:
            out["excluded"]["under 4 minutes"].append(s)
            continue
        if len({x["rating"] for x in r}) == 1:
            out["excluded"]["every rating identical"].append(s)
            continue
        out["kept"].append(s)
    return out


def channel(s):
    return "prolific" if s["src"] == "prolific" else "other"


# --------------------------------------------------------------- measures ---

def rater_items(s, piles, drop_seen):
    """[(item_id, pile, rating)] for one rater."""
    return [(x["id"], piles[x["id"]], float(x["rating"]))
            for x in s["responses"]
            if not (drop_seen and str(x.get("seen", "")).lower() == "yes")]


def diff_for(items, p, q, weights=None):
    sp = [(w, r) for (i, pl, r) in items if pl == p
          for w in [weights.get(i, 0) if weights is not None else 1] if w]
    sq = [(w, r) for (i, pl, r) in items if pl == q
          for w in [weights.get(i, 0) if weights is not None else 1] if w]
    if not sp or not sq:
        return None
    mp = sum(w * r for w, r in sp) / sum(w for w, _ in sp)
    mq = sum(w * r for w, r in sq) / sum(w for w, _ in sq)
    return mp - mq


def mean_diff(raters, p, q):
    ds = [d for d in (diff_for(it, p, q) for it in raters) if d is not None]
    return (sum(ds) / len(ds), len(ds)) if ds else (None, 0)


def bootstrap(raters, piles, p, q, rng):
    by_pile = defaultdict(list)
    for iid, pl in piles.items():
        by_pile[pl].append(iid)
    stats = []
    n = len(raters)
    if n == 0:
        return None, None
    for _ in range(DRAWS):
        w = Counter()
        for pl, ids in by_pile.items():
            w.update(rng.choice(ids) for _ in ids)
        sample = [raters[rng.randrange(n)] for _ in range(n)]
        ds = [d for d in (diff_for(it, p, q, w) for it in sample) if d is not None]
        if ds:
            stats.append(sum(ds) / len(ds))
    stats.sort()
    lo = stats[int(0.025 * len(stats))]
    hi = stats[min(len(stats) - 1, int(0.975 * len(stats)))]
    return lo, hi


def verdict(est, lo, hi):
    if est is None or lo is None:
        return "not estimable"
    excludes_zero = lo > 0 or hi < 0
    if est >= 1.0 and excludes_zero:
        return "supports the Map"
    if 0.5 <= est < 1.0 and excludes_zero:
        return "small effect"
    return "against the Map"


def krippendorff_interval(raters_all):
    """Krippendorff's alpha (interval) over items x raters."""
    units = defaultdict(list)
    for items in raters_all:
        for iid, _, r in items:
            units[iid].append(r)
    pairable = {u: v for u, v in units.items() if len(v) >= 2}
    vals = [r for v in pairable.values() for r in v]
    n = len(vals)
    if n < 2:
        return None
    do = 0.0
    for v in pairable.values():
        m = len(v)
        do += sum((a - b) ** 2 for i, a in enumerate(v) for j, b in enumerate(v) if i != j) / (m - 1)
    do /= n
    mean = sum(vals) / n
    de = sum((a - mean) ** 2 for a in vals) * 2 / (n - 1)
    return None if de == 0 else 1 - do / de


def measure(set_, piles, p, q, drop_seen, rng):
    raters = [rater_items(s, piles, drop_seen) for s in set_]
    est, k = mean_diff(raters, p, q)
    lo, hi = bootstrap(raters, piles, p, q, rng) if k else (None, None)
    return {"estimate": est, "ci95": [lo, hi], "raters": k, "of": len(set_)}


def pile_means(set_, piles, drop_seen):
    acc = defaultdict(list)
    for s in set_:
        for _, pl, r in rater_items(s, piles, drop_seen):
            acc[pl].append(r)
    return {pl: round(sum(v) / len(v), 3) for pl, v in sorted(acc.items())}


# -------------------------------------------------------------------- run ---

def run(csv_path, key_path, cutoff=None):
    rng = random.Random(SEED)
    subs, piles = load(csv_path, key_path)
    c = classify(subs, piles, cutoff)
    kept = c["kept"]

    def chan_counts(lst):
        return dict(Counter(channel(s) for s in lst))

    public_before = kept + [s for v in c["excluded"].values() for s in v]
    report = {
        "rules": "edge_inquiry04_field_test.md §1, Amendments 1 and 2",
        "seed": SEED, "draws": DRAWS,
        "cutoff": cutoff.isoformat() if cutoff else None,
        "counts": {
            "rows_in_export": len(subs),
            "not_v3_public (trial or other)": len(c["not_v3_public"]),
            "public_v3_by_cutoff": len(public_before),
            "by_channel": chan_counts(public_before),
            "excluded": {k: len(v) for k, v in c["excluded"].items()},
            "excluded_by_channel": {k: chan_counts(v) for k, v in c["excluded"].items()},
            "analysed": len(kept),
            "analysed_by_channel": chan_counts(kept),
            "late_after_cutoff": len(c["late"]),
        },
    }

    prim = measure(kept, piles, "A", "B", True, rng)
    prim["verdict"] = verdict(prim["estimate"], *prim["ci95"])
    report["primary_A_minus_B"] = prim
    report["pile_means_primary"] = pile_means(kept, piles, True)

    sec = {}
    sec["C_minus_B"] = measure(kept, piles, "C", "B", True, rng)
    sec["A_minus_C"] = measure(kept, piles, "A", "C", True, rng)
    sec["A_minus_B_with_recognised_kept"] = measure(kept, piles, "A", "B", False, rng)
    sec["by_lean"] = {ln or "(blank)": measure([s for s in kept if s["lean"] == ln], piles, "A", "B", True, rng)
                      for ln in sorted({s["lean"] for s in kept})}
    sec["by_channel"] = {
        "all": prim,
        "without_prolific": measure([s for s in kept if channel(s) != "prolific"], piles, "A", "B", True, rng),
        "prolific_only": measure([s for s in kept if channel(s) == "prolific"], piles, "A", "B", True, rng),
    }
    sec["agreement_krippendorff_alpha_interval"] = krippendorff_interval(
        [rater_items(s, piles, False) for s in kept])
    if c["late"]:
        late_ok = classify(c["late"], piles, None)["kept"]
        sec["late_group_A_minus_B (not in the main result)"] = measure(late_ok, piles, "A", "B", True, rng)
    report["secondary"] = sec
    return report


def _round(o):
    if isinstance(o, float):
        return round(o, 3)
    if isinstance(o, dict):
        return {k: _round(v) for k, v in o.items()}
    if isinstance(o, list):
        return [_round(v) for v in o]
    return o


if __name__ == "__main__":
    args = sys.argv[1:]
    if len(args) < 2:
        sys.exit(__doc__)
    cutoff, out = None, None
    if "--cutoff" in args:
        cutoff = datetime.fromisoformat(args[args.index("--cutoff") + 1].replace("Z", "+00:00"))
        if not cutoff.tzinfo:
            cutoff = cutoff.replace(tzinfo=timezone.utc)
    if "--out" in args:
        out = args[args.index("--out") + 1]
    rep = _round(run(args[0], args[1], cutoff))
    txt = json.dumps(rep, indent=2, ensure_ascii=False)
    print(txt)
    if out:
        open(out, "w", encoding="utf-8").write(txt + "\n")
