import { DataSource } from 'typeorm';
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigModule } from '@nestjs/config';
import { UserGroupsRepositoryModule } from '../../repositories/user_groups/user_groups.repository.module';
import { TasksRepositoryModule } from '../../repositories/tasks/tasks.repository.module';
import { AuthModule } from '../../features/auth/auth.module';
import { AdB2cModule } from '../../gateways/adb2c/adb2c.module';
import { AccountsModule } from '../../features/accounts/accounts.module';
import { UsersModule } from '../../features/users/users.module';
import { FilesModule } from '../../features/files/files.module';
import { TasksModule } from '../../features/tasks/tasks.module';
import { SendGridModule } from '../../features/../gateways/sendgrid/sendgrid.module';
import { LicensesModule } from '../../features/licenses/licenses.module';
import { AccountsRepositoryModule } from '../../repositories/accounts/accounts.repository.module';
import { UsersRepositoryModule } from '../../repositories/users/users.repository.module';
import { LicensesRepositoryModule } from '../../repositories/licenses/licenses.repository.module';
import { AudioFilesRepositoryModule } from '../../repositories/audio_files/audio_files.repository.module';
import { AudioOptionItemsRepositoryModule } from '../../repositories/audio_option_items/audio_option_items.repository.module';
import { CheckoutPermissionsRepositoryModule } from '../../repositories/checkout_permissions/checkout_permissions.repository.module';
import { NotificationModule } from '../../features//notification/notification.module';
import { NotificationhubModule } from '../../gateways/notificationhub/notificationhub.module';
import { BlobstorageModule } from '../../gateways/blobstorage/blobstorage.module';
import { AuthGuardsModule } from '../../common/guards/auth/authguards.module';
import { SortCriteriaRepositoryModule } from '../../repositories/sort_criteria/sort_criteria.repository.module';
import { AuthService } from '../../features/auth/auth.service';
import { AccountsService } from '../../features/accounts/accounts.service';
import { UsersService } from '../../features/users/users.service';
import { NotificationhubService } from '../../gateways/notificationhub/notificationhub.service';
import { FilesService } from '../../features/files/files.service';
import { LicensesService } from '../../features/licenses/licenses.service';
import { TasksService } from '../../features/tasks/tasks.service';

export const makeTestingModule = async (
  datasource: DataSource,
): Promise<TestingModule> => {
  try {
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        ConfigModule.forRoot({
          envFilePath: ['.env.local', '.env'],
          isGlobal: true,
        }),
        AuthModule,
        AdB2cModule,
        AccountsModule,
        UsersModule,
        FilesModule,
        TasksModule,
        UsersModule,
        SendGridModule,
        LicensesModule,
        AccountsRepositoryModule,
        UsersRepositoryModule,
        LicensesRepositoryModule,
        AudioFilesRepositoryModule,
        AudioOptionItemsRepositoryModule,
        TasksRepositoryModule,
        CheckoutPermissionsRepositoryModule,
        UserGroupsRepositoryModule,
        UserGroupsRepositoryModule,
        NotificationModule,
        NotificationhubModule,
        BlobstorageModule,
        AuthGuardsModule,
        SortCriteriaRepositoryModule,
      ],
      providers: [
        AuthService,
        AccountsService,
        UsersService,
        NotificationhubService,
        FilesService,
        TasksService,
        LicensesService,
      ],
    })
      .useMocker(async (token) => {
        switch (token) {
          case DataSource:
            return datasource;
        }
      })
      .compile();

    return module;
  } catch (e) {
    console.log(e);
  }
};
