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-ROLE-001: 角色列表加载', 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');
    
    await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
  });

  test('UAT-ROLE-002: 新增角色表单验证', 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 addButton = page.locator('button:has-text("新增")').first();
    if (await addButton.isVisible()) {
      await addButton.click();
      await page.waitForTimeout(500);
      
      await expect(page.locator('.el-dialog')).toBeVisible();
      
      const roleNameInput = page.locator('.el-dialog input[placeholder*="角色名称"]').first();
      if (await roleNameInput.isVisible()) {
        await roleNameInput.fill('测试角色');
        
        const roleKeyInput = page.locator('.el-dialog input[placeholder*="角色标识"]').first();
        if (await roleKeyInput.isVisible()) {
          await roleKeyInput.fill('test_role');
          
          const confirmButton = page.locator('.el-dialog button:has-text("确定")');
          if (await confirmButton.isVisible()) {
            await confirmButton.click();
            await page.waitForTimeout(1000);
          }
        }
      }
    }
  });

  test('UAT-ROLE-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 permissionButton = page.locator('button:has-text("权限")').first();
    if (await permissionButton.isVisible()) {
      await permissionButton.click();
      await page.waitForTimeout(500);
      
      await expect(page.locator('.el-dialog')).toBeVisible();
      
      const tree = page.locator('.el-tree');
      if (await tree.isVisible()) {
        const firstCheckbox = tree.locator('.el-checkbox').first();
        if (await firstCheckbox.isVisible()) {
          await firstCheckbox.click();
        }
      }
    }
  });
});
