import { test, expect } from "@playwright/test";
import { execFileSync } from "node:child_process";
import { login } from "./auth";

test("College picker excludes High School and unfinished enrollment can be removed without deleting Student", async ({
    page,
}, info) => {
    const f = JSON.parse(
        execFileSync("php", ["tests/browser/phase1-fixture.php", "college"], {
            encoding: "utf8",
        }),
    );
    const errors: string[] = [];
    page.on("pageerror", (error) => errors.push(error.message));
    await login(page, "registrar");
    await page.goto(`/enrollment-batches/${f.batch}`);
    await page
        .getByRole("button", { name: "Add Student", exact: true })
        .click();
    let dialog = page.getByRole("dialog");
    const picker = dialog.getByLabel("Student", { exact: true });
    await expect(
        picker.locator(`option[value="${f.ineligible_student}"]`),
    ).toHaveCount(0);
    await picker.selectOption(String(f.student));
    await dialog
        .getByRole("button", { name: "Begin enrollment", exact: true })
        .click();
    await expect(page).toHaveURL(/\/enrollment-applications\/\d+$/);
    const applicationUrl = page.url();
    await page
        .getByRole("link", { name: "Back to batch", exact: true })
        .click();
    await expect(page).toHaveURL(`/enrollment-batches/${f.batch}`);
    const row = page
        .locator("tr:visible,article.subject-choice:visible")
        .filter({ hasText: f.tag });
    await row
        .getByRole("button", { name: "Remove from batch", exact: true })
        .click();
    dialog = page.getByRole("dialog");
    await expect(dialog).toContainText(
        "previous enrollments, grades, financial records and audit history are retained",
    );
    await dialog
        .getByLabel("Removal reason", { exact: true })
        .fill("Student was added to the wrong intake during review.");
    await page.screenshot({
        path: `design/implementation/remove-enrollment-student-${info.project.name}.png`,
    });
    await dialog
        .getByRole("button", { name: "Remove Student from batch", exact: true })
        .click();
    await expect(dialog).toHaveCount(0);
    await expect(row).toHaveCount(0);
    await page
        .getByRole("button", { name: "Add Student", exact: true })
        .click();
    dialog = page.getByRole("dialog");
    await expect(
        dialog
            .getByLabel("Student", { exact: true })
            .locator(`option[value="${f.student}"]`),
    ).toHaveCount(1);
    await page.keyboard.press("Escape");
    await page.goto(`/students/${f.student}`);
    await expect(
        page.getByRole("heading", { name: `Phase One ${f.tag}`, exact: true }),
    ).toBeVisible();
    await page.goto(applicationUrl);
    await expect(page).toHaveURL(`/enrollment-batches/${f.batch}`);
    expect(
        await page.evaluate(
            () => document.documentElement.scrollWidth <= innerWidth + 1,
        ),
    ).toBeTruthy();
    expect(errors).toEqual([]);
});
