import { DataSource } from 'typeorm';
import { beforeEach, afterEach } from 'vitest';
import { UserEntity as UserEntityMt, Role as RoleMt } from '@d8d/core-module/user-module';
import { File as FileMt } from '@d8d/core-module/file-module';
import { AppDataSource } from '@d8d/shared-utils';

/**
 * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
 */
export class IntegrationTestDatabase {
  /**
   * 清理集成测试数据库
   */
  static async cleanup(): Promise<void> {
    if (AppDataSource.isInitialized) {
      await AppDataSource.destroy();
    }
  }

  /**
   * 获取当前数据源
   */
  static async getDataSource(): Promise<DataSource> {
    if(!AppDataSource.isInitialized) {
      await AppDataSource.initialize();
    }
    return AppDataSource
  }
}

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

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

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

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

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

  /**
   * 创建测试文件数据
   */
  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,
      uploadTime: new Date(),
      ...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);
  }
}

/**
 * 集成测试数据库生命周期钩子
 */
export function setupIntegrationDatabaseHooks() {
  beforeEach(async () => {
    await IntegrationTestDatabase.getDataSource();
  });

  afterEach(async () => {
    await IntegrationTestDatabase.cleanup();
  });
}