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

test.describe('UAT阶段一：核心功能验证', () => {
  test.slow();
  
  test('UAT-AUTH-001: 成功登录流程', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);

    await test.step('访问登录页面', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await expect(page).toHaveTitle(/登录/);
    });

    await test.step('输入用户名和密码', async () => {
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
    });

    await test.step('点击登录按钮', async () => {
      await loginPage.loginButton.click();
    });

    await test.step('验证登录成功', async () => {
      await page.waitForURL('**/dashboard', { timeout: 30000 });
      await page.waitForLoadState('networkidle');
      const username = await dashboardPage.getUsername();
      expect(username).toContain('admin');
    });
  });

  test('UAT-AUTH-002: 登录失败 - 无效凭证', async ({ page }) => {
    const loginPage = new LoginPage(page);

    await test.step('访问登录页面', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await expect(page).toHaveTitle(/登录/);
    });

    await test.step('输入无效凭证', async () => {
      await loginPage.usernameInput.fill('invalid');
      await loginPage.passwordInput.fill('invalid');
      await loginPage.loginButton.click();
    });

    await test.step('验证错误消息显示', async () => {
      await page.waitForTimeout(2000);
      const currentUrl = page.url();
      expect(currentUrl).toContain('/login');
    });

    await test.step('验证保持在登录页面', async () => {
      await expect(page).toHaveURL(/.*login/);
    });
  });

  test('UAT-AUTH-003: 登出流程', async ({ page }) => {
    const loginPage = new LoginPage(page);

    await test.step('登录系统', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
      await loginPage.loginButton.click();
      await page.waitForURL(/.*dashboard/, { timeout: 30000 });
    });

    await test.step('点击用户头像', async () => {
      const avatar = page.locator('.el-avatar');
      await avatar.click();
      await page.waitForSelector('.el-dropdown-menu', { state: 'visible' });
    });

    await test.step('点击退出登录', async () => {
      const logoutButton = page.locator('.el-dropdown-menu').getByText('退出登录');
      await logoutButton.click();
    });

    await test.step('验证跳转到登录页面', async () => {
      await page.waitForURL(/.*login/, { timeout: 30000 });
      await expect(page).toHaveTitle(/登录/);
    });
  });

  test('UAT-NAV-001: 系统管理菜单导航', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);

    await test.step('登录系统', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
      await loginPage.loginButton.click();
      await page.waitForURL(/.*dashboard/, { timeout: 30000 });
    });

    await test.step('点击系统管理菜单', async () => {
      const systemMenu = page.locator('.el-sub-menu').filter({ hasText: '系统管理' });
      await systemMenu.click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('点击用户管理', async () => {
      await dashboardPage.userManagementLink.click();
    });

    await test.step('验证页面跳转', async () => {
      await page.waitForURL(/.*users/, { timeout: 30000 });
      await expect(page).toHaveURL(/.*users/);
    });
  });

  test('UAT-NAV-002: 角色管理菜单导航', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);

    await test.step('登录系统', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
      await loginPage.loginButton.click();
      await page.waitForURL(/.*dashboard/, { timeout: 30000 });
    });

    await test.step('点击系统管理菜单', async () => {
      const systemMenu = page.locator('.el-sub-menu').filter({ hasText: '系统管理' });
      await systemMenu.click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('点击角色管理', async () => {
      await dashboardPage.roleManagementLink.click();
    });

    await test.step('验证页面跳转', async () => {
      await page.waitForURL(/.*roles/, { timeout: 30000 });
      await expect(page).toHaveURL(/.*roles/);
    });
  });

  test('UAT-NAV-003: 菜单管理菜单导航', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);

    await test.step('登录系统', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
      await loginPage.loginButton.click();
      await page.waitForURL(/.*dashboard/, { timeout: 30000 });
    });

    await test.step('点击系统管理菜单', async () => {
      const systemMenu = page.locator('.el-sub-menu').filter({ hasText: '系统管理' });
      await systemMenu.click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('点击菜单管理', async () => {
      await dashboardPage.menuManagementLink.click();
    });

    await test.step('验证页面跳转', async () => {
      await page.waitForURL(/.*menus/, { timeout: 30000 });
      await expect(page).toHaveURL(/.*menus/);
    });
  });

  test('UAT-NAV-004: 系统配置菜单导航', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);

    await test.step('登录系统', async () => {
      await loginPage.goto();
      await page.waitForLoadState('networkidle');
      await loginPage.usernameInput.fill('admin');
      await loginPage.passwordInput.fill('admin123');
      await loginPage.loginButton.click();
      await page.waitForURL(/.*dashboard/, { timeout: 30000 });
    });

    await test.step('点击系统配置菜单', async () => {
      const configMenu = page.locator('.el-sub-menu').filter({ hasText: '系统配置' });
      await configMenu.click();
      await page.waitForLoadState('networkidle');
    });

    await test.step('点击参数配置', async () => {
      await dashboardPage.systemConfigLink.click();
    });

    await test.step('验证页面跳转', async () => {
      await page.waitForURL(/.*sys\/config/, { timeout: 30000 });
      await expect(page).toHaveURL(/.*sys\/config/);
    });
  });
});
