/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { runPromise, test, WEB } from "./t3.ts";

test("Upward and downward diff comments both sit below line 10", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Diff review", { "sample.ts": "// Original file\n" });
  await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(`${project}/sample.ts`, Array.from({ length: 16 }, (_, i) => `export const value${i + 1} = ${i + 1};`).join("\n") + "\n")));
  const thread = await t3.thread({ project, title: "Review sample changes", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: false }).waitFor();
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
  await screen.getByRole("button", "Expand sample.ts", { exact: true }).tap();
  const points = () => browser.evaluate(() => {
    const roots = Array.from(document.querySelectorAll("*")).flatMap(el => el.shadowRoot ? [el.shadowRoot] : []);
    return Array.from({ length: 6 }, (_, i) => {
      const line = roots.flatMap(root => Array.from(root.querySelectorAll<HTMLElement>(`[data-column-number="${i + 5}"]`))).at(-1);
      if (!line) throw new Error(`Missing line ${i + 5}`);
      const r = line.getBoundingClientRect();
      return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
    });
  });
  const belowTen = () => browser.evaluate(() => {
    const roots = Array.from(document.querySelectorAll("*")).flatMap(el => el.shadowRoot ? [el.shadowRoot] : []);
    const ten = roots.flatMap(root => Array.from(root.querySelectorAll<HTMLElement>('[data-line="10"]'))).at(-1)!;
    const comments = Array.from(document.querySelectorAll<HTMLElement>('[data-diff-comment-annotation]'));
    return comments.at(-1)!.getBoundingClientRect().top >= ten.getBoundingClientRect().bottom - 1;
  });
  const results: boolean[] = [];
  for (const direction of ["upward", "downward"]) {
    const p = await points();
    const ordered = direction === "upward" ? [...p].reverse() : p;
    await browser.mouse.move(ordered[0]!.x, ordered[0]!.y);
    const plus = await browser.evaluate(() => {
      for (const el of document.querySelectorAll("*")) {
        const button = el.shadowRoot?.querySelector('[data-gutter-utility-slot] button');
        if (button) { const r = button.getBoundingClientRect(); return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; }
      }
      throw new Error("Missing gutter plus");
    });
    await browser.mouse.move(plus.x, plus.y);
    await browser.mouse.down();
    for (const point of ordered) await browser.mouse.move(point.x, point.y);
    await browser.mouse.up();
    await screen.getByRole("textbox", "Comment on lines +5 to +10", { exact: true }).waitFor();
    await browser.mouse.move(1100, 90);
    await app.screenshot(`${direction}-draft`);
    results.push(await belowTen());
    await screen.getByRole("textbox", "Comment on lines +5 to +10", { exact: true }).fill(`${direction} selection: review lines 5 through 10`);
    await screen.getByRole("button", "Comment", { exact: true }).tap();
    await screen.getByText(`${direction} selection: review lines 5 through 10`, { exact: true }).waitFor();
    await app.screenshot(`${direction}-submitted`);
    results.push(await belowTen());
  }
  await expect(results).toEqual([true, true, true, true]);
});
