import { DynamicModule, Global, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PlayerAssets } from './player_assets.entity';
import { PlayerAssetsMemoryService } from './player_assets_memory.service';
import { PlayerAssetsService } from './player_assets.service';
import { RoomDecorPlacements } from './room_decor_placements.entity';
import { RoomDecorPlacementsMemoryService } from './room_decor_placements_memory.service';
import { RoomDecorPlacementsService } from './room_decor_placements.service';

@Global()
@Module({})
export class PlayerAssetsModule {
  static forDatabase(): DynamicModule {
    return {
      module: PlayerAssetsModule,
      imports: [TypeOrmModule.forFeature([PlayerAssets, RoomDecorPlacements])],
      providers: [
        PlayerAssetsService,
        RoomDecorPlacementsService,
        { provide: 'IPlayerAssetsService', useClass: PlayerAssetsService },
        { provide: 'IRoomDecorPlacementsService', useClass: RoomDecorPlacementsService },
      ],
      exports: [PlayerAssetsService, RoomDecorPlacementsService, 'IPlayerAssetsService', 'IRoomDecorPlacementsService'],
    };
  }

  static forMemory(): DynamicModule {
    return {
      module: PlayerAssetsModule,
      providers: [
        PlayerAssetsMemoryService,
        RoomDecorPlacementsMemoryService,
        { provide: 'IPlayerAssetsService', useClass: PlayerAssetsMemoryService },
        { provide: 'IRoomDecorPlacementsService', useClass: RoomDecorPlacementsMemoryService },
      ],
      exports: [PlayerAssetsMemoryService, RoomDecorPlacementsMemoryService, 'IPlayerAssetsService', 'IRoomDecorPlacementsService'],
    };
  }

  static forRoot(useMemory?: boolean): DynamicModule {
    const shouldUseMemory = useMemory ?? (
      process.env.NODE_ENV === 'test' ||
      process.env.USE_MEMORY_STORAGE === 'true' ||
      !process.env.DB_HOST
    );
    return shouldUseMemory ? this.forMemory() : this.forDatabase();
  }
}
