import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { ScheduleModule } from '@nestjs/schedule';
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';

// Domain entities
import { TelemetryEvent } from './domain/entities/telemetry-event.entity';
import { OnlineSnapshot } from './domain/entities/online-snapshot.entity';
import { DailyActiveStats } from './domain/entities/daily-active-stats.entity';

// Domain repositories
import { TELEMETRY_EVENT_REPOSITORY } from './domain/repositories/telemetry-event.repository.interface';
import { ONLINE_SNAPSHOT_REPOSITORY } from './domain/repositories/online-snapshot.repository.interface';
import { DAILY_ACTIVE_STATS_REPOSITORY } from './domain/repositories/daily-active-stats.repository.interface';

// Domain ports
import { PRESENCE_SERVICE } from './domain/ports/presence.service.interface';
import { TELEMETRY_METRICS_SERVICE } from './domain/ports/telemetry-metrics.service.interface';
import { TELEMETRY_PRODUCER_SERVICE } from './domain/ports/telemetry-producer.service.interface';

// Infrastructure
import { TelemetryEventRepository } from './infrastructure/persistence/telemetry-event.repository';
import { OnlineSnapshotRepository } from './infrastructure/persistence/online-snapshot.repository';
import { DailyActiveStatsRepository } from './infrastructure/persistence/daily-active-stats.repository';
import { PresenceRedisService } from './infrastructure/redis/presence-redis.service';
import { TelemetryProducerService } from './infrastructure/kafka/telemetry-producer.service';
import { TelemetryMetricsService } from './infrastructure/metrics/telemetry-metrics.service';

// Application services
import { TelemetryService } from './application/services/telemetry.service';
import { TelemetrySchedulerService } from './application/services/telemetry-scheduler.service';

// Shared guards
import { JwtAuthGuard } from './shared/guards/jwt-auth.guard';
import { AdminGuard } from './shared/guards/admin.guard';

// Interface - Controllers
import { TelemetryController } from './interface/http/controllers/telemetry.controller';
import { AdminTelemetryController } from './interface/http/controllers/admin-telemetry.controller';
import { MetricsController } from './interface/http/controllers/metrics.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([
      TelemetryEvent,
      OnlineSnapshot,
      DailyActiveStats,
    ]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({
      secret: process.env.JWT_ACCESS_SECRET || 'dev-access-secret',
    }),
    ScheduleModule.forRoot(),
  ],
  controllers: [
    TelemetryController,
    AdminTelemetryController,
    MetricsController,
    HealthController,
  ],
  providers: [
    JwtStrategy,
    JwtAuthGuard,
    AdminGuard,
    { provide: TELEMETRY_EVENT_REPOSITORY, useClass: TelemetryEventRepository },
    { provide: ONLINE_SNAPSHOT_REPOSITORY, useClass: OnlineSnapshotRepository },
    { provide: DAILY_ACTIVE_STATS_REPOSITORY, useClass: DailyActiveStatsRepository },
    { provide: PRESENCE_SERVICE, useClass: PresenceRedisService },
    { provide: TELEMETRY_PRODUCER_SERVICE, useClass: TelemetryProducerService },
    { provide: TELEMETRY_METRICS_SERVICE, useClass: TelemetryMetricsService },
    TelemetryService,
    TelemetrySchedulerService,
  ],
  exports: [TelemetryService],
})
export class TelemetryModule {}
