/** The episode browser. The v4 benchmark first: every model's run on every three-day window as a row with its C, R, P,
* cost and whether it made a valid submission, filtered by model, side of the training cutoffs, front and battery and
* sorted by any column. Then the development runs: each version's eval pack as a grid of days (or three-day windows)
* by battery, every cell one model's replay, a few picks first. */
import { esc, num, pc, shortSpan, usd } from "./fmt.js";
const ORDER = ["sol", "luna", "qwen"];
const WX = { "freezing rain": "Freezing rain", "snow and sleet": "Snow and sleet", thunderstorms: "Thunderstorms", "hard freeze": "Hard freeze", "extreme heat": "Extreme heat", rain: "Rain" };
const SORTS = { window: 1, C: -1, R: -1, P: -1, usd: -1 };
const WORDS = ["none", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen"];
const $ = (id) => document.getElementById(id);
export function buildSheet(C, { onPick }) {
const box = $("sheet"), B = C.versions.find((x) => x.role === "bench"), dev = C.versions.filter((x) => x !== B);
const st = { v: null, m: "sol", key: null, opener: null, f: { m: "", split: "", front: "", site: "" }, sort: "window", dir: 1 };
const runs = Object.entries(C.episodes).filter(([, e]) => e.version === "bench");
$("sheet-sub").textContent = `Every recorded run, each on the battery site in 3D beside its desk: the v4 benchmark's ${runs.length}, then ${Object.keys(C.episodes).length - runs.length} development runs on the desk's own runner.`;
function render() {
const V = C.versions.find((x) => x.version === st.v) || C.versions[0];
const name = (x) => x.label || `${x.version} ${x.name}`;
const size = (x) => (x.role === "check" ? `${x.check_of}'s ${x.tasks.length} battery-days, re-cleared` : `${x.tasks.length} ${x.days_per_task > 1 ? `${x.days_per_task}-day windows` : "battery-days"}`);
const tab = (x) => (x === B ? ``
: ``);
$("vtabs").innerHTML = `${B ? tab(B) : ""}Development runs${dev.map(tab).join("")}`;
if (V === B) return bench();
$("adds").innerHTML = `${esc(name(V))}, ${esc(size(V))}, on the desk's own runner. ${V.role === "check" ? `A price-impact check, not a step on the ladder: ${V.check_of}'s battery-days again, each day-ahead market re-cleared with the desk's own offers in ERCOT's offer stack for the day.` : `${esc(V.adds[0].toUpperCase() + V.adds.slice(1))}.`}`;
if (!V.models.includes(st.m)) st.m = V.models[0];
const picks = C.featured.filter((f) => C.episodes[f.key]);
$("ctl").innerHTML = `
${picks.map((f) => {
const e = C.episodes[f.key], t = C.tasks[e.task];
return ``;
}).join("")}
${sites.map((s) => `
${esc(s.name)}${esc(s.region)}, ${esc(s.size)}
`).join("")}
${days.map(row).join("")}
`;
}
function bench() {
const R = B.roster, f = st.f, fronts = Object.fromEntries(B.fronts.map((x) => [x.front, x]));
const sites = [...new Map(B.tasks.map((k) => [C.tasks[k].site, C.tasks[k]])).values()].sort((a, b) => a.site.localeCompare(b.site));
const span = (x) => shortSpan([x.front, new Date(Date.parse(`${x.front}T12:00:00Z`) + (B.days_per_task - 1) * 864e5).toISOString().slice(0, 10)]);
const n = B.models.length, word = WORDS[n] || String(n);
$("adds").innerHTML = `${word[0].toUpperCase()}${word.slice(1)} models, each in its native agent harness at high effort, trade a real battery for three days at a time: ${B.fronts.length} real Texas weather fronts × ${sites.length} batteries, one run per model per window. C is the share of the hindsight optimum's profit over idle, R the contract's reward and P the settled ERCOT statement; the cost is the graded run's list-price tokens${Object.values(R).some((m) => m.basis === "gpu_time") ? ", or the open model's GPU time" : ""}.`;
const opt = (v, cur, label) => ``;
$("ctl").innerHTML = `
${shortSpan(t.dates)}${esc(fronts[e.front].kind)}${B.names[e.front] ? `, ${esc(B.names[e.front])}` : ""}${esc(t.site)}${esc(t.region)}${esc(m.name)}${esc(m.harness)}Cappedstopped by the ${esc(e.why.replace(/, final$/, ""))} after $${e.usd.toFixed(2)}; final and unscored, never imputed
`;
return ``;
};
$("grid").innerHTML = `
${scored.length} run${scored.length === 1 ? "" : "s"}${mean == null ? "" : ` · mean C ${pc(mean)}`}${invalid ? ` · ${invalid} without a valid submission` : ""}${off.length ? ` · ${off.length} capped window${off.length === 1 ? "" : "s"}, unscored` : ""}${rows.length ? "" : " · no run matches these filters"}