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

test.describe('异常日志 E2E 测试', () => {
  let loginPage: LoginPage;
  let exceptionLogPage: ExceptionLogPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    exceptionLogPage = new ExceptionLogPage(page);

    await loginPage.goto();
    await loginPage.login('admin', 'admin123');
    await expect(page).toHaveURL(/.*dashboard/);
  });

  test.afterEach(async ({ page }) => {
    await loginPage.logout();
  });

  test('EXCEPTION-001: 访问异常日志页面', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
      await expect(page).toHaveURL(/.*exceptionlog/);
    });

    await test.step('验证页面元素可见', async () => {
      await expect(exceptionLogPage.table).toBeVisible();
      await expect(exceptionLogPage.searchInput).toBeVisible();
      await expect(exceptionLogPage.exportButton).toBeVisible();
    });
  });

  test('EXCEPTION-002: 搜索异常日志', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('搜索异常日志', async () => {
      const keyword = 'admin';
      await exceptionLogPage.search(keyword);
      await exceptionLogPage.verifyTableContains(keyword);
    });

    await test.step('清除搜索', async () => {
      await exceptionLogPage.clearSearch();
      const rowCount = await exceptionLogPage.getLogCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('EXCEPTION-003: 异常日志分页功能', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证表格数据加载', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('EXCEPTION-004: 异常日志响应式布局', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证桌面端布局', async () => {
      await page.setViewportSize({ width: 1280, height: 720 });
      await expect(exceptionLogPage.table).toBeVisible();
      await expect(exceptionLogPage.exportButton).toBeVisible();
    });

    await test.step('验证平板端布局', async () => {
      await page.setViewportSize({ width: 768, height: 1024 });
      await expect(exceptionLogPage.table).toBeVisible();
      await expect(exceptionLogPage.exportButton).toBeVisible();
    });

    await test.step('验证移动端布局', async () => {
      await page.setViewportSize({ width: 375, height: 667 });
      await expect(exceptionLogPage.table).toBeVisible();
    });
  });

  test('EXCEPTION-005: 异常日志数据验证', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证日志数据完整性', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });

    await test.step('验证日志字段显示', async () => {
      await expect(exceptionLogPage.table).toBeVisible();
    });
  });

  test('EXCEPTION-006: 异常日志搜索功能', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('按用户名搜索', async () => {
      const operator = 'admin';
      await exceptionLogPage.search(operator);
      await exceptionLogPage.verifyTableContains(operator);
    });

    await test.step('按异常信息搜索', async () => {
      const exceptionInfo = 'Exception';
      await exceptionLogPage.search(exceptionInfo);
    });

    await test.step('清除搜索结果', async () => {
      await exceptionLogPage.clearSearch();
      const rowCount = await exceptionLogPage.getLogCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('EXCEPTION-007: 异常日志导出功能', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('导出异常日志', async () => {
      const downloadPromise = page.waitForEvent('download');
      await exceptionLogPage.exportData();
      const download = await downloadPromise;
      expect(download).toBeDefined();
    });
  });

  test('EXCEPTION-008: 异常日志时间范围验证', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证日志时间戳显示', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      if (rowCount > 0) {
        await expect(exceptionLogPage.table).toBeVisible();
      }
    });
  });

  test('EXCEPTION-009: 异常日志权限验证', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证导出按钮可见性', async () => {
      await expect(exceptionLogPage.exportButton).toBeVisible();
    });

    await test.step('验证搜索功能可用', async () => {
      await expect(exceptionLogPage.searchInput).toBeVisible();
      await expect(exceptionLogPage.searchButton).toBeVisible();
    });
  });

  test('EXCEPTION-010: 异常日志详情查看', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证日志详情显示', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      if (rowCount > 0) {
        await expect(exceptionLogPage.table).toBeVisible();
      }
    });
  });

  test('EXCEPTION-011: 异常日志刷新功能', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('刷新异常日志', async () => {
      await exceptionLogPage.refresh();
      await expect(exceptionLogPage.table).toBeVisible();
    });
  });

  test('EXCEPTION-012: 异常日志排序功能', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证表格排序功能', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      if (rowCount > 0) {
        await expect(exceptionLogPage.table).toBeVisible();
      }
    });
  });

  test('EXCEPTION-013: 异常日志空状态显示', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('搜索不存在的异常', async () => {
      await exceptionLogPage.search('nonexistent_exception_123456');
      await page.waitForTimeout(1000);
    });

    await test.step('验证空状态显示', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      expect(rowCount).toBe(0);
    });
  });

  test('EXCEPTION-014: 异常日志批量操作', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证批量操作按钮可见性', async () => {
      await expect(exceptionLogPage.table).toBeVisible();
    });
  });

  test('EXCEPTION-015: 异常日志详细信息验证', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证异常日志包含必要信息', async () => {
      await expect(exceptionLogPage.table).toBeVisible();
    });
  });
});
