import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { APP_DIR } from "../e2e.config.ts";
// Use the same synthetic provider recording on both commits; only app code differs.
export function installRecording() {
  const agent = join(APP_DIR, "apps/server/scripts/dev-replay-agent.ts");
  writeFileSync(agent, readFileSync(agent, "utf8").replace('"usage_update",', '"usage_update", "compaction_update",'));
  const frames: unknown[] = [{ type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: {} } }];
  const update = (update: unknown) => frames.push({ type: "emit_inbound", afterMs: 50, frame: { kind: "notification", method: "session/update", params: { sessionId: "synthetic-session", update } } });
  for (let i = 1; i <= 3; i++) {
    update({ sessionUpdate: "tool_call", toolCallId: `read-${i}`, kind: "read", title: `Read phase-${i}.txt`, status: "completed", rawInput: { path: `phase-${i}.txt` }, rawOutput: `Phase ${i} verified.` });
    if (i < 3) {
      update({ sessionUpdate: "usage_update", used: i === 1 ? 100000 : 90000, size: 200000 });
      update({ sessionUpdate: "compaction_update", compactionId: `compact-${i}`, status: "in_progress" });
      update({ sessionUpdate: "compaction_update", compactionId: `compact-${i}`, status: "completed", summary: [{ type: "text", text: `Phase ${i} context retained.` }] });
    }
  }
  update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Review complete. Three files checked; context compacted twice during this turn." } });
  frames.push({ type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } } });
  writeFileSync(join(APP_DIR, "apps/server/scripts/dev-replay/showcase_transcript.ndjson"), frames.map(x => JSON.stringify(x)).join("\n") + "\n");
}

