import { expect, test } from "@playwright/test";

test.setTimeout(120_000);
const screenshotRoot = "../../../../specs/develop/20260623_2/m7_copyable_values_pagination_playwright";

async function openRepositoryBranch(page: import("@playwright/test").Page) {
  await page.goto("/");
  await page.getByRole("button", { name: /Repositories/i }).click();
  const reposSection = page.locator('section[aria-label="Repositories"]');
  await expect(reposSection.locator(".stack-row").first()).toBeVisible({ timeout: 30_000 });
  await reposSection.getByLabel("Filter repositories on current page").fill("sakuya");
  await reposSection.locator(".stack-row-select").first().click();

  const ppSection = page.locator('section[aria-label="Publication points"]');
  await expect(ppSection.locator(".stack-row").first()).toBeVisible({ timeout: 30_000 });
  await ppSection.locator(".stack-row-select").first().click();

  const objectsPanel = page.getByRole("region", { name: "Objects for publication point" });
  await expect(objectsPanel.locator("tbody tr").first()).toBeVisible({ timeout: 70_000 });
  return objectsPanel;
}

test("long repository, publication point, URI, and hash values expose copy and full hover text", async ({ context, page }) => {
  await context.grantPermissions(["clipboard-write"], { origin: "http://127.0.0.1:5173" });
  const objectsPanel = await openRepositoryBranch(page);

  await expect(page.getByRole("button", { name: "Copy repository URI" }).first()).toBeVisible();
  await expect(page.getByRole("button", { name: "Copy publication point URI" }).first()).toBeVisible();
  await expect(page.getByRole("button", { name: "Copy manifest URI" }).first()).toBeVisible();
  await expect(objectsPanel.getByRole("button", { name: "Copy object URI" }).first()).toBeVisible();
  await expect(objectsPanel.getByRole("button", { name: "Copy object SHA256" }).first()).toBeVisible();

  const objectUri = objectsPanel.locator(".uri-cell .copyable-value-text").first();
  const objectUriText = await objectUri.innerText();
  await objectUri.hover();
  await expect(page.locator(".copyable-tooltip")).toContainText(objectUriText);

  await objectsPanel.getByRole("button", { name: "Copy object URI" }).first().click();
  await expect(objectsPanel.locator(".copy-state-text").first()).toHaveText("Copied");
  await objectUri.hover();
  await page.screenshot({ path: `${screenshotRoot}/repository-copyable-tooltip.png`, fullPage: true });
});

test("object detail keeps copy controls without extra object list panels", async ({ page }) => {
  const objectsPanel = await openRepositoryBranch(page);
  await objectsPanel.getByRole("button", { name: "Open" }).first().click();
  await expect(page.getByText("Object detail · live query service")).toBeVisible({ timeout: 70_000 });
  await expect.poll(async () => page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(true);

  await expect(page.getByRole("complementary", { name: "Publication point object list" })).toHaveCount(0);
  await expect(page.getByRole("region", { name: "Publication point object table" })).toHaveCount(0);
  await expect(page.getByRole("button", { name: "Copy selected object URI" })).toBeVisible();
  await expect(page.getByRole("button", { name: "Copy SHA256" })).toBeVisible();
  await expect(page.getByRole("button", { name: "Copy Repository" })).toBeVisible();
  await expect(page.getByRole("button", { name: "Copy Publication Point" })).toBeVisible();

  const objectHash = page.locator(".object-meta-grid").getByText(/^[a-f0-9]{64}$/).first();
  const objectHashText = await objectHash.innerText();
  await objectHash.hover();
  await expect(page.locator(".copyable-tooltip")).toContainText(objectHashText.toLowerCase());
  await page.screenshot({ path: `${screenshotRoot}/object-detail-copyable-only.png`, fullPage: true });
});
