import { test, expect } from "@playwright/test";
import { installLifecycleModuleApiStubs, E2E_IDS } from "./fixtures/lifecycle-api-stubs";
import { loginAsDdAdmin } from "./fixtures/login";

test.describe("Lifecycle modules (stubbed API)", () => {
    test.beforeEach(async ({ page }) => {
        await installLifecycleModuleApiStubs(page);
    });

    test("Termination — list and detail", async ({ page }) => {
        await loginAsDdAdmin(page);
        await page.goto("/termination");
        await expect(page.getByRole("heading", { name: "Termination Requests" })).toBeVisible();
        await expect(page.getByText("E2E Termination Dealer").first()).toBeVisible();

        await page.getByRole("button", { name: "View Details" }).first().click();
        await expect(page).toHaveURL(new RegExp(`/termination/${E2E_IDS.termination}$`));
        await expect(page.getByText("E2E Termination Dealer").first()).toBeVisible();
    });

    test("Resignation — list and detail", async ({ page }) => {
        await loginAsDdAdmin(page);
        await page.goto("/resignation");
        await expect(page.getByRole("heading", { name: "Resignation Requests" })).toBeVisible();
        await expect(page.getByText("RES-E2E-001").first()).toBeVisible();

        await page.getByRole("button", { name: "View Details" }).first().click();
        await expect(page).toHaveURL(new RegExp(`/resignation/${E2E_IDS.resignation}$`));
        await expect(page.getByText("E2E Resignation Dealer")).toBeVisible();
    });

    test("Relocation — list and detail", async ({ page }) => {
        await loginAsDdAdmin(page);
        await page.goto("/relocation-requests");
        await expect(page.getByRole("heading", { name: "Relocation Request Management" })).toBeVisible();
        await expect(page.getByText("REL-E2E-001")).toBeVisible();

        await page.getByRole("button", { name: "View", exact: true }).first().click();
        await expect(page).toHaveURL(new RegExp(`/relocation-requests/${E2E_IDS.relocation}$`));
        await expect(page.getByText("E2E Relocation Dealer").first()).toBeVisible();
    });

    test("Constitutional change — list and detail", async ({ page }) => {
        await loginAsDdAdmin(page);
        await page.goto("/constitutional-change");
        await expect(page.getByRole("heading", { name: "Constitutional Change Management" })).toBeVisible();
        await expect(page.getByText("CCR-E2E-001")).toBeVisible();

        await page.getByRole("button", { name: "View", exact: true }).first().click();
        await expect(page).toHaveURL(new RegExp(`/constitutional-change/${E2E_IDS.constitutionalPublicId}$`));
        await expect(page.getByText("E2E Constitutional Dealer", { exact: true }).first()).toBeVisible();
    });
});
