import {
	authFile,
	createMedicationViaAPI,
	createShareTokenViaAPI,
	deleteAllMedicationsViaAPI,
	expect,
	navigateTo,
	test,
} from "./fixtures";

async function requireUserMenu(page: Parameters<Parameters<typeof test>[0]>[0]["page"]) {
	const userMenuButton = page.getByTestId("user-menu-trigger");
	test.skip(!(await userMenuButton.isVisible().catch(() => false)), "User menu is unavailable in this environment");
	return userMenuButton;
}

test.describe("App Shell", () => {
	test.use({ storageState: authFile });
	test.describe.configure({ timeout: 90000 });

	test("opens and closes profile modal from user menu", async ({ page }) => {
		await navigateTo(page, "/dashboard");

		await (await requireUserMenu(page)).click();
		await page.getByTestId("user-menu-profile").click();

		await expect(page.locator(".modal-content.profile-modal")).toBeVisible();
		await page.locator(".modal-content.profile-modal .modal-close").click();
		await expect(page.locator(".modal-content.profile-modal")).not.toBeVisible();
	});

	test("opens and closes about modal from user menu", async ({ page }) => {
		await navigateTo(page, "/dashboard");

		await (await requireUserMenu(page)).click();
		await page.getByTestId("user-menu-about").click();

		await expect(page.locator(".modal-content.about-modal")).toBeVisible();
		await expect(page.locator(".about-header h2")).toContainText("MedAssist-ng");
		await page.locator(".modal-content.about-modal .modal-close").click();
		await expect(page.locator(".modal-content.about-modal")).not.toBeVisible();
	});

	test("signs out from user menu", async ({ page }) => {
		await navigateTo(page, "/dashboard");

		await (await requireUserMenu(page)).click();
		await page.getByTestId("user-menu-signout").click();

		await expect(page.locator(".auth-container")).toBeVisible({ timeout: 15000 });
	});
});

test.describe("Public Share Routes", () => {
	test.use({ storageState: authFile });
	test.describe.configure({ timeout: 90000 });

	test.beforeAll(async () => {
		await deleteAllMedicationsViaAPI();
		await createMedicationViaAPI({
			name: "Share Overview Redirect Med",
			genericName: "Paracetamol",
			takenBy: ["Alice"],
			packageType: "blister",
			packCount: 1,
			blistersPerPack: 2,
			pillsPerBlister: 10,
			intakes: [
				{
					usage: 1,
					every: 1,
					start: new Date().toISOString().slice(0, 16),
					intakeRemindersEnabled: false,
					takenBy: "Alice",
				},
			],
		});
	});

	test.afterAll(async () => {
		await deleteAllMedicationsViaAPI();
	});

	test("redirects /share/:token/overview to /share/:token", async ({ page }) => {
		const shareToken = await createShareTokenViaAPI("Alice", 30);

		await page.goto(`/share/${shareToken.token}/overview`);
		await page.waitForLoadState("networkidle");

		await expect(page).toHaveURL(new RegExp(`/share/${shareToken.token}$`));
		await expect(page.locator(".shared-schedule-container")).toBeVisible({ timeout: 15000 });
	});
});
