import { test, expect, type Page } from "@playwright/test";
import { login } from "./auth";
test.setTimeout(140000);
test("role workspaces render real records on desktop and mobile", async ({
    page,
}, info) => {
    test.setTimeout(140000);
    const errors: string[] = [];
    page.on("pageerror", (e) => errors.push(e.message));
    for (const [name, paths] of Object.entries({
        teacher: ["/", "/classes", "/classes/1", "/reports"],
        registrar: [
            "/",
            "/students",
            "/enrollments",
            "/enrollments/1",
            "/credits",
        ],
        finance: ["/", "/finance?student=1"],
        admin: ["/", "/setup", "/users"],
        department: ["/", "/classes", "/reports"],
        student: ["/", "/classes/1", "/enrollments/1/cor"],
    })) {
        await login(page, name);
        for (const path of paths) {
            await page.goto(path);
            await expect(page.locator("main#main")).toBeVisible();
            await expect(
                page.getByText("Server Error", { exact: true }),
            ).toHaveCount(0);
            const overflow = await page.evaluate(
                () =>
                    document.documentElement.scrollWidth >
                    window.innerWidth + 1,
            );
            expect(
                overflow,
                `${name} ${path} overflows on ${info.project.name}`,
            ).toBeFalsy();
        }
        if (name === "teacher") {
            await page.goto("/");
            await page.screenshot({
                path: `design/implementation/teacher-${info.project.name}.png`,
                fullPage: true,
            });
        }
        if (info.project.name === "mobile") {
            await page
                .getByRole("button", { name: "Open menu", exact: true })
                .click();
            await expect(page.getByRole("dialog")).toBeVisible();
            await page
                .getByRole("button", { name: "Sign out", exact: true })
                .click();
        } else
            await page
                .getByRole("button", { name: "Sign out", exact: true })
                .click();
        await expect(page).toHaveURL(/\/login$/);
    }
    expect(errors).toEqual([]);
});
test("administrator creates a staff account through the form", async ({
    page,
}, info) => {
    test.skip(
        info.project.name === "mobile",
        "Desktop covers account creation; mobile covers the same responsive workspace.",
    );
    const email = `browser-${Date.now()}@school.test`;
    await login(page, "admin");
    await page.goto("/users");
    await page
        .getByRole("button", { name: "Create user", exact: true })
        .click();
    const dialog = page.getByRole("dialog");
    await dialog
        .getByLabel("Full name", { exact: true })
        .fill("Browser Test Teacher");
    await dialog.getByLabel("Email address", { exact: true }).fill(email);
    await dialog
        .getByLabel("Temporary password", { exact: true })
        .fill("Temporary2026Pass");
    await dialog
        .getByLabel("Confirm temporary password", { exact: true })
        .fill("Temporary2026Pass");
    await dialog
        .getByLabel("Branch scope", { exact: true })
        .selectOption({ label: "Main campus" });
    await dialog
        .getByRole("button", { name: "Create account", exact: true })
        .click();
    await expect(dialog).not.toBeVisible();
    await page
        .getByRole("textbox", { name: "Search users", exact: true })
        .fill(email);
    await page.getByRole("button", { name: "Search", exact: true }).click();
    await expect(page.getByText(email, { exact: true })).toBeVisible();
    await expect(page).toHaveURL(/search=/);
    const row = page.getByRole("row").filter({ hasText: email });
    await row.getByRole("button", { name: "Edit access", exact: true }).click();
    await page
        .getByRole("dialog")
        .getByLabel("Account status")
        .selectOption("0");
    await page
        .getByRole("button", { name: "Save account", exact: true })
        .click();
    await expect(page.getByRole("dialog")).not.toBeVisible();
    await expect(row.getByText("Inactive", { exact: true })).toBeVisible();
});
