import { test, expect } from '../../utils/test-setup';
import testUsers from '../../fixtures/test-users.json' with { type: 'json' };

test.describe('管理后台仪表盘', () => {
  test.beforeEach(async ({ adminLoginPage, dashboardPage }) => {
    // 以管理员身份登录
    await adminLoginPage.goto();
    await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
    await dashboardPage.expectToBeVisible();
  });

  test('仪表盘页面加载', async ({ dashboardPage }) => {
    await dashboardPage.expectToBeVisible();
  });

  test('查看活跃用户统计', async ({ dashboardPage }) => {
    const activeUsersCount = await dashboardPage.getActiveUsersCount();
    expect(activeUsersCount).toBeTruthy();
    expect(parseInt(activeUsersCount) >= 0).toBeTruthy();
  });

  test('查看系统消息统计', async ({ dashboardPage }) => {
    const systemMessagesCount = await dashboardPage.getSystemMessagesCount();
    expect(systemMessagesCount).toBeTruthy();
    expect(parseInt(systemMessagesCount) >= 0).toBeTruthy();
  });

  test('导航到用户管理', async ({ dashboardPage, userManagementPage }) => {
    await dashboardPage.navigateToUserManagement();
    await userManagementPage.expectToBeVisible();
  });

  test.skip('导航到系统设置 - 功能暂未实现', async ({ dashboardPage, page }) => {
    // 系统设置功能暂未实现，跳过此测试
    await dashboardPage.navigateToSystemSettings();
    await expect(page).toHaveURL(/.*settings.*/);
    await expect(page.getByRole('heading', { name: /系统设置|设置/i })).toBeVisible();
  });

  test('仪表盘数据刷新', async ({ dashboardPage, page }) => {
    // 获取初始数据
    await dashboardPage.expectToBeVisible();

    // 刷新页面
    await page.reload();
    await dashboardPage.expectToBeVisible();

    // 验证刷新后数据仍然存在
    const refreshedActiveUsers = await dashboardPage.getActiveUsersCount();
    expect(refreshedActiveUsers).toBeTruthy();
  });

  test('响应式布局 - 桌面端', async ({ dashboardPage, page }) => {
    await page.setViewportSize({ width: 1200, height: 800 });
    await dashboardPage.expectToBeVisible();

    // 验证桌面端布局元素
    await expect(dashboardPage.activeUsersCard).toBeVisible();
    await expect(dashboardPage.systemMessagesCard).toBeVisible();
    await expect(dashboardPage.onlineUsersCard).toBeVisible();
  });

  test('响应式布局 - 平板端', async ({ dashboardPage, page }) => {
    await page.setViewportSize({ width: 768, height: 1024 });
    await dashboardPage.expectToBeVisible();

    // 验证平板端布局
    await expect(dashboardPage.activeUsersCard).toBeVisible();
    await expect(dashboardPage.systemMessagesCard).toBeVisible();
  });

  test('响应式布局 - 移动端', async ({ dashboardPage, page }) => {
    await page.setViewportSize({ width: 375, height: 667 });
    await dashboardPage.expectToBeVisible();

    // 验证移动端布局
    await expect(dashboardPage.pageTitle).toBeVisible();
  });
});