import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { Notice } from './notice.entity';
import { NoticeService } from './notice.service';
import { NoticeMemoryService } from './notice_memory.service';
import { NoticeController } from './notice.controller';
import { NoticeGateway } from './notice.gateway';
import { LoginCoreModule } from '../../core/login_core/login_core.module';

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

@Module({
  imports: [
    ...(isDatabaseConfigured() ? [TypeOrmModule.forFeature([Notice])] : []),
    ScheduleModule.forRoot(),
    LoginCoreModule,
  ],
  controllers: [NoticeController],
  providers: [
    {
      provide: NoticeService,
      useClass: isDatabaseConfigured() ? NoticeService : NoticeMemoryService,
    },
    NoticeGateway,
  ],
  exports: [NoticeService, NoticeGateway],
})
export class NoticeModule {}
