import { IntegrationTestDatabase } from '@d8d/shared-test-util';
import { UserEntityMt } from '@d8d/user-module-mt';
import { MerchantMt } from '../../src/entities/merchant.mt.entity';

/**
 * 商户模块测试工具类
 * 避免依赖共享测试数据工厂的复杂依赖关系
 */
export class MerchantTestUtils {
  /**
   * 创建测试用户
   */
  static async createTestUser(overrides: Partial<any> = {}): Promise<UserEntityMt> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    const userRepository = dataSource.getRepository(UserEntityMt);

    const defaultUser = {
      username: `test_user_${Date.now()}`,
      password: 'test_password',
      nickname: '测试用户',
      registrationSource: 'web',
      tenantId: 1
    };

    const userData = { ...defaultUser, ...overrides };
    const user = userRepository.create(userData);
    return await userRepository.save(user);
  }

  /**
   * 创建测试商户
   */
  static async createTestMerchant(
    createdBy: number,
    tenantId: number = 1,
    overrides: Partial<MerchantMt> = {}
  ): Promise<MerchantMt> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    const merchantRepository = dataSource.getRepository(MerchantMt);

    const timestamp = Date.now();
    const defaultMerchant = {
      name: `测试商户_${timestamp}`,
      username: `test_m_${timestamp.toString().slice(-8)}`,
      password: 'password123',
      phone: '13800138000',
      realname: '测试商户',
      state: 1,
      loginNum: 0,
      loginTime: 0,
      loginIp: '',
      lastLoginTime: 0,
      lastLoginIp: '',
      rsaPublicKey: '',
      aesKey: '',
      tenantId,
      createdBy
    };

    const merchantData = { ...defaultMerchant, ...overrides };
    const merchant = merchantRepository.create(merchantData);
    return await merchantRepository.save(merchant);
  }

  /**
   * 创建多个测试商户
   */
  static async createTestMerchants(
    createdBy: number,
    count: number,
    tenantId: number = 1,
    overrides: Partial<MerchantMt> = {}
  ): Promise<MerchantMt[]> {
    const merchants: MerchantMt[] = [];
    for (let i = 0; i < count; i++) {
      const merchant = await this.createTestMerchant(createdBy, tenantId, {
        ...overrides,
        name: `测试商户_${i + 1}`,
        username: `test_m${i + 1}_${Date.now().toString().slice(-6)}`
      });
      merchants.push(merchant);
    }
    return merchants;
  }

  /**
   * 创建测试商户数据集
   */
  static async createTestMerchantDataSet(
    userId: number,
    otherUserId: number,
    tenantId: number = 1
  ): Promise<{
    userMerchants: MerchantMt[];
    otherUserMerchants: MerchantMt[];
  }> {
    const userMerchants = await this.createTestMerchants(userId, 2, tenantId, {
      name: '用户商户'
    });

    const otherUserMerchants = await this.createTestMerchants(otherUserId, 1, tenantId, {
      name: '其他用户商户'
    });

    return {
      userMerchants,
      otherUserMerchants
    };
  }

  /**
   * 清理测试数据
   */
  static async cleanupTestData(): Promise<void> {
    const dataSource = await IntegrationTestDatabase.getDataSource();
    const merchantRepository = dataSource.getRepository(MerchantMt);
    const userRepository = dataSource.getRepository(UserEntityMt);

    await merchantRepository.clear();
    await userRepository.clear();
  }
}