import { DynamicModule, Global, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '../auth/auth.module';
import { ChatModule } from '../chat/chat.module';
import { LoginCoreModule } from '../../core/login_core/login_core.module';
import { SocialController, WorldTravelController } from './social.controller';
import { SocialDatabaseStore } from './social.database-store';
import { SocialMemoryStore } from './social.memory-store';
import { DirectMessage, FriendRequest, Friendship, PlayerTravelUnlock, SocialNotification, UserBlock, UserReport } from './social.entities';
import { SocialService } from './social.service';
import { SOCIAL_STORE } from './social.store';

function isDatabaseConfigured(): boolean {
  return ['DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_NAME'].every((key) => process.env[key]);
}

@Global()
@Module({})
export class SocialModule {
  static forRoot(): DynamicModule {
    const database = isDatabaseConfigured();
    return {
      module: SocialModule,
      imports: [AuthModule, ChatModule, LoginCoreModule, ...(database ? [TypeOrmModule.forFeature([Friendship, FriendRequest, UserBlock, DirectMessage, UserReport, PlayerTravelUnlock, SocialNotification])] : [])],
      controllers: [SocialController, WorldTravelController],
      providers: [
        ...(database ? [SocialDatabaseStore] : [SocialMemoryStore]),
        { provide: SOCIAL_STORE, useExisting: database ? SocialDatabaseStore : SocialMemoryStore },
        SocialService,
      ],
      exports: [SocialService],
    };
  }
}
