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

test.describe('通知公告E2E测试', () => {
  let loginPage: LoginPage;
  let noticePage: NotificationPage;

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

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

  test('创建通知公告', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('创建新通知公告', async () => {
      const title = `测试通知_${Date.now()}`;
      const content = `这是一条测试通知内容_${Date.now()}`;
      
      await noticePage.addNotification(title, content);
      
      await noticePage.verifyTableContains(title);
    });
  });

  test('编辑通知公告', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('编辑现有通知公告', async () => {
      const title = '系统维护通知';
      const newContent = `系统将于今晚进行维护，请提前保存工作_${Date.now()}`;
      
      await noticePage.editNotification(title, newContent);
      
      await noticePage.verifyTableContains(title);
    });
  });

  test('删除通知公告', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('删除通知公告', async () => {
      const title = `测试通知_${Date.now()}`;
      const content = `这是一条测试通知内容_${Date.now()}`;
      
      await noticePage.addNotification(title, content);
      await noticePage.verifyTableContains(title);
      
      await noticePage.deleteNotification(title);
      await noticePage.verifyTableNotContains(title);
    });
  });

  test('搜索通知公告', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('搜索通知公告', async () => {
      const title = '系统维护通知';
      
      await noticePage.searchNotification(title);
      await noticePage.verifyTableContains(title);
    });

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

  test('通知公告分页功能', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

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

  test('通知公告响应式布局', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

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

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

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

  test('通知公告权限验证', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('验证添加按钮可见性', async () => {
      await expect(noticePage.addButton).toBeVisible();
    });

    await test.step('验证编辑和删除按钮可见性', async () => {
      const rows = await noticePage.table.locator('.el-table__row').count();
      if (rows > 0) {
        await expect(noticePage.table).toBeVisible();
      }
    });
  });

  test('通知公告状态管理', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('创建已发布通知', async () => {
      const title = `已发布通知_${Date.now()}`;
      const content = `这是一条已发布的通知_${Date.now()}`;
      
      await noticePage.addNotification(title, content, '1', '0');
      await noticePage.verifyTableContains(title);
    });

    await test.step('创建草稿通知', async () => {
      const title = `草稿通知_${Date.now()}`;
      const content = `这是一条草稿通知_${Date.now()}`;
      
      await noticePage.addNotification(title, content, '1', '1');
      await noticePage.verifyTableContains(title);
    });
  });

  test('通知公告内容验证', async ({ page }) => {
    await test.step('导航到通知公告页面', async () => {
      await noticePage.goto();
    });

    await test.step('验证通知标题长度限制', async () => {
      const longTitle = '这是一个非常非常长的通知标题，用于测试系统对长标题的处理能力，确保系统能够正确显示和存储长标题';
      const content = '测试内容';
      
      await noticePage.addNotification(longTitle, content);
      await noticePage.verifyTableContains(longTitle.substring(0, 50));
    });

    await test.step('验证通知内容格式', async () => {
      const title = `格式测试通知_${Date.now()}`;
      const content = '支持富文本格式：<b>粗体</b>、<i>斜体</i>、<u>下划线</u>';
      
      await noticePage.addNotification(title, content);
      await noticePage.verifyTableContains(title);
    });
  });
});
