import { test, expect } from '../../utils/test-setup';

test.describe('活动管理 E2E 测试', () => {
  test.beforeEach(async ({ adminLoginPage, activityManagementPage }) => {
    // 以管理员身份登录后台
    await adminLoginPage.goto();
    await adminLoginPage.login('admin', 'admin123');
    await activityManagementPage.goto();
  });

  test('查看活动列表', async ({ activityManagementPage }) => {
    await activityManagementPage.expectToBeVisible();
    const activityCount = await activityManagementPage.getActivityCount();
    expect(activityCount).toBeGreaterThan(0);
  });

  test('搜索活动', async ({ activityManagementPage }) => {
    // 先获取所有活动
    const initialCount = await activityManagementPage.getActivityCount();

    // 搜索存在的活动
    await activityManagementPage.searchActivities('活动');
    const searchedCount = await activityManagementPage.getActivityCount();
    expect(searchedCount).toBeGreaterThan(0);
    expect(searchedCount).toBeLessThanOrEqual(initialCount);

    // 搜索不存在的活动
    await activityManagementPage.searchActivities('nonexistentactivity12345');
    const emptyCount = await activityManagementPage.getActivityCount();
    expect(emptyCount).toBe(0);

    // 清除搜索
    await activityManagementPage.searchActivities('');
    const normalCount = await activityManagementPage.getActivityCount();
    expect(normalCount).toBeGreaterThan(0);
  });

  test('按活动类型筛选', async ({ activityManagementPage }) => {
    // 筛选去程活动
    await activityManagementPage.filterByType('去程');
    const departureCount = await activityManagementPage.getActivityCount();
    expect(departureCount).toBeGreaterThan(0);

    // 筛选返程活动
    await activityManagementPage.filterByType('返程');
    const returnCount = await activityManagementPage.getActivityCount();
    expect(returnCount).toBeGreaterThan(0);

    // 重置筛选 - 通过选择全部类型选项
    await activityManagementPage.typeFilter.click();
    await activityManagementPage.page.getByRole('option', { name: '全部类型' }).click();
    const allCount = await activityManagementPage.getActivityCount();
    expect(allCount).toBeGreaterThan(0);
  });

  test('创建新活动', async ({ activityManagementPage }) => {
    const testActivityName = `测试活动_${Date.now()}`;

    await activityManagementPage.createActivity({
      name: testActivityName,
      description: '这是一个测试活动',
      type: '去程',
      startDate: '2025-01-01T08:00',
      endDate: '2025-01-01T18:00'
    });

    // 验证活动创建成功
    await activityManagementPage.expectActivityExists(testActivityName);
  });

  test('编辑活动信息', async ({ activityManagementPage }) => {
    const testActivityName = `编辑活动_${Date.now()}`;

    // 先创建测试活动
    await activityManagementPage.createActivity({
      name: testActivityName,
      description: '原始描述',
      type: '去程',
      startDate: '2025-01-01T08:00',
      endDate: '2025-01-01T18:00'
    });

    // 编辑活动信息
    await activityManagementPage.editActivity(testActivityName, {
      name: `${testActivityName}_更新`,
      description: '更新后的描述',
      type: '返程活动',
      startDate: '2025-01-02T09:00',
      endDate: '2025-01-02T19:00'
    });

    // 验证活动信息已更新
    await activityManagementPage.expectActivityExists(`${testActivityName}_更新`);
  });

  test('删除活动', async ({ activityManagementPage }) => {
    const testActivityName = `删除活动_${Date.now()}`;

    // 先创建测试活动
    await activityManagementPage.createActivity({
      name: testActivityName,
      description: '待删除活动',
      type: '去程',
      startDate: '2025-01-01T08:00',
      endDate: '2025-01-01T18:00'
    });

    // 验证活动存在
    await activityManagementPage.expectActivityExists(testActivityName);

    // 删除活动
    await activityManagementPage.deleteActivity(testActivityName);

    // 验证活动已被删除
    await activityManagementPage.expectActivityNotExists(testActivityName);
  });

  test('启用/禁用活动', async ({ activityManagementPage }) => {
    const testActivityName = `状态活动_${Date.now()}`;

    // 先创建测试活动
    await activityManagementPage.createActivity({
      name: testActivityName,
      description: '状态测试活动',
      type: '去程',
      startDate: '2025-01-01T08:00',
      endDate: '2025-01-01T18:00'
    });

    // 验证活动存在
    await activityManagementPage.expectActivityExists(testActivityName);

    // 获取初始状态
    const initialStatus = await activityManagementPage.getActivityStatus(testActivityName);
    expect(initialStatus).toMatch(/启用|禁用/);

    // 切换状态
    await activityManagementPage.toggleActivityStatus(testActivityName);

    // 验证状态已切换
    const newStatus = await activityManagementPage.getActivityStatus(testActivityName);
    expect(newStatus).not.toBe(initialStatus);

    // 再次切换状态
    await activityManagementPage.toggleActivityStatus(testActivityName);

    // 验证状态恢复
    const finalStatus = await activityManagementPage.getActivityStatus(testActivityName);
    expect(finalStatus).toBe(initialStatus);
  });

  test('活动分页功能', async ({ activityManagementPage }) => {
    // 确保有足够多的活动来测试分页
    const initialCount = await activityManagementPage.getActivityCount();

    if (initialCount < 10) {
      // 创建一些测试活动
      for (let i = 0; i < 5; i++) {
        await activityManagementPage.createActivity({
          name: `分页测试活动_${Date.now()}_${i}`,
          description: `分页测试活动 ${i}`,
          type: i % 2 === 0 ? '去程' : '返程',
          startDate: '2025-01-01T08:00',
          endDate: '2025-01-01T18:00'
        });
      }
    }

    // 验证分页控件可见
    await expect(activityManagementPage.pagination).toBeVisible();
  });

  test('完整活动管理工作流 - 登录→创建→编辑→启用/禁用→删除', async ({ adminLoginPage, activityManagementPage, page }) => {
    const testActivityName = `工作流活动_${Date.now()}`;

    // 1. 登录系统
    await adminLoginPage.goto();
    await adminLoginPage.login('admin', 'admin123');

    // 验证登录成功，跳转到Dashboard
    await expect(page).toHaveURL(/.*dashboard/);
    await expect(page.locator('text=仪表盘')).toBeVisible();

    // 2. 导航到活动管理页面
    await activityManagementPage.goto();
    await activityManagementPage.expectToBeVisible();

    // 3. 创建新活动
    await activityManagementPage.createActivity({
      name: testActivityName,
      description: '工作流测试活动',
      type: '去程',
      startDate: '2025-01-01T08:00',
      endDate: '2025-01-01T18:00'
    });

    // 验证活动创建成功
    await activityManagementPage.expectActivityExists(testActivityName);

    // 4. 搜索并编辑活动
    await activityManagementPage.searchActivities(testActivityName);
    await activityManagementPage.editActivity(testActivityName, {
      name: `${testActivityName}_更新`,
      description: '更新后的工作流活动',
      type: '返程活动',
      startDate: '2025-01-02T09:00',
      endDate: '2025-01-02T19:00'
    });

    // 验证活动信息已更新
    await activityManagementPage.expectActivityExists(`${testActivityName}_更新`);

    // 5. 启用/禁用活动
    await activityManagementPage.toggleActivityStatus(`${testActivityName}_更新`);
    const statusAfterToggle = await activityManagementPage.getActivityStatus(`${testActivityName}_更新`);
    expect(statusAfterToggle).toMatch(/启用|禁用/);

    // 6. 删除活动
    await activityManagementPage.deleteActivity(`${testActivityName}_更新`);

    // 验证活动已被删除
    await activityManagementPage.expectActivityNotExists(`${testActivityName}_更新`);

    // 7. 返回Dashboard验证状态
    await page.goto('/admin/dashboard');
    await expect(page.locator('text=仪表盘')).toBeVisible();
    await expect(page.locator('text=欢迎回来')).toBeVisible();
  });

  test('边界条件测试 - 空搜索和特殊字符', async ({ activityManagementPage }) => {
    // 测试空搜索
    await activityManagementPage.searchActivities('');
    const normalCount = await activityManagementPage.getActivityCount();
    expect(normalCount).toBeGreaterThan(0);

    // 测试特殊字符搜索
    await activityManagementPage.searchActivities('@#$%');
    const specialCharCount = await activityManagementPage.getActivityCount();
    expect(specialCharCount).toBe(0);

    // 清除搜索
    await activityManagementPage.searchActivities('');
    const restoredCount = await activityManagementPage.getActivityCount();
    expect(restoredCount).toBeGreaterThan(0);
  });

  test('响应式布局 - 桌面端', async ({ activityManagementPage, page }) => {
    await page.setViewportSize({ width: 1200, height: 800 });
    await activityManagementPage.expectToBeVisible();

    // 验证桌面端布局元素
    await expect(activityManagementPage.searchInput).toBeVisible();
    await expect(activityManagementPage.createActivityButton).toBeVisible();
    await expect(activityManagementPage.activityTable).toBeVisible();
  });

  test('响应式布局 - 移动端', async ({ activityManagementPage, page }) => {
    await page.setViewportSize({ width: 375, height: 667 });
    await activityManagementPage.expectToBeVisible();

    // 验证移动端布局
    await expect(activityManagementPage.pageTitle).toBeVisible();
    await expect(activityManagementPage.searchInput).toBeVisible();
  });
});