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

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

  test('查看路线列表', async ({ routeManagementPage }) => {
    await routeManagementPage.expectToBeVisible();
    const routeCount = await routeManagementPage.getRouteCount();
    expect(routeCount).toBeGreaterThan(0);
  });

  test('搜索路线', async ({ routeManagementPage }) => {
    // 先获取所有路线
    const initialCount = await routeManagementPage.getRouteCount();

    // 搜索存在的路线
    await routeManagementPage.searchRoutes('北京');
    const searchedCount = await routeManagementPage.getRouteCount();
    expect(searchedCount).toBeGreaterThan(0);
    expect(searchedCount).toBeLessThanOrEqual(initialCount);

    // 搜索不存在的路线
    await routeManagementPage.searchRoutes('nonexistentroute12345');
    const emptyCount = await routeManagementPage.getRouteCount();
    expect(emptyCount).toBe(0);

    // 清除搜索
    await routeManagementPage.searchRoutes('');
    const normalCount = await routeManagementPage.getRouteCount();
    expect(normalCount).toBeGreaterThan(0);
  });

  test('按车型筛选', async ({ routeManagementPage }) => {
    // 筛选大巴车型
    await routeManagementPage.filterByVehicleType('大巴');
    const busCount = await routeManagementPage.getRouteCount();
    expect(busCount).toBeGreaterThanOrEqual(0);

    // 筛选中巴车型
    await routeManagementPage.filterByVehicleType('中巴');
    const vanCount = await routeManagementPage.getRouteCount();
    expect(vanCount).toBeGreaterThanOrEqual(0);

    // 筛选小车车型
    await routeManagementPage.filterByVehicleType('小车');
    const carCount = await routeManagementPage.getRouteCount();
    expect(carCount).toBeGreaterThanOrEqual(0);

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

  test('创建新路线', async ({ routeManagementPage }) => {
    const testRouteName = `测试路线_${Date.now()}`;

    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 200,
      seatCount: 40,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 验证路线创建成功
    await routeManagementPage.expectRouteExists(testRouteName);
  });

  test('编辑路线信息', async ({ routeManagementPage }) => {
    const testRouteName = `编辑路线_${Date.now()}`;

    // 先创建测试路线
    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 200,
      seatCount: 40,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 编辑路线信息
    await routeManagementPage.editRoute(testRouteName, {
      name: `${testRouteName}_更新`,
      startPoint: '广州',
      endPoint: '深圳',
      vehicleType: 'van',
      price: 150,
      seatCount: 20,
      departureTime: '2025-01-02 09:00'
    });

    // 验证路线信息已更新
    await routeManagementPage.expectRouteExists(`${testRouteName}_更新`);
  });

  test('删除路线', async ({ routeManagementPage }) => {
    const testRouteName = `删除路线_${Date.now()}`;

    // 先创建测试路线
    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 200,
      seatCount: 40,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 验证路线存在
    await routeManagementPage.expectRouteExists(testRouteName);

    // 删除路线
    await routeManagementPage.deleteRoute(testRouteName);

    // 验证路线已被删除
    await routeManagementPage.expectRouteNotExists(testRouteName);
  });

  test('启用/禁用路线', async ({ routeManagementPage }) => {
    const testRouteName = `状态路线_${Date.now()}`;

    // 先创建测试路线
    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 200,
      seatCount: 40,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 验证路线存在
    await routeManagementPage.expectRouteExists(testRouteName);

    // 获取初始状态
    const initialStatus = await routeManagementPage.getRouteStatus(testRouteName);
    expect(initialStatus).toMatch(/启用|禁用/);

    // 切换状态
    await routeManagementPage.toggleRouteStatus(testRouteName);

    // 验证状态已切换
    const newStatus = await routeManagementPage.getRouteStatus(testRouteName);
    expect(newStatus).not.toBe(initialStatus);

    // 再次切换状态
    await routeManagementPage.toggleRouteStatus(testRouteName);

    // 验证状态恢复
    const finalStatus = await routeManagementPage.getRouteStatus(testRouteName);
    expect(finalStatus).toBe(initialStatus);
  });

  test('路线分页功能', async ({ routeManagementPage }) => {
    // 确保有足够多的路线来测试分页
    const initialCount = await routeManagementPage.getRouteCount();

    if (initialCount < 10) {
      // 创建一些测试路线
      for (let i = 0; i < 5; i++) {
        await routeManagementPage.createRoute({
          name: `分页测试路线_${Date.now()}_${i}`,
          startPoint: `城市${i}`,
          endPoint: `城市${i + 1}`,
          vehicleType: i % 3 === 0 ? 'bus' : i % 3 === 1 ? 'van' : 'car',
          price: 100 + i * 20,
          seatCount: 30 + i * 5,
          departureTime: `2025-01-01 0${8 + i}:00`,
          activityId: 1
        });
      }
    }

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

  test('完整路线管理工作流 - 登录→创建→编辑→启用/禁用→删除', async ({ adminLoginPage, routeManagementPage, page }) => {
    const testRouteName = `工作流路线_${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 routeManagementPage.goto();
    await routeManagementPage.expectToBeVisible();

    // 3. 创建新路线
    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 200,
      seatCount: 40,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 验证路线创建成功
    await routeManagementPage.expectRouteExists(testRouteName);

    // 4. 搜索并编辑路线
    await routeManagementPage.searchRoutes(testRouteName);
    await routeManagementPage.editRoute(testRouteName, {
      name: `${testRouteName}_更新`,
      startPoint: '广州',
      endPoint: '深圳',
      vehicleType: 'van',
      price: 150,
      seatCount: 20,
      departureTime: '2025-01-02 09:00'
    });

    // 验证路线信息已更新
    await routeManagementPage.expectRouteExists(`${testRouteName}_更新`);

    // 5. 启用/禁用路线
    await routeManagementPage.toggleRouteStatus(`${testRouteName}_更新`);
    const statusAfterToggle = await routeManagementPage.getRouteStatus(`${testRouteName}_更新`);
    expect(statusAfterToggle).toMatch(/启用|禁用/);

    // 6. 删除路线
    await routeManagementPage.deleteRoute(`${testRouteName}_更新`);

    // 验证路线已被删除
    await routeManagementPage.expectRouteNotExists(`${testRouteName}_更新`);

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

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

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

    // 清除搜索
    await routeManagementPage.searchRoutes('');
    const restoredCount = await routeManagementPage.getRouteCount();
    expect(restoredCount).toBeGreaterThan(0);
  });

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

    // 验证桌面端布局元素
    await expect(routeManagementPage.searchInput).toBeVisible();
    await expect(routeManagementPage.createRouteButton).toBeVisible();
    await expect(routeManagementPage.routeTable).toBeVisible();
  });

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

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

  test('价格和座位数验证', async ({ routeManagementPage }) => {
    const testRouteName = `验证路线_${Date.now()}`;

    // 创建路线并验证价格和座位数显示
    await routeManagementPage.createRoute({
      name: testRouteName,
      startPoint: '北京',
      endPoint: '上海',
      vehicleType: 'bus',
      price: 250,
      seatCount: 45,
      departureTime: '2025-01-01 08:00',
      activityId: 1
    });

    // 验证路线存在
    await routeManagementPage.expectRouteExists(testRouteName);

    // 验证价格和座位数在表格中正确显示
    const routeRow = await routeManagementPage.getRouteByName(testRouteName);
    expect(routeRow).not.toBeNull();
    await expect(routeRow!).toContainText('¥250');
    await expect(routeRow!).toContainText('45');
  });

  test('车型显示验证', async ({ routeManagementPage }) => {
    const testRouteName = `车型路线_${Date.now()}`;

    // 创建不同车型的路线
    const vehicleTypes = ['bus', 'van', 'car'];
    const vehicleTypeNames = ['大巴', '中巴', '小车'];

    for (let i = 0; i < vehicleTypes.length; i++) {
      const routeName = `${testRouteName}_${vehicleTypes[i]}`;

      await routeManagementPage.createRoute({
        name: routeName,
        startPoint: '城市A',
        endPoint: '城市B',
        vehicleType: vehicleTypes[i] as 'bus' | 'van' | 'car',
        price: 100,
        seatCount: 30,
        departureTime: '2025-01-01 08:00',
        activityId: 1
      });

      // 验证车型正确显示
      await routeManagementPage.expectRouteExists(routeName);
      const routeRow = await routeManagementPage.getRouteByName(routeName);
      expect(routeRow).not.toBeNull();
      await expect(routeRow!).toContainText(vehicleTypeNames[i]);
    }
  });
});