
import { IntegrationTestDatabase } from './integration-test-db';
import { UserEntity } from '@d8d/server/modules/users/user.entity';
import { ActivityEntity } from '@d8d/server/modules/activities/activity.entity';
import { RouteEntity } from '@d8d/server/modules/routes/route.entity';
import { LocationEntity } from '@d8d/server/modules/locations/location.entity';
import { Passenger } from '@d8d/server/modules/passengers/passenger.entity';
import { Order } from '@d8d/server/modules/orders/order.entity';



/**
 * 集成测试断言工具
 */
export class IntegrationTestAssertions {
  /**
   * 断言响应状态码
   */
  static expectStatus(response: { status: number }, expectedStatus: number): void {
    if (response.status !== expectedStatus) {
      throw new Error(`Expected status ${expectedStatus}, but got ${response.status}`);
    }
  }

  /**
   * 断言响应包含特定字段
   */
  static expectResponseToHave(response: { data: any }, expectedFields: Record<string, any>): void {
    for (const [key, value] of Object.entries(expectedFields)) {
      if (response.data[key] !== value) {
        throw new Error(`Expected field ${key} to be ${value}, but got ${response.data[key]}`);
      }
    }
  }

  /**
   * 断言响应包含特定结构
   */
  static expectResponseStructure(response: { data: any }, structure: Record<string, any>): void {
    for (const key of Object.keys(structure)) {
      if (!(key in response.data)) {
        throw new Error(`Expected response to have key: ${key}`);
      }
    }
  }

  /**
   * 断言用户存在于数据库中
   */
  static async expectUserToExist(username: string): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const userRepository = dataSource.getRepository(UserEntity);
    const user = await userRepository.findOne({ where: { username } });

    if (!user) {
      throw new Error(`Expected user ${username} to exist in database`);
    }
  }

  /**
   * 断言用户不存在于数据库中
   */
  static async expectUserNotToExist(username: string): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const userRepository = dataSource.getRepository(UserEntity);
    const user = await userRepository.findOne({ where: { username } });

    if (user) {
      throw new Error(`Expected user ${username} not to exist in database`);
    }
  }

  /**
   * 断言活动存在于数据库中
   */
  static async expectActivityToExist(activityId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const activityRepository = dataSource.getRepository(ActivityEntity);
    const activity = await activityRepository.findOne({ where: { id: activityId } });

    if (!activity) {
      throw new Error(`Expected activity ${activityId} to exist in database`);
    }
  }

  /**
   * 断言活动不存在于数据库中
   */
  static async expectActivityNotToExist(activityId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const activityRepository = dataSource.getRepository(ActivityEntity);
    const activity = await activityRepository.findOne({ where: { id: activityId } });

    if (activity) {
      throw new Error(`Expected activity ${activityId} not to exist in database`);
    }
  }

  /**
   * 断言路线存在于数据库中
   */
  static async expectRouteToExist(routeId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const routeRepository = dataSource.getRepository(RouteEntity);
    const route = await routeRepository.findOne({ where: { id: routeId } });

    if (!route) {
      throw new Error(`Expected route ${routeId} to exist in database`);
    }
  }

  /**
   * 断言路线不存在于数据库中
   */
  static async expectRouteNotToExist(routeId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const routeRepository = dataSource.getRepository(RouteEntity);
    const route = await routeRepository.findOne({ where: { id: routeId } });

    if (route) {
      throw new Error(`Expected route ${routeId} not to exist in database`);
    }
  }

  /**
   * 断言地点存在于数据库中
   */
  static async expectLocationToExist(locationId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const locationRepository = dataSource.getRepository(LocationEntity);
    const location = await locationRepository.findOne({ where: { id: locationId } });

    if (!location) {
      throw new Error(`Expected location ${locationId} to exist in database`);
    }
  }

  /**
   * 断言地点不存在于数据库中
   */
  static async expectLocationNotToExist(locationId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const locationRepository = dataSource.getRepository(LocationEntity);
    const location = await locationRepository.findOne({ where: { id: locationId } });

    if (location) {
      throw new Error(`Expected location ${locationId} not to exist in database`);
    }
  }

  /**
   * 断言乘客存在于数据库中
   */
  static async expectPassengerToExist(passengerId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const passengerRepository = dataSource.getRepository(Passenger);
    const passenger = await passengerRepository.findOne({ where: { id: passengerId } });

    if (!passenger) {
      throw new Error(`Expected passenger ${passengerId} to exist in database`);
    }
  }

  /**
   * 断言乘客不存在于数据库中
   */
  static async expectPassengerNotToExist(passengerId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const passengerRepository = dataSource.getRepository(Passenger);
    const passenger = await passengerRepository.findOne({ where: { id: passengerId } });

    if (passenger) {
      throw new Error(`Expected passenger ${passengerId} not to exist in database`);
    }
  }

  /**
   * 断言订单存在于数据库中
   */
  static async expectOrderToExist(orderId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const orderRepository = dataSource.getRepository(Order);
    const order = await orderRepository.findOne({ where: { id: orderId } });

    if (!order) {
      throw new Error(`Expected order ${orderId} to exist in database`);
    }
  }

  /**
   * 断言订单不存在于数据库中
   */
  static async expectOrderNotToExist(orderId: number): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const orderRepository = dataSource.getRepository(Order);
    const order = await orderRepository.findOne({ where: { id: orderId } });

    if (order) {
      throw new Error(`Expected order ${orderId} not to exist in database`);
    }
  }

  /**
   * 根据ID获取订单
   */
  static async getOrderById(orderId: number): Promise<Order | null> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }

    const orderRepository = dataSource.getRepository(Order);
    return await orderRepository.findOne({ where: { id: orderId } });
  }
}