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

test.describe('登录日志E2E测试', () => {
  let loginPage: LoginPage;
  let loginLogPage: LoginLogPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    loginLogPage = new LoginLogPage(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 loginLogPage.goto();
      await expect(page).toHaveURL(/.*loginlog/);
    });

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

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

    await test.step('搜索登录日志', async () => {
      const keyword = 'admin';
      
      await loginLogPage.searchByKeyword(keyword);
      await loginLogPage.verifyTableContains(keyword);
    });

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

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

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

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

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

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

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

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

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

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

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

    await test.step('按用户名搜索', async () => {
      const username = 'admin';
      await loginLogPage.searchByKeyword(username);
      await loginLogPage.verifyTableContains(username);
    });

    await test.step('按IP地址搜索', async () => {
      const ipAddress = '127.0.0.1';
      await loginLogPage.searchByKeyword(ipAddress);
    });

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

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

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

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

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

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

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

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