import { test, expect, describe } from "@playwright/test";

const TEST_USER = {
  email: process.env.E2E_TEST_USER_EMAIL || "admin@example.com",
  password: process.env.E2E_TEST_USER_PASSWORD || "admin@123",
};

describe("登录页面访问测试", () => {
  test("访问根路径应重定向到登录页", async ({ page }) => {
    await page.goto("/");
    await expect(page).toHaveURL(/\/login/);
  });

  test("直接访问登录页面应成功", async ({ page }) => {
    await page.goto("/login");
    await expect(page.getByRole("heading", { name: "登录" })).toBeVisible();
  });

  test("登录页面标题正确", async ({ page }) => {
    await page.goto("/login");
    await expect(page.getByText("请输入您的账号信息登录GEO平台")).toBeVisible();
  });
});

describe("登录表单元素测试", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/login");
  });

  test("邮箱输入框存在且可填写", async ({ page }) => {
    const emailInput = page.locator("#email");
    await expect(emailInput).toBeVisible();
    await emailInput.fill("test@example.com");
    await expect(emailInput).toHaveValue("test@example.com");
  });

  test("密码输入框存在且可填写", async ({ page }) => {
    const passwordInput = page.locator("#password");
    await expect(passwordInput).toBeVisible();
    await passwordInput.fill("password123");
    await expect(passwordInput).toHaveValue("password123");
  });

  test("登录按钮存在", async ({ page }) => {
    const submitButton = page.getByRole("button", { name: /登录/ });
    await expect(submitButton).toBeVisible();
  });

  test("忘记密码链接存在", async ({ page }) => {
    const forgotLink = page.getByRole("link", { name: "忘记密码？" });
    await expect(forgotLink).toBeVisible();
  });

  test("立即注册链接存在", async ({ page }) => {
    const registerLink = page.getByRole("link", { name: "立即注册" });
    await expect(registerLink).toBeVisible();
  });
});

describe("表单验证测试", () => {
  test("未输入邮箱时HTML5验证应触发", async ({ page }) => {
    await page.goto("/login");
    const emailInput = page.locator("#email");
    const passwordInput = page.locator("#password");

    await passwordInput.fill("password123");
    await emailInput.focus();
    await emailInput.blur();

    const submitButton = page.getByRole("button", { name: /登录/ });
    await submitButton.click();

    await expect(emailInput).toHaveAttribute("required", "");
  });

  test("未输入密码时HTML5验证应触发", async ({ page }) => {
    await page.goto("/login");
    const emailInput = page.locator("#email");
    const passwordInput = page.locator("#password");

    await emailInput.fill("test@example.com");
    await emailInput.blur();

    const submitButton = page.getByRole("button", { name: /登录/ });
    await submitButton.click();

    await expect(passwordInput).toHaveAttribute("required", "");
  });
});

describe("登录失败测试", () => {
  test("使用错误密码应显示错误提示", async ({ page }) => {
    await page.goto("/login");

    await page.locator("#email").fill("admin@example.com");
    await page.locator("#password").fill("wrongpassword");
    await page.getByRole("button", { name: /登录/ }).click();

    await expect(page.locator(".text-destructive")).toBeVisible({ timeout: 10000 });
  });

  test("使用不存在的账号应显示错误提示", async ({ page }) => {
    await page.goto("/login");

    await page.locator("#email").fill("nonexistent@example.com");
    await page.locator("#password").fill("anypassword");
    await page.getByRole("button", { name: /登录/ }).click();

    await expect(page.locator(".text-destructive")).toBeVisible({ timeout: 10000 });
  });
});

describe("链接功能测试", () => {
  test("忘记密码链接可点击", async ({ page }) => {
    await page.goto("/login");
    const forgotLink = page.getByRole("link", { name: "忘记密码？" });
    await expect(forgotLink).toHaveAttribute("href", "/forgot-password");
  });

  test("立即注册链接可点击", async ({ page }) => {
    await page.goto("/login");
    const registerLink = page.getByRole("link", { name: "立即注册" });
    await expect(registerLink).toHaveAttribute("href", "/register");
  });
});
