import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB, rpcClient, runPromise } from "./t3.ts";

test("Saving Started by the first run discards the old scheduled-task thread", WEB, async ({ t3, app, screen, browser }) => {
  const completedRun = (count: number) => runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    return yield* client["scheduledTasks.subscribe"]({}).pipe(
      Stream.filter(({ tasks }) => tasks.some((task) => task.runCount >= count && task.lastRunStatus === "succeeded")),
      Stream.runHead,
    );
  }).pipe(Effect.scoped));
  await t3.project("Scheduled task demo");
  await t3.open("/settings/scheduled-tasks");
  await screen.getByRole("button", "New task").tap();
  await screen.getByLabel("Name", { exact: true }).fill("Repro 39630");
  await browser.locator("#scheduled-task-destination").tap();
  await screen.getByRole("option", "One thread, continuing the conversation").tap();
  await browser.locator("#scheduled-task-workspace").tap();
  await screen.getByRole("option", "Use the project checkout").tap();
  await screen.getByLabel("Prompt", { exact: true }).fill("Describe this project briefly.");
  await browser.locator('[data-chat-provider-model-picker="true"]').tap();
  await screen.getByText("Showcase", { exact: true }).tap();
  await screen.getByRole("button", "Every interval", { exact: true }).tap();
  await browser.locator("#scheduled-task-interval").fill("15");
  await screen.getByRole("switch", "Enabled", { exact: true }).tap();
  await screen.getByRole("button", "Create task", { exact: true }).tap();
  let actions = screen.getByRole("button", "Actions for Repro 39630");
  await actions.tap();
  await screen.getByRole("menuitem", "Run now").tap();
  await completedRun(1);
  const oldThreadLink = await screen.getByRole("link", "Repro 39630", { exact: true }).getAttribute("href");
  await app.screenshot("first-run-bound");
  await actions.tap();
  await screen.getByRole("menuitem", "Edit", { exact: true }).tap();
  await browser.locator("#scheduled-task-destination").tap();
  await screen.getByRole("option", "New thread each run", { exact: true }).tap();
  await browser.locator("#scheduled-task-destination").tap();
  await screen.getByRole("option", "One thread, continuing the conversation").tap();
  await browser.locator('[data-chat-provider-model-picker="true"]').tap();
  await screen.getByRole("option", "Multi turn", { exact: false }).tap();
  await screen.getByLabel("Name", { exact: true }).fill("Repro 39630 reset");
  await browser.locator("#scheduled-task-workspace").tap();
  await screen.getByRole("option", "Use the project checkout").tap();
  await browser.locator("#scheduled-task-destination").scrollIntoView();
  await app.screenshot("reset-before-save");
  await screen.getByRole("button", "Save task", { exact: true }).tap();
  actions = screen.getByRole("button", "Actions for Repro 39630 reset");
  await actions.tap();
  await screen.getByRole("menuitem", "Edit", { exact: true }).tap();
  await app.screenshot("destination-after-reopening");
  const savedDestination = await browser.locator("#scheduled-task-destination-thread").textContent();
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await actions.tap();
  await screen.getByRole("menuitem", "Run now").tap();
  await completedRun(2);
  await screen.getByText("Succeeded", { exact: true }).waitFor();
  const destinationLink = browser.locator('a.underline');
  const secondThreadLink = await destinationLink.getAttribute("href");
  await app.screenshot("second-run-destination");
  await actions.tap();
  await screen.getByRole("menuitem", "Edit", { exact: true }).tap();
  await app.screenshot("second-run-model");
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await actions.tap();
  await screen.getByRole("menuitem", "Run now").tap();
  await completedRun(3);
  const thirdThreadLink = await destinationLink.getAttribute("href");
  await app.screenshot("later-run-continues");
  expect(savedDestination?.trim()).toBe("Started by the first run");
  expect(secondThreadLink).not.toBe(oldThreadLink);
  expect(thirdThreadLink).toBe(secondThreadLink);
});
