import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { APP_DIR } from "../e2e.config.ts";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Pending answers submit while providers are disabled", WEB, async ({ t3, app, screen, browser }) => {
  const fixture = join(APP_DIR, "apps/server/src/orchestration-v2/testkit/fixtures/acp_elicitation/registry_transcript.ndjson");
  const original = await readFile(fixture, "utf8");
  const recording = original.split("\n").filter(Boolean).map((line) => {
    const frame = JSON.parse(line);
    if (frame.label === "elicitation.request") delete frame.frame.params.requestedSchema.properties.schema_vs_ui_flexibility.enum;
    return JSON.stringify(frame);
  }).join("\n") + "\n";
  const agent = join(APP_DIR, "apps/server/scripts/dev-replay-agent.ts");
  const agentOriginal = await readFile(agent, "utf8");
  const custom = join(APP_DIR, "apps/server/scripts/dev-replay/custom-question.ndjson");
  await writeFile(custom, recording);
  await writeFile(agent, agentOriginal.replace('${FIXTURES}/acp_elicitation/registry_transcript.ndjson', './dev-replay/custom-question.ndjson'));
  const project = await t3.project("Question demo");
  await t3.thread({ project, title: "Enter answer", messages: ["Ask a question"], model: "question" });
  await t3.thread({ project, title: "Button answer", messages: ["Ask a question"], model: "question" });
  await writeFile(agent, agentOriginal);
  await t3.open();
  await screen.getByRole("button", "Settings", { exact: true }).waitFor();
  await app.open("/settings/providers");
  const replayToggle = screen.getByRole("switch", "Enable Replay (dev)");
  await screen.getByRole("button", "Select Replay (dev)").tap();
  await replayToggle.tap();
  await browser.locator('[role="switch"][aria-label="Enable Replay (dev)"][aria-checked="false"]').waitFor();
  await browser.keyboard.press("Escape");
  await screen.getByRole("dialog", "Settings").waitFor({ state: "hidden" });

  await screen.getByRole("button", "Enter answer, Input, Question demo").tap();
  await screen.getByRole("textbox").fill("Prefer a simple and flexible interface.");
  await app.screenshot("enter-ready");
  await screen.getByRole("textbox").press("Enter");
  await expect.soft(screen.getByText("plan questions fixture complete", { exact: false })).toBeVisible({ timeout: 6000 });
  await app.screenshot("enter-result");

  await screen.getByRole("button", "Button answer, Input, Question demo").tap();
  await screen.getByRole("textbox").fill("Prefer a simple and flexible interface.");
  const submit = screen.getByRole("button", "Submit", { exact: true });
  await app.screenshot("submit-ready");
  await expect.soft(submit).toBeEnabled({ timeout: 1000 });
  const box = await submit.boundingBox();
  if (!box) throw new Error("Submit button has no box");
  await screen.tapAt({ x: box.x + box.width / 2, y: box.y + box.height / 2 });
  await expect.soft(screen.getByText("plan questions fixture complete", { exact: false })).toBeVisible({ timeout: 6000 });
  await app.screenshot("submit-result");
});
