import { DataSource } from 'typeorm';
import { File } from '../../src/entities';
import { UserEntity } from '@d8d/user-module';

/**
 * 测试数据工厂类
 */
export class TestDataFactory {
  /**
   * 创建测试文件数据
   */
  static createFileData(overrides: Partial<File> = {}): Partial<File> {
    const timestamp = Date.now();
    return {
      name: `testfile_${timestamp}.txt`,
      type: 'text/plain',
      size: 1024,
      path: `/uploads/testfile_${timestamp}.txt`,
      description: `Test file ${timestamp}`,
      uploadUserId: 1,
      uploadTime: new Date(),
      ...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,
      ...overrides
    };
  }

  /**
   * 在数据库中创建测试文件
   */
  static async createTestFile(dataSource: DataSource, overrides: Partial<File> = {}): Promise<File> {
    const fileData = this.createFileData(overrides);
    const fileRepository = dataSource.getRepository(File);

    const file = fileRepository.create(fileData);
    return await fileRepository.save(file);
  }

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