import { DataSource } from 'typeorm';
import { UserEntity } from '@d8d/core-module/user-module';
import { Role } from '@d8d/core-module/user-module';
import { DisabledPerson } from '@d8d/allin-disability-module/entities';
import { UserType } from '@d8d/shared-types';

/**
 * 测试数据工厂类
 */
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);
  }

  /**
   * 创建测试残疾人数据
   */
  static createDisabledPersonData(overrides: Partial<DisabledPerson> = {}): Partial<DisabledPerson> {
    const timestamp = Date.now();
    return {
      name: `测试残疾人${timestamp}`,
      gender: '男',
      idCard: `11010119900101001${timestamp.toString().slice(-2)}`,
      disabilityId: `D123456${timestamp}`,
      disabilityType: '肢体残疾',
      disabilityLevel: '一级',
      idAddress: '北京市朝阳区',
      phone: `138${timestamp.toString().slice(-8)}`,
      province: '北京市',
      city: '北京市',
      district: '朝阳区',
      canDirectContact: 1,
      isInBlackList: 0,
      jobStatus: 0,
      ...overrides
    };
  }

  /**
   * 在数据库中创建测试残疾人
   */
  static async createTestDisabledPerson(dataSource: DataSource, overrides: Partial<DisabledPerson> = {}): Promise<DisabledPerson> {
    const personData = this.createDisabledPersonData(overrides);
    const personRepository = dataSource.getRepository(DisabledPerson);

    const person = personRepository.create(personData);
    return await personRepository.save(person);
  }

  /**
   * 创建测试人才用户（包含残疾人关联）
   */
  static async createTestTalentUser(dataSource: DataSource, overrides: Partial<UserEntity> = {}, personOverrides: Partial<DisabledPerson> = {}): Promise<{ user: UserEntity; person: DisabledPerson }> {
    // 先创建残疾人记录
    const person = await this.createTestDisabledPerson(dataSource, personOverrides);

    // 创建人才用户，关联残疾人记录
    const userData = this.createUserData({
      ...overrides,
      userType: UserType.TALENT,
      personId: person.id
    });

    const userRepository = dataSource.getRepository(UserEntity);
    const user = userRepository.create(userData);
    const savedUser = await userRepository.save(user);

    return { user: savedUser, person };
  }
}