import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';

// Domain
import { AppVersion } from './domain/entities/app-version.entity';

// Application
import { AppVersionService } from './application/services/app-version.service';
import { FileStorageService } from './application/services/file-storage.service';

// Domain
import { APP_VERSION_REPOSITORY } from './domain/repositories/app-version.repository.interface';
import { PACKAGE_PARSER } from './domain/ports/package-parser.interface';

// Infrastructure
import { AppVersionRepository } from './infrastructure/persistence/app-version.repository';
import { PackageParserService } from './infrastructure/parsers/package-parser.service';

// Interface - Controllers
import { AppVersionController } from './interface/http/controllers/app-version.controller';
import { AdminVersionController } from './interface/http/controllers/admin-version.controller';
import { HealthController } from './interface/http/controllers/health.controller';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.DB_HOST || 'localhost',
      port: parseInt(process.env.DB_PORT || '5432', 10),
      username: process.env.DB_USERNAME || 'genex',
      password: process.env.DB_PASSWORD || 'genex_dev_password',
      database: process.env.DB_NAME || 'genex',
      autoLoadEntities: true,
      synchronize: false,
      logging: process.env.NODE_ENV === 'development',
      extra: {
        max: parseInt(process.env.DB_POOL_MAX || '20', 10),
        min: parseInt(process.env.DB_POOL_MIN || '5', 10),
      },
    }),
    TypeOrmModule.forFeature([AppVersion]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({
      secret: process.env.JWT_SECRET || 'genex-jwt-secret-dev',
      signOptions: { expiresIn: process.env.JWT_EXPIRES_IN || '24h' },
    }),
  ],
  controllers: [
    HealthController,
    AppVersionController,
    AdminVersionController,
  ],
  providers: [
    { provide: APP_VERSION_REPOSITORY, useClass: AppVersionRepository },
    AppVersionService,
    FileStorageService,
    { provide: PACKAGE_PARSER, useClass: PackageParserService },
  ],
})
export class AdminModule {}
