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

test.describe('用户生命周期 E2E 测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
  });

  test('完整用户生命周期：登录 -> 查看用户列表 -> 登出', async ({ page }) => {
    const timestamp = Date.now();

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

    await test.step('2. 查看用户列表', async () => {
      await page.goto('/users');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
      const userCount = await page.locator('.el-table__body tr').count();
      expect(userCount).toBeGreaterThan(0);
    });

    await test.step('3. 用户登出', async () => {
      await loginPage.logout();
      
      await expect(page).toHaveURL(/.*login/);
      const isLoggedOut = !(await loginPage.isLoggedIn());
      expect(isLoggedOut).toBe(true);
    });

    await test.step('4. 验证登出后重新登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      
      await expect(page).toHaveURL(/.*dashboard/);
      const isLoggedIn = await loginPage.isLoggedIn();
      expect(isLoggedIn).toBe(true);
    });
  });

  test('用户登录成功场景：正确密码', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 使用正确密码登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      
      await expect(page).toHaveURL(/.*dashboard/);
      const isLoggedIn = await loginPage.isLoggedIn();
      expect(isLoggedIn).toBe(true);
    });

    await test.step('2. 验证可以访问用户管理页面', async () => {
      await page.goto('/users');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
      const userCount = await page.locator('.el-table__body tr').count();
      expect(userCount).toBeGreaterThan(0);
    });

    await test.step('3. 验证可以访问角色管理页面', async () => {
      await page.goto('/roles');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
      const roleCount = await page.locator('.el-table__body tr').count();
      expect(roleCount).toBeGreaterThan(0);
    });
  });

  test('用户会话管理：验证登录状态持久性', async ({ page }) => {
    await test.step('1. 用户登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      
      await expect(page).toHaveURL(/.*dashboard/);
      const isLoggedIn = await loginPage.isLoggedIn();
      expect(isLoggedIn).toBe(true);
    });

    await test.step('2. 刷新页面验证登录状态', async () => {
      await page.reload();
      await page.waitForLoadState('networkidle');
      
      await expect(page).toHaveURL(/.*dashboard/);
      const isLoggedIn = await loginPage.isLoggedIn();
      expect(isLoggedIn).toBe(true);
    });

    await test.step('3. 导航到不同页面验证登录状态', async () => {
      await page.goto('/users');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
      
      await page.goto('/roles');
      await page.waitForLoadState('networkidle');
      
      const roleTable = page.locator('.el-table').first();
      await expect(roleTable).toBeVisible();
    });
  });

  test('用户导航功能：测试系统菜单导航', async ({ 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 expect(page.locator('.dashboard')).toBeVisible();
    });

    await test.step('3. 导航到用户管理', async () => {
      await page.goto('/users');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
    });

    await test.step('4. 导航到角色管理', async () => {
      await page.goto('/roles');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
    });

    await test.step('5. 导航到菜单管理', async () => {
      await page.goto('/menus');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
    });

    await test.step('6. 导航到文件管理', async () => {
      await page.goto('/files');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
    });

    await test.step('7. 导航到操作日志', async () => {
      await page.goto('/operation-logs');
      await page.waitForLoadState('networkidle');
      
      const table = page.locator('.el-table').first();
      await expect(table).toBeVisible();
    });
  });
});