#!/usr/bin/env python3
"""Ten sequential edits, four image models — metrics and web frames.

    python3 scripts/bench10-edit-chains-metrics.py

Reads scripts/bench10/ (raw.json + the saved frames) and writes
  apps/site/public/benchmarks/ten-edits-four-models/run.json   (public copy)
  apps/site/src/data/benchmarks/ten-edits-four-models.json     (build-time copy)
  apps/site/public/benchmarks/ten-edits-four-models/frames.json (frames manifest)
  apps/site/public/benchmarks/ten-edits-four-models/<model>-<step>.webp (720 px)

Adapted from scripts/bench-edit-degradation.py, same five measures per step:
  stepDelta      mean |d| against the PREVIOUS step (0-255)
  driftFromSeed  mean |d| against step 0, the model's OWN seed (0-255)
  sharpness      Laplacian variance relative to the seed; 1.00 = as crisp as the seed
  structureIoU   edge-map overlap with the seed — is it still the same building
  atmosphereTone mean R/G/B of the outer frame band (sky, trees), away from the building
  skyShift       change of mean R/G/B in the top 12% of the frame, a control no edit names

Differences from the three-engine run, all forced by this run's shape:
  - every model made its OWN seed, so each chain is measured against its own
    step 0, never against another model's picture;
  - frames differ in size across models (1280x720 to 1536x864) and MAI-Image
    delivers 1365x768 after a 1360x768 seed. Every frame is therefore resampled
    to its own chain's seed grid (long edge 768 px) before any comparison;
  - cost, latency and delivered size are carried from raw.json untouched.

No number on the page is typed by hand: the page reads the JSON this writes.
"""

import json
import statistics
from pathlib import Path

import numpy as np
from PIL import Image

ROOT = Path(__file__).resolve().parent.parent
SRC = ROOT / "scripts/bench10"
RAW = SRC / "raw.json"
SLUG = "ten-edits-four-models"
OUT = ROOT / "apps/site/public/benchmarks" / SLUG
SRC_DATA = ROOT / "apps/site/src/data/benchmarks"

LONG_EDGE = 768
WEB_WIDTH = 720
WEB_QUALITY = 80

VENDORS = {
    "gpt25flare": "OpenAI",
    "flux3": "Black Forest Labs",
    "seedream5flash": "ByteDance",
    "mai26flash": "Microsoft",
}


def to_grid(im, size=None):
    w, h = im.size
    if size is None:
        s = LONG_EDGE / max(w, h)
        size = (max(1, round(w * s)), max(1, round(h * s)))
    return np.asarray(im.convert("RGB").resize(size, Image.LANCZOS), dtype=np.float64), size


def laplacian_var(a):
    g = a.mean(axis=2)
    h, w = g.shape
    k = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=np.float64)
    out = np.zeros((h - 2, w - 2))
    for dy in range(3):
        for dx in range(3):
            if k[dy, dx]:
                out += k[dy, dx] * g[dy : dy + h - 2, dx : dx + w - 2]
    return float(out.var())


def edge_map(a, pct=90):
    g = a.mean(axis=2)
    gx = g[1:-1, 2:] - g[1:-1, :-2]
    gy = g[2:, 1:-1] - g[:-2, 1:-1]
    return np.hypot(gx, gy) > np.percentile(np.hypot(gx, gy), pct)


def iou(a, b):
    u = np.logical_or(a, b).sum()
    return float(np.logical_and(a, b).sum() / u) if u else 0.0


def margin_tone(a, frac=0.16):
    """Mean R/G/B of the frame's outer band: sky, trees, ground. The building is
    in the middle, so a cladding change does not read as an atmosphere change."""
    h, w = a.shape[:2]
    mh, mw = max(1, int(h * frac)), max(1, int(w * frac))
    mask = np.ones((h, w), bool)
    mask[mh : h - mh, mw : w - mw] = False
    return [round(float(a[:, :, c][mask].mean()), 2) for c in range(3)]


def sky_tone(a, frac=0.12):
    """Mean R/G/B of the top band only: sky, never edited by any step. A control
    that, unlike the margin band, does not include the tree removed on the left."""
    h = a.shape[0]
    band = a[: max(1, int(h * frac))]
    return [round(float(band[:, :, c].mean()), 2) for c in range(3)]


def mad(a, b):
    return float(np.abs(a - b).mean())


def main():
    raw = json.loads(RAW.read_text())
    OUT.mkdir(parents=True, exist_ok=True)
    SRC_DATA.mkdir(parents=True, exist_ok=True)

    edits = [
        {"step": r["step"], "slug": r["slug"], "kind": r["kind"], "zone": r["zone"], "instruct": r["instruct"]}
        for r in raw["records"]
        if r["modelKey"] == raw["models"][0]["key"] and r["step"] > 0
    ]

    models, manifest = [], []
    for m in raw["models"]:
        recs = sorted(
            [r for r in raw["records"] if r["modelKey"] == m["key"] and not r.get("error")],
            key=lambda r: r["step"],
        )
        seed_rec = recs[0]
        seed_im = Image.open(SRC / seed_rec["file"])
        seed, grid = to_grid(seed_im)
        seed_sharp = laplacian_var(seed)
        seed_edges = edge_map(seed)
        seed_tone = margin_tone(seed)
        seed_sky = sky_tone(seed)
        prev = seed
        steps = []
        sizes = set()
        for r in recs:
            im = Image.open(SRC / r["file"])
            sizes.add(im.size)
            cur, _ = to_grid(im, grid)
            tone = margin_tone(cur)
            sky = sky_tone(cur)
            steps.append(
                {
                    "step": r["step"],
                    "slug": r["slug"],
                    "frame": f"{m['key']}-{r['step']:02d}",
                    "latencyMs": r["latencyMs"],
                    "latencySource": r.get("latencySource", "measured"),
                    "costUsd": r["gatewayCostUsd"],
                    "costSource": r.get("costSource", "reported by the endpoint"),
                    "width": r["width"],
                    "height": r["height"],
                    "stepDelta": round(mad(cur, prev), 2),
                    "driftFromSeed": round(mad(cur, seed), 2),
                    "sharpness": round(laplacian_var(cur) / seed_sharp, 4),
                    "structureIoU": round(iou(edge_map(cur), seed_edges), 4),
                    "atmosphereTone": tone,
                    "atmosphereShift": [round(tone[c] - seed_tone[c], 2) for c in range(3)],
                    "skyShift": [round(sky[c] - seed_sky[c], 2) for c in range(3)],
                }
            )
            prev = cur
            # web frame
            w, h = im.size
            web = im.convert("RGB").resize((WEB_WIDTH, round(WEB_WIDTH * h / w)), Image.LANCZOS)
            web.save(OUT / f"{m['key']}-{r['step']:02d}.webp", "WEBP", quality=WEB_QUALITY, method=6)
            manifest.append(
                {"model": m["key"], "step": r["step"], "file": f"{m['key']}-{r['step']:02d}.webp",
                 "width": web.size[0], "height": web.size[1], "source": r["file"]}
            )

        edits_only = [s for s in steps if s["step"] > 0]
        lat = [s["latencyMs"] for s in edits_only]
        cost = [s["costUsd"] for s in edits_only]
        last = steps[-1]
        file_mtime = [s["step"] for s in edits_only if s["latencySource"] != "measured"]
        models.append(
            {
                "key": m["key"],
                "label": m["label"],
                "id": m["id"],
                "vendor": VENDORS[m["key"]],
                "request": m["extra"],
                "stepsCompleted": len(edits_only),
                "seedSize": [seed_im.size[0], seed_im.size[1]],
                "deliveredSizes": sorted([list(s) for s in sizes]),
                "analysisGrid": list(grid),
                "seedCostUsd": steps[0]["costUsd"],
                "seedLatencyMs": steps[0]["latencyMs"],
                "costPerEditUsd": round(sum(cost) / len(cost), 5),
                "totalEditCostUsd": round(sum(cost), 5),
                "latencyMedianMs": int(statistics.median(lat)),
                "latencyTotalMs": int(sum(lat)),
                "latencyFromFileTimestampsSteps": file_mtime,
                "costFlatCatalogSteps": [s["step"] for s in edits_only if s["costSource"] != "reported by the endpoint"],
                "finalSharpness": last["sharpness"],
                "finalStructureIoU": last["structureIoU"],
                "finalDriftFromSeed": last["driftFromSeed"],
                "medianStepDelta": round(statistics.median(s["stepDelta"] for s in edits_only), 2),
                "minSharpness": min(s["sharpness"] for s in edits_only),
                "maxSharpness": max(s["sharpness"] for s in edits_only),
                "atmosphereShiftFinal": last["atmosphereShift"],
                "atmosphereShiftMagnitude": round(float(np.linalg.norm(last["atmosphereShift"])), 2),
                "skyShiftFinal": last["skyShift"],
                "skyShiftMagnitude": round(float(np.linalg.norm(last["skyShift"])), 2),
                "steps": steps,
            }
        )

    result = {
        "runDate": raw["runAt"][:10],
        "runAt": raw["runAt"],
        "aspectRequested": "16:9 (reference chain: 3:2)",
        "seedPrompt": raw["seedPrompt"],
        "keepClause": raw["keepClause"],
        "lightingPolicy": raw["lightingPolicy"],
        "framingPolicy": raw["framingPolicy"],
        "controlRegions": raw["controlRegions"],
        "harness": "scripts/bench10-edit-chain.mjs",
        "seedHarness": "scripts/bench10-seeds.mjs",
        "metricsScript": "scripts/bench10-edit-chains-metrics.py",
        "commonLongEdgePx": LONG_EDGE,
        "webFrameWidthPx": WEB_WIDTH,
        "seedSharpnessReference": 1.0,
        "edits": edits,
        "models": models,
        "disclosures": [
            "Each model generated its own seed once, from the same prompt, in a separate step; the seeds were reused for the chains.",
            "One chain per model, no repeats. Differences between neighbouring figures can be run-to-run noise.",
            "Delivered resolution differs per model; every frame is resampled to its own seed's grid before measuring.",
            "FLUX.3 steps 1-8: the run was interrupted before the raw output was written, so latency for those steps comes from file modification times and cost is the flat per-image price that every other recorded FLUX.3 call carried.",
            "Sharpness, structure overlap and drift are measured against the seed, and the edits legitimately add and remove objects; they are comparable across models, not an absolute quality score.",
        ],
        "notes": raw["notes"],
    }

    payload = json.dumps(result, indent=2, ensure_ascii=False) + "\n"
    (OUT / "run.json").write_text(payload)
    (SRC_DATA / f"{SLUG}.json").write_text(payload)
    (OUT / "frames.json").write_text(json.dumps({"slug": SLUG, "frames": manifest}, indent=2) + "\n")
    print(f"wrote {OUT/'run.json'} + frames.json + {len(manifest)} webp")
    for m in models:
        print(
            f"  {m['label']:<30} {m['stepsCompleted']}/10  sharp {m['finalSharpness']:.3f} "
            f"iou {m['finalStructureIoU']:.3f} drift {m['finalDriftFromSeed']:.1f} "
            f"stepDelta~{m['medianStepDelta']} atm {m['atmosphereShiftMagnitude']} sky {m['skyShiftMagnitude']} "
            f"cost/edit {m['costPerEditUsd']} total {m['totalEditCostUsd']} "
            f"lat med {m['latencyMedianMs']/1000:.1f}s tot {m['latencyTotalMs']/1000:.0f}s"
        )


if __name__ == "__main__":
    main()
