import { expect, test } from "../../../frontend/node_modules/@playwright/test/index.mjs";

test.beforeEach(async ({ page }) => {
  await page.route("**/api/auth/config", async (route) => {
    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({ enabled: false }),
    });
  });
  await page.route("**/api/lab/status*", async (route) => {
    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({
        connected: true,
        atlas: { up: true },
        oceanus: { up: false },
      }),
    });
  });
  await page.route("**/api/access/request/availability*", async (route) => {
    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({ available: true }),
    });
  });
  await page.route("**/api/access/request", async (route) => {
    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({
        status: "pending_email_verification",
        request_code: "alice~ABC123",
      }),
    });
  });
});

test("submits an access request and shows the request code", async ({ page }) => {
  await page.goto("/request-access");

  await page.getByLabel("Lab Name (username)").fill("alice");
  await expect(page.getByText("Username is available.")).toBeVisible();
  await page.getByLabel("Last name").fill("Atlas");
  await page.getByLabel("Email").fill("alice@example.dev");
  await page.getByRole("button", { name: "Submit request" }).click();

  await expect(page.getByText("Request submitted.")).toBeVisible();
  await expect(page.getByText("alice~ABC123")).toBeVisible();
});
