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

test.describe('操作日志E2E测试', () => {
  let loginPage: LoginPage;
  let operationLogPage: OperationLogPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    operationLogPage = new OperationLogPage(page);

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

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

  test('操作日志页面导航', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
      await expect(page).toHaveURL(/.*oplog/);
    });

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

  test('搜索操作日志', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

    await test.step('搜索操作日志', async () => {
      const keyword = 'admin';
      
      await operationLogPage.searchByKeyword(keyword);
      await operationLogPage.verifyTableContains(keyword);
    });

    await test.step('清除搜索', async () => {
      await operationLogPage.clearSearch();
      const rowCount = await operationLogPage.getTableRowCount();
      expect(rowCount).toBeGreaterThan(0);
    });
  });

  test('操作日志分页功能', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

  test('操作日志响应式布局', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

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

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

  test('操作日志数据验证', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

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

  test('操作日志搜索功能', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

    await test.step('按操作人搜索', async () => {
      const operator = 'admin';
      await operationLogPage.searchByKeyword(operator);
      await operationLogPage.verifyTableContains(operator);
    });

    await test.step('按操作模块搜索', async () => {
      const module = '用户管理';
      await operationLogPage.searchByKeyword(module);
    });

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

  test('操作日志导出功能', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

  test('操作日志时间范围验证', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

  test('操作日志权限验证', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

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

  test('操作日志详情查看', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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

  test('操作日志排序功能', async ({ page }) => {
    await test.step('导航到操作日志页面', async () => {
      await operationLogPage.goto();
    });

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