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

test("Filtered sidebar explains hidden threads and offers recovery in every empty scope", WEB, async ({ t3, app, screen, browser }) => {
  const alpha = await t3.project("alpha");
  const beta = await t3.project("beta");
  await t3.project("gamma");
  await t3.project("delta");
  await t3.thread({ project: alpha, title: "Alpha working one", messages: ["First task"] });
  await t3.thread({ project: alpha, title: "Alpha working two", messages: ["Second task"] });
  await t3.thread({ project: beta, title: "Beta completed task", messages: ["Completed task"] });
  await t3.open();
  const betaRow = browser.locator('[data-sidebar-row]').filter({ hasText: "Beta completed task" });
  await betaRow.hover();
  await betaRow.getByRole("button", "Settle thread").tap();
  const filter = screen.getByRole("combobox", { name: /^Filter threads by project/ });
  const pick = async (name: string) => {
    await filter.tap();
    await screen.getByRole("option", name).tap();
  };
  const capture = async (label: string, message: string) => {
    await browser.mouse.move(900, 650);
    await filter.waitFor({ state: "visible" });
    await screen.getByText("Alpha working one").first().waitFor({ state: "hidden" });
    await expect.soft(screen.getByText(message)).toBeVisible();
    await expect.soft(screen.getByRole("button", "Show all projects")).toBeVisible();
    await app.screenshot(label);
  };
  await pick("beta");
  await capture("single-project-settled-only", "No active threads in beta");
  // A route inside the selected scope preserves that scope on reload.
  await screen.getByRole("button", { name: /^Settled/ }).tap();
  await screen.getByText("Beta completed task").first().tap();
  await browser.reload();
  await browser.mouse.move(900, 650);
  await capture("single-project-after-reload", "No active threads in beta");
  await filter.tap();
  await screen.getByRole("option", "gamma").tap({ modifiers: ["Shift"] });
  await browser.keyboard.press("Escape");
  await capture("multiple-projects-settled-only", "No active threads in these projects");
  await pick("gamma");
  await capture("single-project-no-threads", "No threads in gamma yet");
  await filter.tap();
  await screen.getByRole("option", "delta").tap({ modifiers: ["Shift"] });
  await browser.keyboard.press("Escape");
  await capture("multiple-projects-no-threads", "No threads in these projects yet");
  const clear = screen.getByRole("button", "Show all projects");
  if (await clear.isVisible()) await clear.tap();
  else await pick("All projects");
  await expect.soft(screen.getByText("Alpha working one").first()).toBeVisible();
  await expect.soft(screen.getByText("Alpha working two").first()).toBeVisible();
  await app.screenshot("all-projects-recovered");
});
