import { test, expect } from '@playwright/test';

test.describe('UAT阶段六：数据持久化验证', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/');
    await page.waitForLoadState('networkidle');
    
    const usernameInput = page.locator('input[type="text"]').first();
    const passwordInput = page.locator('input[type="password"]').first();
    const loginButton = page.locator('button:has-text("登录")');
    
    await usernameInput.fill('admin');
    await passwordInput.fill('admin123');
    await loginButton.click();
    
    await page.waitForURL('**/dashboard', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
  });

  test('UAT-PERSIST-001: 角色创建持久化验证', async ({ page }) => {
    const timestamp = Date.now();
    const roleName = `测试角色_${timestamp}`;
    const roleKey = `test_role_${timestamp}`;
    
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=角色管理');
    await page.waitForURL('**/roles', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const addButton = page.locator('button:has-text("新增")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      const roleNameInput = page.locator('.el-dialog input[placeholder*="角色名称"]').first();
      await roleNameInput.fill(roleName);
      
      const roleKeyInput = page.locator('.el-dialog input[placeholder*="角色标识"]').first();
      await roleKeyInput.fill(roleKey);
      
      const confirmButton = page.locator('.el-dialog button:has-text("确定")');
      await confirmButton.click();
      await page.waitForTimeout(1000);
      
      await page.reload();
      await page.waitForLoadState('networkidle');
      await page.waitForTimeout(1000);
      
      const createdRole = page.locator(`text=${roleName}`);
      await expect(createdRole).toBeVisible({ timeout: 5000 });
    }
  });

  test('UAT-PERSIST-002: 用户创建持久化验证', async ({ page }) => {
    const timestamp = Date.now();
    const username = `testuser_${timestamp}`;
    
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=用户管理');
    await page.waitForURL('**/users', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const addButton = page.locator('button:has-text("新增用户")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      const usernameInput = page.locator('.el-dialog input[placeholder*="用户名"]').first();
      await usernameInput.fill(username);
      
      const nicknameInput = page.locator('.el-dialog input[placeholder*="昵称"]').first();
      await nicknameInput.fill(`测试用户_${timestamp}`);
      
      const emailInput = page.locator('.el-dialog input[placeholder*="邮箱"]').first();
      await emailInput.fill(`${username}@test.com`);
      
      const phoneInput = page.locator('.el-dialog input[placeholder*="手机"]').first();
      await phoneInput.fill('13800138000');
      
      const passwordInput = page.locator('.el-dialog input[placeholder*="密码"]').first();
      await passwordInput.fill('Test123456');
      
      const confirmButton = page.locator('.el-dialog button:has-text("确定")');
      await confirmButton.click();
      await page.waitForTimeout(1000);
      
      await page.reload();
      await page.waitForLoadState('networkidle');
      await page.waitForTimeout(1000);
      
      const searchInput = page.locator('input[placeholder*="搜索"]').first();
      if (await searchInput.isVisible()) {
        await searchInput.fill(username);
        await page.waitForTimeout(1000);
        
        const createdUser = page.locator(`text=${username}`);
        await expect(createdUser).toBeVisible({ timeout: 5000 });
      }
    }
  });

  test('UAT-PERSIST-003: 数据更新持久化验证', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=角色管理');
    await page.waitForURL('**/roles', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const editButton = page.locator('button:has-text("编辑")').first();
    if (await editButton.isVisible()) {
      await editButton.click();
      await page.waitForTimeout(500);
      
      const roleNameInput = page.locator('.el-dialog input[placeholder*="角色名称"]').first();
      const currentValue = await roleNameInput.inputValue();
      const newValue = `${currentValue}_已修改_${Date.now()}`;
      
      await roleNameInput.fill(newValue);
      
      const confirmButton = page.locator('.el-dialog button:has-text("确定")');
      await confirmButton.click();
      await page.waitForTimeout(1000);
      
      await page.reload();
      await page.waitForLoadState('networkidle');
      await page.waitForTimeout(1000);
      
      const updatedRole = page.locator(`text=${newValue}`);
      await expect(updatedRole).toBeVisible({ timeout: 5000 });
    }
  });

  test('UAT-PERSIST-004: 数据删除持久化验证', async ({ page }) => {
    const timestamp = Date.now();
    const roleName = `待删除角色_${timestamp}`;
    
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=角色管理');
    await page.waitForURL('**/roles', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    const addButton = page.locator('button:has-text("新增")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      const roleNameInput = page.locator('.el-dialog input[placeholder*="角色名称"]').first();
      await roleNameInput.fill(roleName);
      
      const roleKeyInput = page.locator('.el-dialog input[placeholder*="角色标识"]').first();
      await roleKeyInput.fill(`delete_test_${timestamp}`);
      
      const confirmButton = page.locator('.el-dialog button:has-text("确定")');
      await confirmButton.click();
      await page.waitForTimeout(1000);
      
      const createdRole = page.locator(`text=${roleName}`);
      await createdRole.scrollIntoViewIfNeeded();
      
      const deleteButton = page.locator(`tr:has-text("${roleName}") button:has-text("删除")`).first();
      if (await deleteButton.isVisible()) {
        await deleteButton.click();
        await page.waitForTimeout(500);
        
        const confirmDeleteButton = page.locator('.el-message-box button:has-text("确定")');
        if (await confirmDeleteButton.isVisible()) {
          await confirmDeleteButton.click();
          await page.waitForTimeout(1000);
          
          await page.reload();
          await page.waitForLoadState('networkidle');
          await page.waitForTimeout(1000);
          
          const deletedRole = page.locator(`text=${roleName}`);
          await expect(deletedRole).not.toBeVisible({ timeout: 5000 });
        }
      }
    }
  });
});
