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

test("PowerPoint notes exclude notes-page decorations", WEB, async ({t3,app,screen,browser}) => {
  const project = await t3.project("Harbor briefing");
  const thread = await t3.thread({project,title:"PowerPoint speaker notes",messages:["Prepare to inspect the attached harbor briefing."]});
  await t3.open(thread);
  await screen.getByRole("button", "Attach files").waitFor({timeout:120_000});
  const bytes = await readFile(new URL("./harbor-briefing.pptx", import.meta.url));
  const uploaded = browser.waitForResponse(/\/api\/attachments\/upload\//);
  await browser.evaluate((b64) => {
    const input = document.querySelector('input[type="file"]') as HTMLInputElement;
    const transfer = new DataTransfer();
    transfer.items.add(new File([Uint8Array.from(atob(b64), c=>c.charCodeAt(0))],"harbor-briefing.pptx", {type:"application/vnd.openxmlformats-officedocument.presentationml.presentation"}));
    input.files = transfer.files; input.dispatchEvent(new Event("change",{bubbles:true}));
  }, bytes.toString("base64"));
  await uploaded;
  const dir = join(T3_HOME,"userdata","attachments");
  const file = (await readdir(dir)).find(f=>f.endsWith(".pptx.txt"));
  if (!file) throw new Error("Upload did not create its extracted-text sidecar");
  const text = await readFile(join(dir,file),"utf8");
  await copyFile(join(dir,file),join(project,"extracted-speaker-notes.txt"));
  await screen.getByText("harbor-briefing.pptx", {exact:false}).first().waitFor();
  await app.screenshot("uploaded-deck");
  await browser.keyboard.press("Control+p");
  await screen.getByRole("combobox").fill("extracted-speaker-notes");
  await screen.getByText("extracted-speaker-notes.txt").first().tap();
  await screen.getByRole("button", "Maximize panel").tap();
  await screen.getByText("Remind the room about the tide schedule change.", {exact:false}).first().waitFor({timeout:60_000});
  await app.screenshot("extracted-speaker-notes");
  expect(text).toContain("Remind the room about the tide schedule change.");
  expect(text).toContain("Visible slide body text");
  expect(text).toContain("Second visible slide body");
  expect(text).not.toContain("Harbor briefing header");
  expect(text).not.toContain("11/21/2026");
  expect(text).not.toContain("Confidential notes footer");
  expect(text).not.toContain("Slide image placeholder text");
  expect(text.split("Notes:").length-1).toBe(1);
});
