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

test.describe('异常日志工作流', () => {
  let exceptionLogPage: ExceptionLogPage;

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

  test('查看异常日志列表', async ({ page }) => {
    await test.step('导航到异常日志页面', async () => {
      await exceptionLogPage.goto();
    });

    await test.step('验证表格显示', async () => {
      await expect(exceptionLogPage.table).toBeVisible({ timeout: 10000 });
    });

    await test.step('验证数据加载', async () => {
      const rowCount = await exceptionLogPage.getLogCount();
      console.log(`异常日志列表包含 ${rowCount} 条记录`);
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

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

    await test.step('输入搜索关键词', async () => {
      const searchKeyword = 'NullPointerException';
      await exceptionLogPage.search(searchKeyword);
    });

    await test.step('验证搜索结果', async () => {
      await page.waitForLoadState('domcontentloaded');
      await page.waitForTimeout(1000);
      const rowCount = await exceptionLogPage.getLogCount();
      console.log(`搜索结果包含 ${rowCount} 条记录`);
    });
  });

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

    await test.step('等待数据加载', async () => {
      await expect(exceptionLogPage.table).toBeVisible({ timeout: 10000 });
    });

    await test.step('点击查看详情按钮', async () => {
      const detailButton = page.locator('button:has-text("详情")').or(page.locator('.detail-button')).first();
      if (await detailButton.isVisible({ timeout: 3000 }).catch(() => false)) {
        await detailButton.click();
        
        await test.step('验证详情对话框显示', async () => {
          const dialog = page.locator('.el-dialog');
          await expect(dialog).toBeVisible({ timeout: 5000 });
          console.log('异常日志详情对话框已打开');
        });
        
        await test.step('关闭详情对话框', async () => {
          await exceptionLogPage.closeDetailDialog();
        });
      } else {
        console.log('当前没有异常日志记录，跳过详情查看测试');
      }
    });
  });
});
