import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Fixed-time schedules identify the server timezone", WEB, async ({ t3, app, screen, browser }) => {
  const zone = process.env.TZ === "America/Los_Angeles" ? "PT" : "UTC";
  const runZone = zone === "PT" ? "PST" : "UTC";
  const project = await t3.project("Schedule demo");
  await t3.thread({ project, title: "Daily report", messages: ["Prepare the report"] });
  await t3.open();
  await screen.getByText("Daily report", { exact: true }).first().waitFor();
  await app.open("/settings/scheduled-tasks");
  await screen.getByRole("button", "New task").tap();
  await screen.getByLabel("Name", { exact: true }).fill("Daily report task");
  await screen.getByLabel("Destination", { exact: true }).tap();
  await screen.getByRole("option", "One thread, continuing the conversation").tap();
  await screen.getByRole("combobox", "Destination thread").tap();
  await screen.getByRole("option", "Daily report").tap();
  await screen.getByLabel("Prompt", { exact: true }).fill("Prepare a daily summary.");
  await screen.getByLabel("Run at", { exact: true }).fill("16:31");
  await screen.getByRole("button", "Sat", { exact: true }).tap();
  await screen.getByRole("button", "Sun", { exact: true }).tap();
  await screen.getByRole("switch", "Enabled", { exact: true }).tap();
  await screen.getByRole("heading", "New task", { exact: true }).tap();
  await app.screenshot("fixed-time-editor");
  await expect.soft(browser.locator("#scheduled-task-time-zone")).toHaveText(zone);
  await screen.getByRole("button", "Create task", { exact: true }).tap();
  await screen.getByText("Daily at 16:31", { exact: false }).first().waitFor();
  await app.screenshot("paused-fixed-time-row");
  await expect.soft(screen.getByText(`Daily at 16:31 ${zone} · Paused`, { exact: false })).toBeVisible();
  await screen.getByRole("switch", "Enable Daily report task", { exact: true }).tap();
  await screen.getByText("Next run", { exact: false }).waitFor();
  await app.screenshot("enabled-fixed-time-row");
  await expect.soft(screen.getByText(`Daily at 16:31 ${runZone}`, { exact: false })).toBeVisible();
  await screen.getByRole("switch", "Enable Daily report task", { exact: true }).tap();
  await app.open("/");
  await screen.getByText("Daily report", { exact: true }).first().tap();
  await screen.getByRole("button", "Toggle thread details panel", { exact: true }).tap();
  await screen.getByRole("heading", "Automations", { exact: true }).waitFor();
  await app.screenshot("thread-automations-panel");
  await browser.locator("[data-thread-automations-panel]").getByText("Daily report task", { exact: true }).hover();
  await screen.getByRole("tooltip").waitFor();
  await app.screenshot("thread-automations-tooltip");
  await expect.soft(screen.getByRole("tooltip")).toContainText(`Daily at 16:31 ${zone}`);
});
