"""Summarize a fixed-label, validation-only pilot without opening test scores."""
from __future__ import annotations

import argparse
import csv
import json
from pathlib import Path


VARIANTS = (
    ("YOLO26s", "yolo26s", "#217a68"),
    ("YOLO26s + P2", "yolo26_p2_only", "#5aa89a"),
    ("P2 + fusion", "yolo26_p2_fusion_no_edge", "#77b6a9"),
    ("P2 + edge", "yolo26_p2_edge_no_fusion", "#8ac3b9"),
    ("Pallet-YOLO26", "pallet_yolo26", "#d16248"),
    ("YOLOv8s", "yolov8s", "#4477aa"),
    ("YOLOv8s + CSPNeXt approx.", "yolov8_cspnext", "#7b9ac3"),
    ("RF-DETR-Small", "rfdetr_small", "#b38b55"),
)


def summarize(root, output):
    root = Path(root)
    output = Path(output)
    rows = []
    identities = set()
    for name, slug, color in VARIANTS:
        directory = root / "within" / "within" / slug / "seed_42"
        run = json.loads((directory / "run.json").read_text())
        completion = json.loads((directory / "completed.json").read_text())
        metric_file = directory / "val_bootstrap.json"
        if not metric_file.exists():
            metric_file = directory / "val.json"
        result = json.loads(metric_file.read_text())
        if not (directory / "best.pt").exists() or completion["best_checkpoint_sha256"] != result["checkpoint_sha256"]:
            raise ValueError(f"best checkpoint and validation report differ: {slug}")
        if result["independent_test_reference_sha256"] is not None:
            raise ValueError("pilot summary cannot include test scoring")
        identities.add((run["annotation_sha256"], run["protocol_sha256"],
                        run["subset_sha256"], run["seed"],
                        tuple(result["reference_coverage"].items())))
        interval = result.get("bootstrap_ci", {}).get("AP50_95")
        rows.append({"model": name, "slug": slug, "color": color,
                     "AP50": result["AP50"], "AP75": result["AP75"],
                     "AP50_95": result["AP50_95"],
                     "AP50_95_ci_low": interval[0] if interval else None,
                     "AP50_95_ci_high": interval[1] if interval else None,
                     "precision_at_0.25": result["precision"],
                     "recall_at_0.25": result["recall_at_threshold_iou_0.5"],
                     "false_positives_per_frame_at_0.25": result["false_positives_per_frame"],
                     "epochs": completion["completed_epochs"],
                     "checkpoint_sha256": result["checkpoint_sha256"]})
    if len(identities) != 1:
        raise ValueError("pilot models do not share one labels/protocol/subset/seed/coverage identity")
    label_hash, protocol_hash, subset_hash, seed, coverage = next(iter(identities))
    latency_path = output / "trained_latency.json"
    latency = json.loads(latency_path.read_text()) if latency_path.exists() else None
    if latency is not None:
        if latency["protocol_sha256"] != protocol_hash or latency["input_size"] != 640:
            raise ValueError("latency report uses a different protocol or input size")
        for row in rows:
            timing = latency["models"][row["slug"]]
            if timing["checkpoint_sha256"] != row["checkpoint_sha256"]:
                raise ValueError(f"latency used another checkpoint: {row['slug']}")
            row["inference_ms"] = timing["model_and_postprocess"]["median_ms"]
            row["raw_sensor_to_boxes_ms"] = timing["raw_sensor_to_boxes"]["median_ms"]
            row["parameters"] = timing["parameters"]
            row["peak_gpu_mib"] = timing["peak_gpu_mib"]
    compute_path = output / "registered_macs.json"
    compute = json.loads(compute_path.read_text()) if compute_path.exists() else None
    if compute is not None:
        for row in rows:
            estimate = compute["models"][row["slug"]]
            if estimate["checkpoint_sha256"] != row["checkpoint_sha256"]:
                raise ValueError(f"compute estimate used another checkpoint: {row['slug']}")
            row["registered_macs_g"] = estimate["registered_macs"] / 1e9
            if "parameters" in row and row["parameters"] != estimate["parameters"]:
                raise ValueError(f"parameter count differs between measurements: {row['slug']}")
    report = {"scope": "single-seed exploratory validation only; no test model scores",
              "reference": "independently reviewed machine annotations",
              "annotation_sha256": label_hash, "protocol_sha256": protocol_hash,
              "subset_sha256": subset_hash, "seed": seed,
              "coverage": dict(coverage), "models": rows,
              "latency_device": latency["device"] if latency is not None else None,
              "latency_precision": latency["precision"] if latency is not None else None,
              "compute_estimate_method": compute["method"] if compute is not None else None,
              "compute_estimate_limit": compute["limitation"] if compute is not None else None,
              "limitations": ["Only 106 scored validation frames from 40 collection groups.",
                              "Training used 282 scored frames; 318 requested training frames were ignore.",
                              "Group bootstrap intervals overlap; no superiority claim.",
                              "Independent audit of existing training labels found substantial disagreement."]}
    output.mkdir(parents=True, exist_ok=True)
    (output / "pilot_summary.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
    with (output / "pilot_summary.csv").open("w", newline="", encoding="utf-8") as stream:
        writer = csv.DictWriter(stream, fieldnames=[key for key in rows[0] if key != "color"])
        writer.writeheader()
        writer.writerows({key: value for key, value in row.items() if key != "color"} for row in rows)

    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(figsize=(10, 5.4))
    positions = list(range(len(rows)))
    values = [row["AP50_95"] for row in rows]
    error = [[max(0, row["AP50_95"] - row["AP50_95_ci_low"]) if row["AP50_95_ci_low"] is not None else 0
              for row in rows],
             [max(0, row["AP50_95_ci_high"] - row["AP50_95"]) if row["AP50_95_ci_high"] is not None else 0
              for row in rows]]
    ax.barh(positions, values, color=[row["color"] for row in rows], height=.68,
            xerr=error, capsize=3, ecolor="#303030")
    ax.set_yticks(positions, [row["model"] for row in rows])
    ax.invert_yaxis()
    ax.set_xlim(0, max(.56, max((row["AP50_95_ci_high"] or row["AP50_95"])
                              for row in rows) + .05))
    ax.set_xlabel("AP50:95 (95% collection-group bootstrap interval)")
    ax.set_title("Pallet detection pilot: frozen validation reference")
    for index, value in enumerate(values):
        endpoint = rows[index]["AP50_95_ci_high"] or value
        ax.text(endpoint + .007, index, f"{value:.3f}", va="center", fontsize=9)
    ax.grid(axis="x", alpha=.18)
    ax.set_axisbelow(True)
    fig.text(.11, .015, "106 scored frames / 40 groups / seed 42. Machine reference; not final test evidence.",
             fontsize=9, color="#444444")
    fig.tight_layout(rect=(0, .04, 1, 1))
    fig.savefig(output / "pilot_validation_ap.png", dpi=180)
    plt.close(fig)
    if latency is not None:
        fig, ax = plt.subplots(figsize=(9.5, 6.2))
        offsets = ((7, 8), (-120, 9), (7, 10), (7, -16), (7, -23),
                   (7, 10), (7, -19), (7, -15))
        for row, (dx, dy) in zip(rows, offsets):
            ax.scatter(row["inference_ms"], row["AP50_95"], s=95,
                       color=row["color"], edgecolors="white", linewidth=.8, zorder=3)
            ax.annotate(row["model"], (row["inference_ms"], row["AP50_95"]),
                        xytext=(dx, dy), textcoords="offset points", fontsize=8.5)
        ax.set_xlabel("Model + postprocess latency (ms, RTX 3090, FP32, batch 1)")
        ax.set_ylabel("Validation AP50:95")
        ax.set_xlim(5, 21)
        ax.set_ylim(.345, .435)
        ax.grid(alpha=.2)
        ax.set_axisbelow(True)
        ax.set_title("Exploratory accuracy-latency comparison")
        fig.text(.13, .015, "Single seed / 106 scored frames. Confidence intervals overlap; not a final Pareto claim.",
                 fontsize=9, color="#444444")
        fig.tight_layout(rect=(0, .04, 1, 1))
        fig.savefig(output / "pilot_accuracy_latency.png", dpi=180)
        plt.close(fig)
    return report


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--root", default="outputs/pilot600_v5")
    parser.add_argument("--output", default="outputs/pilot600_v5/summary")
    args = parser.parse_args()
    result = summarize(args.root, args.output)
    print(json.dumps({"models": len(result["models"]),
                      "coverage": result["coverage"], "output": args.output}))
