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

test("Sidebar titles expose the text where it is drawn", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Demo project");
  const first = await t3.thread({ project, title: "Fix login redirect loop", messages: ["Demonstrate the login fix"] });
  await t3.thread({ project, title: "Refactor the billing module", messages: ["Demonstrate billing work"] });
  await t3.open(first);
  const billing = screen.getByRole("button", /Refactor the billing module/);
  await billing.waitFor();
  const rail = await screen.getByRole("button", "Resize Sidebar").boundingBox();
  if (!rail) throw new Error("Sidebar resize rail missing");
  await browser.mouse.move(rail.x + rail.width / 2, rail.y + rail.height / 2);
  await browser.mouse.down();
  await browser.mouse.move(400, rail.y + rail.height / 2);
  await browser.mouse.up();
  const threadUrl = await browser.url();
  const results: boolean[] = [];
  async function capture(label: string, titles: string[]) {
    await browser.mouse.move(1000, 500);
    await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
    results.push(await browser.evaluate((titles) => {
      document.querySelectorAll('[data-proof-overlay]').forEach(e => e.remove());
      let correct = true;
      for (const title of titles) {
        const elements = [...document.querySelectorAll('span,div')].filter(e => e.textContent === title && e.children.length === 0);
        const exposed = elements.find(e => !e.closest('[aria-hidden="true"]'));
        const visible = elements.find(e => { const b = e.getBoundingClientRect(); return b.width > 10 && b.height > 10; });
        if (!exposed || !visible) { correct = false; continue; }
        const b = exposed.getBoundingClientRect(), v = visible.getBoundingClientRect();
        const ok = Math.abs(b.x-v.x)<2 && Math.abs(b.y-v.y)<2 && b.width>10 && b.height>10;
        correct &&= ok;
        const box = document.createElement('div');
        box.dataset.proofOverlay = 'true';
        box.setAttribute('aria-hidden', 'true');
        Object.assign(box.style, { position:'fixed', left:`${b.x}px`, top:`${b.y}px`, width:`${Math.max(b.width,3)}px`, height:`${Math.max(b.height,3)}px`, border:`2px solid ${ok?'#16a34a':'#e11d48'}`, zIndex:'2147483647', pointerEvents:'none' });
        document.body.append(box);
        const note = document.createElement('div');
        note.dataset.proofOverlay = 'true'; note.setAttribute('aria-hidden','true');
        note.textContent = `${title}: exposed ${Math.round(b.x)},${Math.round(b.y)} ${Math.round(b.width)}×${Math.round(b.height)}; drawn ${Math.round(v.x)},${Math.round(v.y)} ${Math.round(v.width)}×${Math.round(v.height)}`;
        Object.assign(note.style, { position:'fixed', left:'425px', top:`${100+titles.indexOf(title)*45}px`, padding:'8px', background:'#fff', color:ok?'#166534':'#9f1239', font:'13px monospace', zIndex:'2147483647', pointerEvents:'none', border:'1px solid currentColor' });
        document.body.append(note);
      }
      return correct;
    }, titles));
    await app.screenshot(label);
    await browser.evaluate(() => document.querySelectorAll('[data-proof-overlay]').forEach(e => e.remove()));
  }
  await capture("card-titles", ["Refactor the billing module", "Fix login redirect loop"]);
  await screen.getByRole("button", "New thread", { exact: true }).tap();
  await browser.locator('[contenteditable="true"]').fill("Draft password reset");
  await billing.tap();
  await screen.getByTestId("sidebar-draft-row").waitFor();
  await capture("unsent-draft", ["Draft password reset"]);
  await app.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").tap();
  await browser.goto(threadUrl);
  await screen.getByTestId("sidebar-row-folder").first().waitFor();
  await capture("folder-titles", ["Refactor the billing module", "Fix login redirect loop"]);
  await app.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").tap();
  await browser.goto(threadUrl);
  await billing.waitFor();
  await billing.secondaryTap();
  await screen.getByRole("menuitem", "Snooze", { exact: true }).hover();
  await screen.getByRole("menuitem", /1 hour|In an hour|For an hour/).tap();
  await screen.getByRole("button", /Snoozed/).tap();
  await screen.getByTestId("sidebar-row-slim").first().waitFor();
  await capture("snoozed-slim-title", ["Refactor the billing module"]);
  await expect(results).toEqual([true, true, true, true]);
});
