#!/usr/bin/env python3
"""Summarise v3 frontier runs with the unchanged v2 analysis (../v2/code/03_analyse_v2.py)
and print them next to the v2 local models. Writes out/summary_v3.json."""

import importlib.util
import json
from pathlib import Path

HERE = Path(__file__).resolve().parent
V2 = HERE.parent / "v2"
spec = importlib.util.spec_from_file_location("an2", V2 / "code" / "03_analyse_v2.py")
an2 = importlib.util.module_from_spec(spec)
spec.loader.exec_module(an2)

out = {"move_definition": "P_neutral(none, off) - P(cell), in points; positive = moved toward critic (same as v2)",
       "models": {}}
for f in sorted((HERE / "out").glob("results-*.json")):
    doc = json.loads(f.read_text())
    out["models"][doc["model"]] = an2.analyse_model(doc)
(HERE / "out" / "summary_v3.json").write_text(json.dumps(out, indent=1))

v2 = json.loads((V2 / "out" / "summary_v2.json").read_text())["models"]
allm = {**{k: v for k, v in v2.items()}, **out["models"]}
print("| Model | flawed+strong move | sound+weak move | sound+mistaken move | margin (strong vs mistaken) | 95% CI | sound+pressure move |")
print("|---|---|---|---|---|---|---|")
for m, s in allm.items():
    f, so = s["summary_kind"]["flawed"] if "summary_kind" in s else s["flawed"], s["summary_kind"]["sound"] if "summary_kind" in s else s["sound"]
    mg = s["margins"]["v2_mistaken_foil_p_off"]
    print(f"| {m} | {f['move_strong_off']['mean']} | {so['move_weak_off']['mean']} | {so['move_mistaken_off']['mean']} | {mg['diff']} | {mg['ci95']} | {so['move_none_on']['mean']} |")
