import { expect, test } from "@playwright/test";
import { installMockApi } from "./mockApi";
import { REPOS } from "./fixtures";

test.beforeEach(async ({ page }) => {
  await installMockApi(page);
});

test("repository list renders rows and filters the current page", async ({ page }) => {
  await page.goto("/repositories");
  await expect(page.getByRole("cell", { name: "repo-a.example" })).toBeVisible();
  await expect(page.getByRole("cell", { name: "repo-b.example" })).toBeVisible();
  await expect(page.getByRole("cell", { name: "repo-c.example" })).toBeVisible();

  await page.getByLabel("Filter current page").fill("repo-b");
  await expect(page.getByRole("cell", { name: "repo-a.example" })).toBeHidden();
  await expect(page.getByRole("cell", { name: "repo-b.example" })).toBeVisible();
});

test("repository list paginates with cursors", async ({ page }) => {
  // Register a 2-page dataset on top of the base mock (later routes win).
  await page.route("**/api/v1/runs/*/repos?**", (route) => {
    const url = new URL(route.request().url());
    const cursor = url.searchParams.get("cursor");
    const items = cursor === "2" ? REPOS.slice(2) : REPOS.slice(0, 2);
    return route.fulfill({
      contentType: "application/json",
      body: JSON.stringify({
        data: items,
        page: { nextCursor: cursor === "2" ? null : "2", limit: 50 },
        meta: { runId: "run_0002", schemaVersion: 1 },
      }),
    });
  });

  await page.goto("/repositories");
  await expect(page.getByText("Page 1")).toBeVisible();
  await page.getByRole("button", { name: "Next" }).click();
  await expect(page.getByText("Page 2")).toBeVisible();
  await expect(page.getByRole("cell", { name: "repo-c.example" })).toBeVisible();
  await page.getByRole("button", { name: "Previous" }).click();
  await expect(page.getByText("Page 1")).toBeVisible();
  await expect(page.getByRole("cell", { name: "repo-a.example" })).toBeVisible();
});

test("repository detail: tabs switch between PPs and objects", async ({ page }) => {
  await page.goto("/repositories/repo-a1b2c3d4e5f6");

  await expect(page.getByRole("heading", { name: "repo-a.example" })).toBeVisible();
  await expect(page.getByText("rsync://repo-a.example/ca1/manifest.mft")).toBeVisible();

  // Objects tab is lazy: no repo-scoped objects request before activation
  const objectRequests: string[] = [];
  page.on("request", (req) => {
    if (req.url().includes("/repos/repo-a1b2c3d4e5f6/objects")) objectRequests.push(req.url());
  });

  await page.getByRole("tab", { name: "Objects" }).click();
  await expect(page.getByLabel("URI contains")).toBeVisible();
  await expect(page.getByText("rsync://repo-a.example/ca1/alice.roa")).toBeVisible();
  expect(objectRequests.length).toBeGreaterThan(0);

  await page.getByRole("tab", { name: "Publication points" }).click();
  await expect(page.getByText("rsync://repo-a.example/ca1b/manifest.mft")).toBeVisible();
});

test("publication point detail shows metadata and its objects", async ({ page }) => {
  await page.goto("/publication-points/node_3");
  await expect(page.getByText("rsync://repo-b.example/ca2/manifest.mft").first()).toBeVisible();
  await expect(page.getByText("terminal cached")).toBeVisible();
  await expect(page.getByText("rsync://repo-b.example/ca2/broken.roa")).toBeVisible();
});

test("publication points list navigates to detail", async ({ page }) => {
  await page.goto("/publication-points");
  await page.getByText("rsync://repo-b.example/ca2/manifest.mft").click();
  await expect(page).toHaveURL(/\/publication-points\/node_3/);
});
