import { Module, Global } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { PrismaModule } from './persistence/prisma/prisma.module';
import { RedisService } from './redis/redis.service';
import { KafkaModule } from './kafka/kafka.module';
import { APP_VERSION_REPOSITORY } from '../domain/version-management';
import { AppVersionRepositoryImpl } from './persistence/repositories/app-version.repository.impl';
import { AppVersionMapper } from './persistence/mappers/app-version.mapper';
import { FileStorageService } from './storage/file-storage.service';
import { PackageParserService } from './parsers/package-parser.service';

@Global()
@Module({
  imports: [
    PrismaModule,
    KafkaModule,
    HttpModule.register({
      timeout: 10000,
      maxRedirects: 5,
    }),
  ],
  providers: [
    {
      provide: 'REDIS_OPTIONS',
      useFactory: (configService: ConfigService) => ({
        host: configService.get<string>('REDIS_HOST', 'localhost'),
        port: configService.get<number>('REDIS_PORT', 6379),
        password: configService.get<string>('REDIS_PASSWORD'),
        db: configService.get<number>('REDIS_DB', 13),
      }),
      inject: [ConfigService],
    },
    RedisService,
    // Version Management
    AppVersionMapper,
    {
      provide: APP_VERSION_REPOSITORY,
      useClass: AppVersionRepositoryImpl,
    },
    FileStorageService,
    PackageParserService,
  ],
  exports: [
    PrismaModule,
    RedisService,
    KafkaModule,
    HttpModule,
    APP_VERSION_REPOSITORY,
    AppVersionMapper,
    FileStorageService,
    PackageParserService,
  ],
})
export class InfrastructureModule {}
