import { DataSource } from 'typeorm';
import { SystemConfig } from '../../src/entities/system-config.entity';
import { UserEntity } from '@d8d/core-module/user-module';

/**
 * 测试数据工厂类
 */
export class TestDataFactory {
  /**
   * 创建测试系统配置数据
   */
  static createSystemConfigData(overrides: Partial<SystemConfig> = {}): Partial<SystemConfig> {
    const timestamp = Date.now();
    return {
      configKey: `app.test.config.${timestamp}`,
      configValue: 'true',
      description: `Test system config ${timestamp}`,
       // 为多租户系统配置设置默认租户ID
      ...overrides
    };
  }

  /**
   * 创建测试用户数据
   */
  static createUserData(overrides: Partial<UserEntity> = {}): Partial<UserEntity> {
    const timestamp = Date.now();
    return {
      username: `testuser_${timestamp}`,
      password: 'TestPassword123!',
      email: `test_${timestamp}@example.com`,
      phone: `138${timestamp.toString().slice(-8)}`,
      nickname: `Test User ${timestamp}`,
      name: `Test Name ${timestamp}`,
      isDisabled: 0,
      isDeleted: 0,
       // 为多租户用户设置默认租户ID
      ...overrides
    };
  }

  /**
   * 在数据库中创建测试系统配置
   */
  static async createTestSystemConfig(dataSource: DataSource, overrides: Partial<SystemConfig> = {}): Promise<SystemConfig> {
    const configData = this.createSystemConfigData(overrides);
    const systemConfigRepository = dataSource.getRepository(SystemConfig);
    const systemConfig = systemConfigRepository.create(configData);
    return await systemConfigRepository.save(systemConfig);
  }

  /**
   * 在数据库中创建测试用户
   */
  static async createTestUser(dataSource: DataSource, overrides: Partial<UserEntity> = {}): Promise<UserEntity> {
    const userData = this.createUserData(overrides);
    const userRepository = dataSource.getRepository(UserEntity);
    const user = userRepository.create(userData);
    return await userRepository.save(user);
  }
}