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 { MenuManagementPage } from './pages/MenuManagementPage';
import { SystemConfigPage } from './pages/SystemConfigPage';
import { FileManagementPage } from './pages/FileManagementPage';
import { OperationLogPage } from './pages/OperationLogPage';
import { NotificationPage } from './pages/NotificationPage';
import { DictionaryManagementPage } from './pages/DictionaryManagementPage';
import { TestDataCleanup } from './utils/TestDataCleanup';

test.describe('UAT用户验收测试', () => {
  let testDataCleanup: TestDataCleanup;

  test.beforeEach(async ({ page }) => {
    testDataCleanup = new TestDataCleanup(page);
  });

  test.afterEach(async ({ page }) => {
    await testDataCleanup.cleanupAll();
  });

  test('UAT-001: 用户注册与首次登录场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const userManagementPage = new UserManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建新用户账号', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();

      const userData = {
        username: `newuser_${timestamp}`,
        nickname: `新员工${timestamp}`,
        email: `newuser_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };

      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackUser(userData.username);
    });

    await test.step('3. 设置初始密码', async () => {
      await userManagementPage.editUser(1);
      const dialog = page.locator('.el-dialog');
      const passwordInput = dialog.locator('.el-form-item').filter({ hasText: '密码' }).locator('input[type="password"]');
      await passwordInput.fill('NewPass123!@#');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 分配基本角色', async () => {
      await userManagementPage.editUser(1);
      await page.click('.role-select');
      await page.click('option:has-text("普通用户")');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 新用户使用初始密码登录', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`newuser_${timestamp}`, 'NewPass123!@#');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('6. 验证密码修改提示', async () => {
      await expect(page.locator('.password-change-notice')).toBeVisible();
    });

    await test.step('7. 修改密码', async () => {
      await dashboardPage.navigateToProfile();
      await page.fill('input[name="oldPassword"]', 'NewPass123!@#');
      await page.fill('input[name="newPassword"]', 'FinalPass123!@#');
      await page.fill('input[name="confirmPassword"]', 'FinalPass123!@#');
      await page.click('button[type="submit"]');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('8. 验证登录成功', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`newuser_${timestamp}`, 'FinalPass123!@#');
      await expect(page).toHaveURL(/.*dashboard/);
      const username = await dashboardPage.getUsername();
      expect(username).toContain(`newuser_${timestamp}`);
    });

    await test.step('9. 查看欢迎信息', async () => {
      await expect(page.locator('.welcome-message')).toBeVisible();
      await expect(page.locator('.welcome-message')).toContainText('欢迎');
    });

    await test.step('10. 查看系统通知', async () => {
      await dashboardPage.navigateToNotification();
      await expect(page.locator('.notification-list')).toBeVisible();
    });
  });

  test('UAT-002: 用户信息管理场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const userManagementPage = new UserManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 用户登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 导航到个人信息页面', async () => {
      await dashboardPage.navigateToProfile();
      await expect(page.locator('.profile-form')).toBeVisible();
    });

    await test.step('3. 查看当前信息', async () => {
      const currentUsername = await page.locator('input[name="username"]').inputValue();
      expect(currentUsername).toBe('admin');
    });

    await test.step('4. 修改昵称', async () => {
      await page.fill('input[name="nickname"]', `管理员_${timestamp}`);
      await page.click('button[type="submit"]');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('5. 修改邮箱', async () => {
      await page.fill('input[name="email"]', `admin_${timestamp}@example.com`);
      await page.click('button[type="submit"]');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('6. 修改手机号', async () => {
      await page.fill('input[name="phone"]', '13900139000');
      await page.click('button[type="submit"]');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('7. 上传头像', async () => {
      const fileInput = page.locator('input[type="file"]');
      await fileInput.setInputFiles('./e2e/fixtures/test-file.txt');
      await page.click('button[type="submit"]');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('8. 保存修改', async () => {
      await page.click('button:has-text("保存")');
      await expect(page.locator('.success-message')).toBeVisible();
    });

    await test.step('9. 验证信息更新', async () => {
      await page.reload();
      await expect(page.locator('input[name="nickname"]')).toHaveValue(`管理员_${timestamp}`);
    });

    await test.step('10. 查看操作日志', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(page.locator('table')).toContainText('个人信息');
    });
  });

  test('UAT-003: 角色权限分配场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const roleManagementPage = new RoleManagementPage(page);
    const userManagementPage = new UserManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建新角色', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.clickCreateRole();

      const roleData = {
        roleName: `业务角色_${timestamp}`,
        roleKey: `business_role_${timestamp}`,
        roleSort: '1',
        status: '1',
        remark: `业务操作角色_${timestamp}`,
      };

      await roleManagementPage.fillRoleForm(roleData);
      await roleManagementPage.submitForm();
      await expect(roleManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackRole(roleData.roleKey);
    });

    await test.step('3. 配置角色基本信息', async () => {
      await roleManagementPage.editRole(1);
      await page.fill('input[name="remark"]', `更新备注_${timestamp}`);
      await roleManagementPage.submitForm();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 分配菜单权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('system:user:view');
      await roleManagementPage.selectPermission('system:user:add');
      await roleManagementPage.selectPermission('system:user:edit');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 分配API权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('api:user:list');
      await roleManagementPage.selectPermission('api:user:create');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('6. 保存角色配置', async () => {
      await roleManagementPage.saveRole();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 为用户分配角色', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.editUser(1);
      await page.click('.role-select');
      await page.click('option:has-text("业务角色")');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('8. 用户重新登录', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('9. 验证权限生效', async () => {
      await dashboardPage.navigateToUserManagement();
      await expect(page).toHaveURL(/.*users/);
      await expect(page.locator('button:has-text("新增")')).toBeVisible();
    });

    await test.step('10. 查看权限日志', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(page.locator('table')).toContainText('权限');
    });
  });

  test('UAT-004: 菜单管理场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const menuManagementPage = new MenuManagementPage(page);
    const roleManagementPage = new RoleManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建父级菜单', async () => {
      await dashboardPage.navigateToMenuManagement();
      await menuManagementPage.clickCreateMenu();

      const menuData = {
        menuName: `业务菜单_${timestamp}`,
        parentId: '0',
        orderNum: '1',
        menuType: 'M',
        component: `business_${timestamp}`,
        perms: `business:view_${timestamp}`,
        status: '1',
      };

      await menuManagementPage.fillMenuForm(menuData);
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackMenu(`business_${timestamp}`);
    });

    await test.step('3. 创建子级菜单', async () => {
      await menuManagementPage.clickCreateMenu();

      const menuData = {
        menuName: `业务操作_${timestamp}`,
        parentId: '1',
        orderNum: '1',
        menuType: 'C',
        component: `business_operation_${timestamp}`,
        perms: `business:operation_${timestamp}`,
        status: '1',
      };

      await menuManagementPage.fillMenuForm(menuData);
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 配置菜单权限', async () => {
      await menuManagementPage.editMenu(1);
      await menuManagementPage.selectPermission('menu:view');
      await menuManagementPage.selectPermission('menu:edit');
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 保存菜单配置', async () => {
      await menuManagementPage.saveMenu();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('6. 为角色分配菜单权限', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission(`business:view_${timestamp}`);
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 用户登录系统', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('8. 验证菜单显示', async () => {
      await expect(page.locator('.menu-item')).toContainText(`业务菜单_${timestamp}`);
    });

    await test.step('9. 验证菜单访问', async () => {
      await page.click(`text=业务菜单_${timestamp}`);
      await expect(page).toHaveURL(/.*business/);
    });

    await test.step('10. 验证菜单结构', async () => {
      await dashboardPage.navigateToMenuManagement();
      await expect(page.locator('table')).toContainText(`业务菜单_${timestamp}`);
      await expect(page.locator('table')).toContainText(`业务操作_${timestamp}`);
    });
  });

  test('UAT-005: 文件管理场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const fileManagementPage = new FileManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 用户登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 导航到文件管理页面', async () => {
      await dashboardPage.navigateToFileManagement();
      await expect(fileManagementPage.table).toBeVisible();
    });

    await test.step('3. 上传文件', async () => {
      await fileManagementPage.clickUploadFile();
      const fileInput = page.locator('input[type="file"]');
      await fileInput.setInputFiles('./e2e/fixtures/test-file.txt');
      await fileManagementPage.submitUpload();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 验证文件上传成功', async () => {
      await expect(page.locator('table')).toContainText('test-file.txt');
    });

    await test.step('5. 预览文件', async () => {
      await fileManagementPage.previewFile(1);
      await expect(page.locator('.file-preview')).toBeVisible();
      await expect(page.locator('.file-preview')).toContainText('test');
    });

    await test.step('6. 下载文件', async () => {
      const downloadPromise = page.waitForEvent('download');
      await fileManagementPage.downloadFile(1);
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toBe('test-file.txt');
    });

    await test.step('7. 验证文件内容', async () => {
      await fileManagementPage.previewFile(1);
      const content = await page.locator('.file-preview').textContent();
      expect(content).toContain('test');
    });

    await test.step('8. 设置文件权限', async () => {
      await fileManagementPage.editFile(1);
      await page.selectOption('select[name="permission"]', 'private');
      await fileManagementPage.submitForm();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('9. 删除文件', async () => {
      await fileManagementPage.deleteFile(1);
      await fileManagementPage.confirmDelete();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('10. 验证文件删除', async () => {
      await page.reload();
      await expect(page.locator('table')).not.toContainText('test-file.txt');
    });
  });

  test('UAT-006: 系统配置管理场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const systemConfigPage = new SystemConfigPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 导航到系统配置页面', async () => {
      await dashboardPage.navigateToSystemConfig();
      await expect(systemConfigPage.table).toBeVisible();
    });

    await test.step('3. 查看当前配置', async () => {
      const configCount = await page.locator('table tbody tr').count();
      expect(configCount).toBeGreaterThan(0);
    });

    await test.step('4. 修改配置项', async () => {
      await systemConfigPage.editConfig(1);
      await page.fill('input[name="configValue"]', `test_config_${timestamp}`);
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('5. 验证配置有效性', async () => {
      await systemConfigPage.editConfig(1);
      await expect(page.locator('input[name="configValue"]')).toHaveValue(`test_config_${timestamp}`);
    });

    await test.step('6. 保存配置', async () => {
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('7. 验证配置生效', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`test_config_${timestamp}`);
    });

    await test.step('8. 刷新配置缓存', async () => {
      await systemConfigPage.refreshCache();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('9. 查看配置日志', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(page.locator('table')).toContainText('配置');
    });

    await test.step('10. 恢复默认配置', async () => {
      await dashboardPage.navigateToSystemConfig();
      await systemConfigPage.editConfig(1);
      await page.fill('input[name="configValue"]', 'default_value');
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });
  });

  test('UAT-007: 审计日志查询场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const operationLogPage = new OperationLogPage(page);

    await test.step('1. 审计员登录系统', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 导航到审计日志页面', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(operationLogPage.table).toBeVisible();
    });

    await test.step('3. 查看操作日志', async () => {
      await expect(page.locator('table')).toContainText('操作');
    });

    await test.step('4. 查看登录日志', async () => {
      await operationLogPage.switchToLoginLog();
      await expect(page.locator('table')).toContainText('登录');
    });

    await test.step('5. 查看异常日志', async () => {
      await operationLogPage.switchToExceptionLog();
      await expect(operationLogPage.table).toBeVisible();
    });

    await test.step('6. 搜索日志', async () => {
      await operationLogPage.search('admin');
      await page.waitForTimeout(2000);
      await expect(page.locator('table')).toContainText('admin');
    });

    await test.step('7. 导出日志', async () => {
      const downloadPromise = page.waitForEvent('download');
      await operationLogPage.exportLogs();
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toMatch(/logs.*\.xlsx/);
    });

    await test.step('8. 验证日志准确性', async () => {
      const logCount = await page.locator('table tbody tr').count();
      expect(logCount).toBeGreaterThan(0);
    });

    await test.step('9. 生成审计报告', async () => {
      await operationLogPage.generateReport();
      await expect(operationLogPage.successMessage).toBeVisible();
    });

    await test.step('10. 验证报告内容', async () => {
      await expect(page.locator('.report-content')).toBeVisible();
    });
  });

  test('UAT-008: 通知中心使用场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const notificationPage = new NotificationPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员发布系统通知', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToNotification();
      await notificationPage.clickCreateNotification();

      const notificationData = {
        title: `系统通知_${timestamp}`,
        content: `这是一条重要的系统通知_${timestamp}`,
        type: 'system',
        status: '1',
      };

      await notificationPage.fillNotificationForm(notificationData);
      await notificationPage.submitForm();
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('2. 用户登录系统', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('3. 查看通知列表', async () => {
      await dashboardPage.navigateToNotification();
      await expect(page.locator('.notification-list')).toBeVisible();
      await expect(page.locator('.notification-list')).toContainText(`系统通知_${timestamp}`);
    });

    await test.step('4. 查看通知详情', async () => {
      await notificationPage.viewNotification(1);
      await expect(page.locator('.notification-detail')).toBeVisible();
      await expect(page.locator('.notification-detail')).toContainText(`系统通知_${timestamp}`);
    });

    await test.step('5. 标记通知已读', async () => {
      await notificationPage.markAsRead(1);
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('6. 验证通知状态', async () => {
      await page.reload();
      await expect(page.locator('.notification-item.read')).toBeVisible();
    });

    await test.step('7. 删除通知', async () => {
      await notificationPage.deleteNotification(1);
      await notificationPage.confirmDelete();
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('8. 验证通知删除', async () => {
      await page.reload();
      await expect(page.locator('.notification-list')).not.toContainText(`系统通知_${timestamp}`);
    });

    await test.step('9. 验证通知推送', async () => {
      await notificationPage.clickCreateNotification();
      const notificationData = {
        title: `推送通知_${timestamp}`,
        content: `这是一条推送通知_${timestamp}`,
        type: 'push',
        status: '1',
      };
      await notificationPage.fillNotificationForm(notificationData);
      await notificationPage.submitForm();
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('10. 查看通知历史', async () => {
      await page.reload();
      await expect(page.locator('.notification-list')).toContainText(`推送通知_${timestamp}`);
    });
  });

  test('UAT-009: 字典数据使用场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const dictionaryManagementPage = new DictionaryManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 管理员配置字典数据', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToDictionary();
      await dictionaryManagementPage.clickCreateDictType();

      const dictTypeData = {
        dictName: `业务字典_${timestamp}`,
        dictType: `business_dict_${timestamp}`,
        status: '1',
        remark: `业务字典类型_${timestamp}`,
      };

      await dictionaryManagementPage.fillDictTypeForm(dictTypeData);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackDictType(`business_dict_${timestamp}`);
    });

    await test.step('2. 用户登录系统', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('3. 查看字典数据', async () => {
      await dashboardPage.navigateToDictionary();
      await expect(page.locator('table')).toContainText(`业务字典_${timestamp}`);
    });

    await test.step('4. 使用字典数据', async () => {
      await dictionaryManagementPage.clickCreateDictData();
      const dictData = {
        dictLabel: `业务数据1_${timestamp}`,
        dictValue: `business_value1_${timestamp}`,
        dictSort: '1',
        status: '1',
      };
      await dictionaryManagementPage.fillDictDataForm(dictData);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 验证数据正确性', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`业务数据1_${timestamp}`);
    });

    await test.step('6. 管理员更新字典数据', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToDictionary();
      await dictionaryManagementPage.editDictData(1);
      await page.fill('input[name="dictLabel"]', `更新数据_${timestamp}`);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 用户刷新页面', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`更新数据_${timestamp}`);
    });

    await test.step('8. 验证数据更新', async () => {
      await expect(page.locator('table')).toContainText(`更新数据_${timestamp}`);
    });

    await test.step('9. 验证数据缓存', async () => {
      await dictionaryManagementPage.refreshCache();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('10. 验证数据一致性', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`更新数据_${timestamp}`);
    });
  });

  test('UAT-010: 多用户协作场景', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const userManagementPage = new UserManagementPage(page);
    const timestamp = Date.now();

    await test.step('1. 创建测试用户A', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `user_a_${timestamp}`,
        nickname: `用户A_${timestamp}`,
        email: `user_a_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackUser(`user_a_${timestamp}`);
    });

    await test.step('2. 创建测试用户B', async () => {
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `user_b_${timestamp}`,
        nickname: `用户B_${timestamp}`,
        email: `user_b_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();

      testDataCleanup.trackUser(`user_b_${timestamp}`);
    });

    await test.step('3. 多用户同时登录', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('4. 用户A创建数据', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `data_a_${timestamp}`,
        nickname: `数据A_${timestamp}`,
        email: `data_a_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 用户B同时创建数据', async () => {
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `data_b_${timestamp}`,
        nickname: `数据B_${timestamp}`,
        email: `data_b_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('6. 验证数据一致性', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`data_a_${timestamp}`);
      await expect(page.locator('table')).toContainText(`data_b_${timestamp}`);
    });

    await test.step('7. 验证并发处理', async () => {
      const userCount = await userManagementPage.getUserCount();
      expect(userCount).toBeGreaterThanOrEqual(2);
    });

    await test.step('8. 查看操作日志', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(page.locator('table')).toContainText('创建');
    });

    await test.step('9. 验证日志完整性', async () => {
      const logCount = await page.locator('table tbody tr').count();
      expect(logCount).toBeGreaterThan(0);
    });

    await test.step('10. 清理测试数据', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.search(`user_a_${timestamp}`);
      await page.waitForTimeout(1000);
      const rows = await page.locator('table tbody tr').count();
      if (rows > 0) {
        await userManagementPage.deleteUser(1);
        await userManagementPage.confirmDelete();
      }
    });
  });
});