import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';
import { UserManagementPage } from './pages/UserManagementPage';
import { RoleManagementPage } from './pages/RoleManagementPage';
import { TestHelper } from './utils/testHelper';

test.describe('边缘场景测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let userManagementPage: UserManagementPage;
  let roleManagementPage: RoleManagementPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    userManagementPage = new UserManagementPage(page);
    roleManagementPage = new RoleManagementPage(page);

    await loginPage.goto();
    await loginPage.login('admin', 'admin123');
  });

  test.afterEach(async ({ page }) => {
    await TestHelper.clearAllStorage(page);
  });

  test.describe('边界值测试', () => {
    test('用户名边界值 - 最小长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建最小长度用户名的用户', async () => {
        await userManagementPage.clickCreateUser();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        const minUsername = 'ab';
        await userManagementPage.fillUserForm({
          username: minUsername,
          email: 'test@example.com',
          password: 'password123'
        });
        await userManagementPage.submitForm();
      });

      await test.step('验证用户创建成功', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('用户名边界值 - 最大长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建最大长度用户名的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        const maxUsername = 'a'.repeat(50);
        await userManagementPage.fillUsername(maxUsername);
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证用户创建成功', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('用户名边界值 - 超过最大长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建超过最大长度用户名的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        const exceedUsername = 'a'.repeat(51);
        await userManagementPage.fillUsername(exceedUsername);
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证用户名长度验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('用户名长度不能超过50个字符');
      });
    });

    test('密码边界值 - 最小长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建最小长度密码的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        const minPassword = 'a'.repeat(6);
        await userManagementPage.fillPassword(minPassword);
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证用户创建成功', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('密码边界值 - 最大长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建最大长度密码的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        const maxPassword = 'a'.repeat(20);
        await userManagementPage.fillPassword(maxPassword);
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证用户创建成功', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('密码边界值 - 低于最小长度', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建低于最小长度密码的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        const shortPassword = 'a'.repeat(5);
        await userManagementPage.fillPassword(shortPassword);
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证密码长度验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('密码长度不能少于6个字符');
      });
    });

    test('邮箱边界值 - 无效格式', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建无效邮箱格式的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('invalid-email');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证邮箱格式验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('邮箱格式不正确');
      });
    });

    test('角色名边界值 - 特殊字符', async ({ page }) => {
      await dashboardPage.navigateToRoleManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建包含特殊字符的角色', async () => {
        await roleManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        const specialCharRole = '角色@#$%';
        await roleManagementPage.fillRoleName(specialCharRole);
        await roleManagementPage.fillRoleKey('ROLE_SPECIAL');
        await roleManagementPage.clickSaveButton();
      });

      await test.step('验证特殊字符处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });
  });

  test.describe('空值和null值测试', () => {
    test('用户创建 - 用户名为空', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建用户名为空的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证用户名必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('用户名不能为空');
      });
    });

    test('用户创建 - 密码为空', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建密码为空的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证密码必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('密码不能为空');
      });
    });

    test('用户创建 - 邮箱为空', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建邮箱为空的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证邮箱必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('邮箱不能为空');
      });
    });

    test('用户创建 - 角色为空', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建角色为空的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证角色必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('角色不能为空');
      });
    });

    test('角色创建 - 角色名为空', async ({ page }) => {
      await dashboardPage.navigateToRoleManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建角色名为空的角色', async () => {
        await roleManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await roleManagementPage.fillRoleName('');
        await roleManagementPage.fillRoleKey('ROLE_EMPTY');
        await roleManagementPage.clickSaveButton();
      });

      await test.step('验证角色名必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('角色名不能为空');
      });
    });

    test('角色创建 - 角色键为空', async ({ page }) => {
      await dashboardPage.navigateToRoleManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建角色键为空的角色', async () => {
        await roleManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await roleManagementPage.fillRoleName('测试角色');
        await roleManagementPage.fillRoleKey('');
        await roleManagementPage.clickSaveButton();
      });

      await test.step('验证角色键必填验证', async () => {
        await TestHelper.waitForErrorMessage(page);
        const errorMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(errorMessage).toContain('角色键不能为空');
      });
    });
  });

  test.describe('特殊字符和格式测试', () => {
    test('用户名 - 包含中文字符', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建包含中文的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('测试用户');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证中文用户名处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('用户名 - 包含emoji表情', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建包含emoji的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('test😀user');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证emoji用户名处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('密码 - 包含特殊字符', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建包含特殊字符密码的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('P@ssw0rd!#$');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证特殊字符密码处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });

    test('邮箱 - 包含特殊字符', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建包含特殊字符邮箱的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('testuser');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test.user+tag@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证特殊字符邮箱处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });
  });

  test.describe('并发和竞态条件测试', () => {
    test('并发创建相同用户名', async ({ page, context }) => {
      const page1 = page;
      const page2 = await context.newPage();

      await test.step('在两个页面同时创建相同用户名的用户', async () => {
        await page1.goto('/users');
        await page2.goto('/users');

        await TestHelper.waitForPageLoad(page1);
        await TestHelper.waitForPageLoad(page2);

        await page1.click('.create-button');
        await page2.click('.create-button');

        await TestHelper.waitForElementVisible(page1, '.el-dialog');
        await TestHelper.waitForElementVisible(page2, '.el-dialog');

        await page1.fill('input[name="username"]', 'duplicateuser');
        await page2.fill('input[name="username"]', 'duplicateuser');

        await page1.fill('input[name="password"]', 'password123');
        await page2.fill('input[name="password"]', 'password123');

        await page1.fill('input[name="email"]', 'test1@example.com');
        await page2.fill('input[name="email"]', 'test2@example.com');

        await page1.click('.el-dialog__footer button[type="submit"]');
        await page2.click('.el-dialog__footer button[type="submit"]');
      });

      await test.step('验证并发冲突处理', async () => {
        await TestHelper.waitForPageLoad(page1);
        await TestHelper.waitForPageLoad(page2);

        const errorMessage1 = await TestHelper.getElementText(page1, '.el-message__content');
        const errorMessage2 = await TestHelper.getElementText(page2, '.el-message__content');

        expect(errorMessage1 || errorMessage2).toContain('用户名已存在');
      });
    });

    test('快速连续操作', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('快速连续点击创建按钮', async () => {
        for (let i = 0; i < 3; i++) {
          await page.click('.create-button');
          await page.waitForTimeout(100);
        }
      });

      await test.step('验证重复点击处理', async () => {
        const dialogs = await page.locator('.el-dialog').count();
        expect(dialogs).toBe(1);
      });
    });
  });

  test.describe('国际化场景测试', () => {
    test('中文界面操作', async ({ page }) => {
      await test.step('验证中文界面显示', async () => {
        const dashboardTitle = await page.textContent('h1');
        expect(dashboardTitle).toContain('仪表盘');
      });

      await test.step('验证中文按钮文本', async () => {
        const createButton = await page.textContent('.create-button');
        expect(createButton).toContain('创建');
      });

      await test.step('验证中文表单标签', async () => {
        await dashboardPage.navigateToUserManagement();
        await TestHelper.waitForPageLoad(page);

        const usernameLabel = await page.textContent('label[for="username"]');
        expect(usernameLabel).toContain('用户名');
      });
    });

    test('中英文混合输入', async ({ page }) => {
      await dashboardPage.navigateToUserManagement();
      await TestHelper.waitForPageLoad(page);

      await test.step('创建中英文混合用户名的用户', async () => {
        await userManagementPage.clickCreateButton();
        await TestHelper.waitForElementVisible(page, '.el-dialog');

        await userManagementPage.fillUsername('test测试user');
        await userManagementPage.fillPassword('password123');
        await userManagementPage.fillEmail('test@example.com');
        await userManagementPage.selectRole('管理员');
        await userManagementPage.clickSaveButton();
      });

      await test.step('验证中英文混合处理', async () => {
        await TestHelper.waitForSuccessMessage(page);
        const successMessage = await TestHelper.getElementText(page, '.el-message__content');
        expect(successMessage).toContain('创建成功');
      });
    });
  });
});
