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

test.describe('审计功能 E2E 测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let operationLogPage: OperationLogPage;
  let loginLogPage: LoginLogPage;

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

  test('AUDIT-001: 管理员查看操作日志', async ({ page }) => {
    await test.step('管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

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

    await test.step('验证操作日志页面加载', async () => {
      await operationLogPage.goto();
      await expect(operationLogPage.table).toBeVisible();
      const rowCount = await operationLogPage.getTableRowCount();
      expect(rowCount).toBeGreaterThan(0);
    });

    await test.step('验证日志表格包含必要列', async () => {
      await expect(operationLogPage.table).toContainText('ID');
      await expect(operationLogPage.table).toContainText('操作人');
      await expect(operationLogPage.table).toContainText('操作模块');
      await expect(operationLogPage.table).toContainText('请求方法');
    });
  });

  test('AUDIT-002: 按关键词搜索操作日志', async ({ page }) => {
    await test.step('管理员登录并导航到操作日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await operationLogPage.goto();
    });

    await test.step('搜索特定操作人', async () => {
      await operationLogPage.searchByKeyword('admin');
      await page.waitForTimeout(1000);
      await operationLogPage.verifyTableContains('admin');
    });

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

  test('AUDIT-003: 导出操作日志', async ({ page }) => {
    await test.step('管理员登录并导航到操作日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await operationLogPage.goto();
    });

    await test.step('导出操作日志数据', async () => {
      const downloadPromise = page.waitForEvent('download');
      await operationLogPage.exportData();
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toMatch(/\.(xlsx|csv)$/);
    });
  });

  test('AUDIT-004: 管理员查看登录日志', async ({ page }) => {
    await test.step('管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('导航到登录日志页面', async () => {
      await page.goto('/loginlog');
      await page.waitForLoadState('networkidle');
    });

    await test.step('验证登录日志页面加载', async () => {
      await loginLogPage.goto();
      await expect(loginLogPage.table).toBeVisible();
      const rowCount = await loginLogPage.getTableRowCount();
      expect(rowCount).toBeGreaterThan(0);
    });

    await test.step('验证登录日志表格包含必要列', async () => {
      await expect(loginLogPage.table).toContainText('ID');
      await expect(loginLogPage.table).toContainText('用户名');
      await expect(loginLogPage.table).toContainText('IP地址');
      await expect(loginLogPage.table).toContainText('登录状态');
    });
  });

  test('AUDIT-005: 按IP地址搜索登录日志', async ({ page }) => {
    await test.step('管理员登录并导航到登录日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await loginLogPage.goto();
    });

    await test.step('搜索特定IP地址', async () => {
      await loginLogPage.searchByKeyword('127.0.0.1');
      await page.waitForTimeout(1000);
      await loginLogPage.verifyTableContains('127.0.0.1');
    });

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

  test('AUDIT-006: 导出登录日志', async ({ page }) => {
    await test.step('管理员登录并导航到登录日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await loginLogPage.goto();
    });

    await test.step('导出登录日志数据', async () => {
      const downloadPromise = page.waitForEvent('download');
      await loginLogPage.exportData();
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toMatch(/\.(xlsx|csv)$/);
    });
  });

  test('AUDIT-007: 验证审计权限控制', async ({ page }) => {
    await test.step('普通用户登录', async () => {
      await loginPage.goto();
      await loginPage.login('user', 'user123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('尝试访问操作日志页面', async () => {
      await page.goto('/oplog');
      await page.waitForLoadState('networkidle');
      
      const currentURL = page.url();
      if (currentURL.includes('/oplog')) {
        await expect(operationLogPage.table).toBeVisible();
      } else {
        await expect(page).toHaveURL(/.*dashboard/);
      }
    });
  });

  test('AUDIT-008: 验证操作日志时间排序', async ({ page }) => {
    await test.step('管理员登录并导航到操作日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await operationLogPage.goto();
    });

    await test.step('验证日志按时间倒序排列', async () => {
      const firstRow = operationLogPage.table.locator('.el-table__row').first();
      await expect(firstRow).toBeVisible();
    });
  });

  test('AUDIT-009: 验证登录日志状态显示', async ({ page }) => {
    await test.step('管理员登录并导航到登录日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await loginLogPage.goto();
    });

    await test.step('验证登录状态列显示', async () => {
      await expect(loginLogPage.table).toContainText('成功');
    });
  });

  test('AUDIT-010: 验证审计日志数据完整性', async ({ page }) => {
    await test.step('管理员登录并导航到操作日志', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await operationLogPage.goto();
    });

    await test.step('验证操作日志包含完整信息', async () => {
      await expect(operationLogPage.table).toContainText('操作时间');
      await expect(operationLogPage.table).toContainText('请求参数');
      await expect(operationLogPage.table).toContainText('返回结果');
    });
  });
});