import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { HttpModule } from '@nestjs/axios';
import { DatabaseModule } from '@it0/database';
import { RedisEventBus } from '@it0/events';
import { HealthCheckController } from './interfaces/rest/controllers/health-check.controller';
import { AlertController } from './interfaces/rest/controllers/alert.controller';
import { MetricsController } from './interfaces/rest/controllers/metrics.controller';
import { AlertRuleRepository } from './infrastructure/repositories/alert-rule.repository';
import { AlertEventRepository } from './infrastructure/repositories/alert-event.repository';
import { MetricSnapshotRepository } from './infrastructure/repositories/metric-snapshot.repository';
import { HealthCheckResultRepository } from './infrastructure/repositories/health-check-result.repository';
import { SshCollector } from './infrastructure/collectors/ssh-collector';
import { PrometheusCollector } from './infrastructure/collectors/prometheus-collector';
import { AgentCollector } from './infrastructure/collectors/agent-collector';
import { HealthCheckService } from './domain/services/health-check.service';
import { AlertRunbookBridgeService } from './domain/services/alert-runbook-bridge.service';
import { AlertRule } from './domain/entities/alert-rule.entity';
import { AlertEvent } from './domain/entities/alert-event.entity';
import { HealthCheckResult } from './domain/entities/health-check-result.entity';
import { MetricSnapshot } from './domain/entities/metric-snapshot.entity';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    DatabaseModule.forRoot(),
    TypeOrmModule.forFeature([AlertRule, AlertEvent, HealthCheckResult, MetricSnapshot]),
    ScheduleModule.forRoot(),
    HttpModule.register({ timeout: 10_000 }),
  ],
  controllers: [HealthCheckController, AlertController, MetricsController],
  providers: [
    AlertRuleRepository,
    AlertEventRepository,
    MetricSnapshotRepository,
    HealthCheckResultRepository,
    SshCollector,
    PrometheusCollector,
    AgentCollector,
    HealthCheckService,
    AlertRunbookBridgeService,
    {
      provide: RedisEventBus,
      useFactory: (configService: ConfigService) => {
        const redisUrl = configService.get<string>('REDIS_URL', 'redis://localhost:6379');
        return new RedisEventBus(redisUrl);
      },
      inject: [ConfigService],
    },
  ],
})
export class MonitorModule {}
