import { DataSource } from 'typeorm';
import { FileMt } from '../../src/entities';
import { UserEntityMt } from '@d8d/core-module-mt/user-module-mt';

/**
 * 测试数据工厂类
 */
export class TestDataFactory {
  /**
   * 创建测试文件数据
   */
  static createFileData(overrides: Partial<FileMt> = {}): Partial<FileMt> {
    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,
      tenantId: 1, // 为多租户文件设置默认租户ID
      uploadTime: new Date(),
      ...overrides
    };
  }

  /**
   * 创建测试用户数据
   */
  static createUserData(overrides: Partial<UserEntityMt> = {}): Partial<UserEntityMt> {
    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,
      tenantId: 1, // 为多租户用户设置默认租户ID
      ...overrides
    };
  }

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

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

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

    const user = userRepository.create(userData);
    return await userRepository.save(user);
  }
}