import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ThrottlerModule } from '@nestjs/throttler';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { AuthModule } from './auth/auth.module';
import { CharacterModule } from './character/character.module';
import { MonsterModule } from './monster/monster.module';
import { CombatModule } from './combat/combat.module';
import { ItemModule } from './item/item.module';
import { MaterialModule } from './material/material.module';
import { CraftModule } from './craft/craft.module';
import { ForgeModule } from './forge/forge.module';
import { EconomyModule } from './economy/economy.module';
import { TwitchModule } from './twitch/twitch.module';
import { AchievementModule } from './achievement/achievement.module';
import { CommunityModule } from './community/community.module';
import { HallOfFameModule } from './halloffame/halloffame.module';
import { ProfileModule } from './profile/profile.module';
import { HealthController } from './common/health.controller';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    EventEmitterModule.forRoot(),

    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        type: 'mysql',
        url: config.get<string>('DATABASE_URL'),
        autoLoadEntities: true,
        synchronize: config.get('DB_SYNC') === 'true' || config.get('NODE_ENV') !== 'production',
        logging: config.get('NODE_ENV') === 'development',
      }),
    }),

    ThrottlerModule.forRoot([
      {
        ttl: 60_000,
        limit: 20,
      },
    ]),

    AuthModule,
    CharacterModule,
    MonsterModule,
    CombatModule,
    ItemModule,
    MaterialModule,
    CraftModule,
    ForgeModule,
    EconomyModule,
    TwitchModule,
    AchievementModule,
    CommunityModule,
    HallOfFameModule,
    ProfileModule,
  ],
  controllers: [HealthController],
})
export class AppModule {}
