import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ScheduleModule } from '@nestjs/schedule';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';

// 配置模块
import appConfig from './config/app.config';
import databaseConfig from './config/database.config';
import redisConfig from './config/redis.config';
import jwtConfig from './config/jwt.config';
import marketDataConfig from './config/market-data.config';
import openaiConfig from './config/openai.config';

// 功能模块
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { MarketModule } from './market/market.module';
import { IndicatorsModule } from './indicators/indicators.module';
import { OptionsModule } from './options/options.module';
import { AiModule } from './ai/ai.module';
import { EventsModule } from './events/events.module';
import { WatchlistModule } from './watchlist/watchlist.module';
import { AlertModule } from './alert/alert.module';
import { CommonModule } from './common/common.module';

// 拦截器
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { LoggingInterceptor } from './common/interceptors/logging.interceptor';

@Module({
  imports: [
    // 配置模块
    ConfigModule.forRoot({
      isGlobal: true,
      load: [
        appConfig,
        databaseConfig,
        redisConfig,
        jwtConfig,
        marketDataConfig,
        openaiConfig,
      ],
      envFilePath: ['.env.local', '.env'],
    }),

    // 限流模块
    ThrottlerModule.forRoot([{
      ttl: 60000, // 1分钟
      limit: 100, // 每IP每分钟最多100请求
    }]),

    // 定时任务模块
    ScheduleModule.forRoot(),

    // 公共模块
    CommonModule,

    // 业务模块
    AuthModule,
    UserModule,
    MarketModule,
    IndicatorsModule,
    OptionsModule,
    AiModule,
    EventsModule,
    WatchlistModule,
    AlertModule,
  ],
  providers: [
    // 全局拦截器
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor,
    },
    // 全局限流守卫
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}
