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

test("Nested repository changed file opens in the right panel", WEB, async ({ t3, app, browser, screen }) => {
  const project = await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const parent = yield* fs.makeTempDirectory({ directory: T3_HOME, prefix: "nested-" });
    const cwd = `${parent}/demo`;
    yield* fs.makeDirectory(cwd);
    yield* fs.writeFileString(`${cwd}/.gitignore`, "/api/\n");
    yield* fs.writeFileString(`${cwd}/README.md`, "Demo project\n");
    yield* run("git", ["init", "--quiet", "--initial-branch=main"], { cwd });
    yield* run("git", ["add", "."], { cwd });
    yield* run("git", ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-qm", "Initial demo"], { cwd });
    return cwd;
  }));
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const cwd = `${project}/api`;
    yield* fs.makeDirectory(cwd);
    yield* fs.writeFileString(`${cwd}/api-notes.txt`, "Original API notes\n");
    yield* run("git", ["init", "--quiet", "--initial-branch=main"], { cwd });
    yield* run("git", ["add", "."], { cwd });
    yield* run("git", ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-qm", "Initial API notes"], { cwd });
    yield* fs.writeFileString(`${cwd}/api-notes.txt`, "Updated API notes\n");
  }));
  const added = await runPromise(makeState(T3_HOME).cli(["project", "add", project]));
  const projectId = /Added project (\S+) /.exec(added.stdout)?.[1];
  if (!projectId) throw new Error("Project registration failed");
  t3.trackProject(projectId);
  await t3.open("/");
  await screen.getByRole("button", "Showcase", { exact: false }).first().waitFor();
  const thread = await t3.thread({ project, title: "Review nested repository", messages: ["Start"], model: "showcase" });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  await screen.getByRole("button", /^Diff scope:/).tap();
  await screen.getByRole("menuitemradio", "Uncommitted", { exact: true }).tap();
  await screen.getByRole("button", /^Repository:/).tap();
  await screen.getByRole("menuitemradio", "api", { exact: true }).tap();
  await browser.locator("[data-title]").filter({ hasText: "api-notes.txt" }).click();
  try {
    await expect.poll(() => browser.evaluate(() => {
      const panel = document.querySelector("[data-right-panel-surface-content]");
      for (const host of panel?.querySelectorAll("*") ?? []) {
        const content = host.shadowRoot?.querySelector("[data-content]");
        if (content) return content.textContent ?? "";
      }
      return panel?.textContent ?? "";
    })).toContain("Updated API notes");
  } finally {
    await app.screenshot("nested-repository-file-opened");
  }
});
