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

test.describe('UAT权限分配流程测试', () => {
  test('UAT-PERM-001: 权限分配完整流程', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const roleManagementPage = new RoleManagementPage(page);
    const userManagementPage = new UserManagementPage(page);

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

    await test.step('2. 创建新角色', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.clickCreateRole();
      
      const timestamp = Date.now();
      const roleData = {
        roleName: `UAT角色_${timestamp}`,
        roleKey: `uat_role_${timestamp}`,
        roleSort: '1',
        status: '1',
        remark: 'UAT测试角色'
      };
      
      await roleManagementPage.fillRoleForm(roleData);
      await roleManagementPage.submitForm();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 为角色分配权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('system:user:view');
      await roleManagementPage.selectPermission('system:user:add');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 为用户分配角色', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickEditButton(1);
      await userManagementPage.selectRole('UAT角色');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });
  });

  test('UAT-PERM-002: 角色权限验证', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const roleManagementPage = new RoleManagementPage(page);

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

    await test.step('2. 导航到角色管理', async () => {
      await dashboardPage.navigateToRoleManagement();
      await expect(roleManagementPage.table).toBeVisible();
    });

    await test.step('3. 验证角色权限显示', async () => {
      await roleManagementPage.clickPermissionButton(1);
      await expect(page.locator('.permission-dialog')).toBeVisible();
      
      const permissionCount = await page.locator('.permission-checkbox').count();
      expect(permissionCount).toBeGreaterThan(0);
    });
  });

  test('UAT-PERM-003: 权限撤销流程', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const roleManagementPage = new RoleManagementPage(page);

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

    await test.step('2. 导航到角色管理', async () => {
      await dashboardPage.navigateToRoleManagement();
      await expect(roleManagementPage.table).toBeVisible();
    });

    await test.step('3. 撤销角色权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.deselectPermission('system:user:delete');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });
  });
});