import { test, expect } from "#tests/e2e/fixtures/auth"

test.describe("Login Page", () => {
	test("should display login form with all fields", async ({ loginPage }) => {
		const { page } = loginPage

		await expect(page.getByRole("heading", { name: "Вход" })).toBeVisible()
		await expect(
			page.getByRole("textbox", { name: "Логин" }),
		).toBeVisible()
		await expect(
			page.getByRole("textbox", { name: "Пароль" }),
		).toBeVisible()
		await expect(
			page.getByRole("button", { name: "Войти" }),
		).toBeVisible()
		await expect(
			page.getByRole("link", { name: /Зарегистрироваться/ }),
		).toBeVisible()
	})

	test("should have link to registration page", async ({ loginPage }) => {
		const { page } = loginPage

		await page.getByRole("link", { name: /Зарегистрироваться/ }).click()
		await expect(page).toHaveURL(/\/register/)
	})

	test("should login successfully and set auth cookies", async ({
		loginPage,
	}) => {
		const { page } = loginPage

		await loginPage.mockLoginSuccess()
		await loginPage.login("testuser", "password123")

		// Verify the mutation succeeded by checking auth cookies are set
		await expect(async () => {
			const cookies = await page.evaluate(() => document.cookie)
			expect(cookies).toContain("access_token=fake-access-jwt")
			expect(cookies).toContain("refresh_token=fake-refresh-jwt")
		}).toPass({ timeout: 5_000 })
	})

	test("should show error on invalid credentials", async ({ loginPage }) => {
		const { page } = loginPage
		const consoleErrors: string[] = []

		page.on("console", (msg) => {
			if (msg.type() === "error") {
				consoleErrors.push(msg.text())
			}
		})

		await loginPage.mockLoginError(401)
		await loginPage.login("wronguser", "wrongpassword")

		await page.waitForTimeout(1000)
		await expect(page).toHaveURL(/\/login/)
		expect(consoleErrors.some((e) => e.includes("Login failed"))).toBe(true)
	})

	test("should handle network error gracefully", async ({ loginPage }) => {
		const { page } = loginPage

		await loginPage.mockLoginNetworkError()
		await loginPage.login("testuser", "password123")

		await page.waitForTimeout(1000)
		await expect(page).toHaveURL(/\/login/)
	})

	test("should handle server error (500)", async ({ loginPage }) => {
		const { page } = loginPage

		await loginPage.mockLoginError(500, {
			detail: "Internal server error",
		})
		await loginPage.login("testuser", "password123")

		await page.waitForTimeout(1000)
		await expect(page).toHaveURL(/\/login/)
	})

	test("should submit with empty fields", async ({ loginPage }) => {
		const { page } = loginPage

		await loginPage.mockLoginError(422, { detail: "Validation error" })
		await page.getByRole("button", { name: "Войти" }).click()

		await page.waitForTimeout(500)
		await expect(page).toHaveURL(/\/login/)
	})

	test("should disable submit button while request is pending", async ({
		loginPage,
	}) => {
		const { page } = loginPage

		await loginPage.mockLoginDelayed(2000)

		await page.getByRole("textbox", { name: "Логин" }).fill("testuser")
		await page.getByRole("textbox", { name: "Пароль" }).fill("password123")

		const submitButton = page.getByRole("button", { name: "Войти" })
		await submitButton.click()

		await expect(submitButton).toBeDisabled()
	})
})
