import { readFile } from "node:fs/promises";
import { setTimeout as pause } from "node:timers/promises";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Scheduled tasks retain editable reasoning effort", WEB, async ({ t3, app, screen, browser }) => {
  const capture = async (label: string) => {
    let previous = await readFile(await app.screenshot(`${label}-settling-0`));
    for (let i = 1; i <= 8; i++) {
      await pause(500);
      const next = await readFile(await app.screenshot(`${label}-settling-${i}`));
      if (previous.equals(next)) { await app.screenshot(label); return; }
      previous = next;
    }
    throw new Error("Capture did not become stable");
  };
  await t3.project("Schedule demo");
  await t3.open();
  await screen.getByText("Schedule demo", { exact: true }).first().waitFor();
  await app.open("/settings/scheduled-tasks");
  await screen.getByRole("button", "New task", { exact: true }).tap();
  await screen.getByLabel("Name", { exact: true }).fill("Review issues");
  await screen.getByLabel("Prompt", { exact: true }).fill("Review the project and summarize open work.");
  await screen.getByLabel("Project", { exact: true }).tap();
  await screen.getByRole("option", "Schedule demo", { exact: true }).tap();
  await screen.getByLabel("Destination", { exact: true }).tap();
  await screen.getByRole("option", "New thread each run", { exact: true }).tap();
  await screen.getByRole("button", "Reasoning model", { exact: true }).tap();
  await screen.getByRole("option", /Reasoning model/).tap();
  await screen.getByRole("switch", "Enabled", { exact: true }).tap();
  await capture("effort-control");
  await expect(screen.getByRole("button", "Low", { exact: true })).toBeVisible();
  await screen.getByRole("button", "Low", { exact: true }).tap();
  await screen.getByRole("menuitemradio", /^High/).tap();
  await capture("high-selected");
  await screen.getByRole("button", "Create task", { exact: true }).tap();
  const reopen = async () => {
    await screen.getByRole("button", "Actions for Review issues", { exact: true }).tap();
    await screen.getByRole("menuitem", "Edit", { exact: true }).tap();
  };
  await reopen();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeVisible();
  await capture("high-retained-after-reopen");
  await screen.getByRole("button", "High", { exact: true }).tap();
  await screen.getByRole("menuitemradio", /^Low/).tap();
  await screen.getByRole("button", "Save task", { exact: true }).tap();
  await reopen();
  await expect(screen.getByRole("button", "Low", { exact: true })).toBeVisible();
  await capture("edited-effort-retained");
  await screen.getByRole("button", "Low", { exact: true }).tap();
  await screen.getByRole("menuitemradio", /^High/).tap();
  await screen.getByRole("button", "Reasoning model", { exact: true }).tap();
  await screen.getByRole("option", /Reasoning model/).tap();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeVisible();
  await screen.getByRole("button", "Save task", { exact: true }).tap();
  await reopen();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeVisible();
  await capture("high-retained-after-reselect");
  await screen.getByRole("button", "Reasoning model", { exact: true }).tap();
  await screen.getByRole("option", /Plain model/).tap();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeHidden();
  await screen.getByRole("button", "Save task", { exact: true }).tap();
  await reopen();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeHidden();
  await capture("switched-model-options-cleared");
  await screen.getByRole("button", "Plain model", { exact: true }).tap();
  await screen.getByRole("option", /Reasoning model/).tap();
  await expect(screen.getByRole("button", "Low", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "High", { exact: true })).toBeHidden();
  await capture("returning-model-uses-default-effort");
});
