import { writeFileSync } from "node:fs";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("File mention stash, draft and initial title previews preserve the message", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("demo", {"src/foo.ts": "export {};\n"});
  const thread = await t3.thread({project, title:"Long work", messages:["Show me around"], model:"showcase"});
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("Review @foo");
  await screen.getByRole("option", "foo.ts", {exact:false}).tap();
  await browser.keyboard.press("ControlOrMeta+s");
  await screen.getByRole("button", "Stashed prompts: 1. Open stash.").tap();
  const stash = screen.getByRole("button", "Restore stashed prompt:", {exact:false});
  const stashText = await stash.textContent();
  await app.screenshot("stash-preview");
  const storedStashPrompt = await browser.evaluate(() => JSON.parse(localStorage.getItem("t3code:prompt-stash:v2") ?? "{}").state.entries[0].prompt);
  await stash.tap();
  await app.screenshot("restored-file-chip");
  await browser.keyboard.press("ControlOrMeta+Enter");
  await screen.getByText("Working for", {exact:false}).waitFor();
  const stored = await runPromise(makeState(T3_HOME).cli(["thread", "show", thread.threadId, "--json"]));
  const messages = JSON.parse(stored.stdout).messages.filter((m:any)=>m.role === "user").map((m:any)=>m.text);
  writeFileSync(`/tmp/factory-pr-40924-evidence-2/markdown-preservation-${Date.now()}.json`, JSON.stringify({source:"Real browser stash storage and real server thread show after restoring and sending the @-selected file mention", storedStashPrompt, messages}, null, 2));
  console.log("SENT_MARKDOWN", messages);
  expect(storedStashPrompt.trim()).toBe("Review [foo.ts](src/foo.ts)");
  expect(messages).toContain("Review [foo.ts](src/foo.ts)");
  await screen.getByText("Working for", {exact:false}).waitFor({state:"hidden"});
  await screen.getByRole("button", "New thread", {exact:true}).tap();
  await composer.pressSequentially("Review @foo");
  await screen.getByRole("option", "foo.ts", {exact:false}).tap();
  await t3.open(thread);
  const draft = screen.getByTestId("sidebar-draft-row");
  const draftText = await draft.textContent();
  await app.screenshot("sidebar-draft-preview");
  await draft.tap();
  await browser.keyboard.press("ControlOrMeta+Enter");
  const listed = await runPromise(makeState(T3_HOME).cli(["thread", "list", "--project", project, "--json"]));
  const created = JSON.parse(listed.stdout).find((entry:any)=>entry.threadId !== thread.threadId);
  await t3.open({threadId:created.threadId});
  await screen.getByRole("textbox").waitFor();
  await app.screenshot("initial-thread-title");
  const title = created.title;
  expect(stashText).toContain("Review foo.ts");
  expect(draftText).toContain("Review foo.ts");
  expect(title).toContain("Review foo.ts");
});
