import { DataSource } from 'typeorm';
import { AreaEntityMt } from '../../src/modules/areas/area.entity.mt';
import { expect } from 'vitest';

export class IntegrationTestAssertions {
  /**
   * 断言响应状态码
   */
  static expectStatus(response: any, expectedStatus: number): void {
    expect(response.status).toBe(expectedStatus);
  }

  /**
   * 断言地区存在
   */
  static async expectAreaToExist(areaName: string): Promise<void> {
    const dataSource = await this.getDataSource();
    const areaRepository = dataSource.getRepository(AreaEntityMt);

    const area = await areaRepository.findOne({
      where: { name: areaName }
    });

    expect(area).toBeDefined();
    expect(area?.name).toBe(areaName);
  }

  /**
   * 断言地区不存在
   */
  static async expectAreaNotToExist(areaName: string): Promise<void> {
    const dataSource = await this.getDataSource();
    const areaRepository = dataSource.getRepository(AreaEntityMt);

    const area = await areaRepository.findOne({
      where: { name: areaName }
    });

    expect(area).toBeNull();
  }

  /**
   * 获取数据源
   */
  private static async getDataSource(): Promise<DataSource> {
    const { IntegrationTestDatabase } = await import('@d8d/shared-test-util');
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) {
      throw new Error('Database not initialized');
    }
    return dataSource;
  }
}