import { DataSource } from 'typeorm';
import { UserEntity } from '../../src/entities/user.entity';
import { Role } from '../../src/entities/role.entity';

/**
 * 测试数据工厂类
 */
export class TestDataFactory {
  /**
   * 创建测试用户数据
   */
  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,
      ...overrides
    };
  }

  /**
   * 创建测试角色数据
   */
  static createRoleData(overrides: Partial<Role> = {}): Partial<Role> {
    const timestamp = Date.now();
    return {
      name: `test_role_${timestamp}`,
      description: `Test role description ${timestamp}`,
      ...overrides
    };
  }

  /**
   * 在数据库中创建测试用户
   */
  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);
  }

  /**
   * 在数据库中创建测试角色
   */
  static async createTestRole(dataSource: DataSource, overrides: Partial<Role> = {}): Promise<Role> {
    const roleData = this.createRoleData(overrides);
    const roleRepository = dataSource.getRepository(Role);

    const role = roleRepository.create(roleData);
    return await roleRepository.save(role);
  }
}