import { Global, Module } from '@nestjs/common';
import { ThrottlerModule, ThrottlerStorage } from '@nestjs/throttler';
import { RedisThrottlerStorage } from './redis-throttler.storage';
import { RedisService } from '../../infrastructure/redis/redis.service';

@Global()
@Module({
  imports: [
    ThrottlerModule.forRootAsync({
      useFactory: (redis: RedisService) => ({
        throttlers: [
          { name: 'global', ttl: 60000, limit: 100 },
          { name: 'ai', ttl: 60000, limit: 20 },
          { name: 'login', ttl: 60000, limit: 5 },
          { name: 'upload', ttl: 60000, limit: 10 },
        ],
        storage: new RedisThrottlerStorage(redis),
      }),
      inject: [RedisService],
    }),
  ],
  providers: [RedisThrottlerStorage],
  exports: [ThrottlerModule],
})
export class AppThrottleModule {}
