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

test("Course plan allocates Lecture and Laboratory credits and retains the published plan", async ({
    page,
}, info) => {
    const f = JSON.parse(
        execFileSync("php", ["tests/browser/phase1-fixture.php", "college"], {
            encoding: "utf8",
        }),
    );
    const errors: string[] = [];
    page.on("pageerror", (e) => errors.push(e.message));
    await login(page, "admin");
    await page.goto("/setup");
    await page
        .locator("summary")
        .filter({ hasText: /^Create curriculum version$/ })
        .click();
    const create = page.locator("form").filter({
        has: page.getByRole("button", {
            name: "Create curriculum version",
            exact: true,
        }),
    });
    await create
        .getByLabel("Course", { exact: true })
        .selectOption(String(f.program));
    await create
        .getByLabel("Global Curriculum", { exact: true })
        .selectOption({ label: "Phase 1 browser" });
    await create
        .getByLabel("Version", { exact: true })
        .fill(f.tag + " revised");
    await create
        .getByRole("button", { name: "Create curriculum version", exact: true })
        .click();
    await expect(
        page.getByText("School setup saved.", { exact: true }),
    ).toBeVisible();
    const details = page
        .locator("details")
        .filter({
            has: page.locator("summary").filter({
                hasText:
                    f.tag + " College Course · " + f.tag + " revised · Draft",
            }),
        })
        .first();
    await details.locator("summary").first().click();
    const add = details.locator("form").filter({
        has: page.getByRole("button", {
            name: "Add curriculum subject",
            exact: true,
        }),
    });
    await add
        .getByLabel("Subject", { exact: true })
        .selectOption(String(f.subject));
    await add.getByLabel("Credits 1", { exact: true }).fill("2");
    await add
        .getByRole("button", { name: "Add Unit Type", exact: true })
        .click();
    await add
        .getByLabel("Unit Type 2", { exact: true })
        .selectOption({ label: "Laboratory · ₱0.00 per credit" });
    await add.getByLabel("Credits 2", { exact: true }).fill("1");
    await add.getByLabel("Year / grade", { exact: true }).fill("1");
    await add
        .getByLabel("Curriculum term", { exact: true })
        .selectOption("semester_1");
    await add
        .getByLabel("Classification", { exact: true })
        .selectOption("Major");
    await add
        .getByRole("button", { name: "Add curriculum subject", exact: true })
        .click();
    await expect(
        details.getByText("Lecture: 2.00 credits", { exact: true }),
    ).toBeVisible();
    await expect(
        details.getByText("Laboratory: 1.00 credits", { exact: true }),
    ).toBeVisible();
    expect(
        await page.evaluate(
            () => document.documentElement.scrollWidth <= innerWidth + 1,
        ),
    ).toBeTruthy();
    await details
        .locator("summary")
        .filter({ hasText: `Edit ${f.tag} Lecture / Laboratory credits` })
        .click();
    const allocation = details
        .locator("form")
        .filter({
            has: page.getByRole("button", {
                name: "Save subject Unit Types",
                exact: true,
            }),
        });
    await allocation.scrollIntoViewIfNeeded();
    await page.screenshot({
        path: `design/implementation/curriculum-unit-types-${info.project.name}.png`,
    });
    await details
        .getByRole("button", {
            name: "Publish curriculum version",
            exact: true,
        })
        .click();
    await expect(
        page.getByText("School setup saved.", { exact: true }),
    ).toBeVisible();
    await expect(
        page.getByText(
            f.tag + " College Course · " + f.tag + " revised · Published",
            { exact: true },
        ),
    ).toBeVisible();
    expect(errors).toEqual([]);
});
