import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { HealthModule } from './health/health.module';
import { TrendsModule } from './trends/trends.module';
import { FunnelsModule } from './funnels/funnels.module';
import { CohortsModule } from './cohorts/cohorts.module';
import { RevenueModule } from './revenue/revenue.module';
import { GdprModule } from './gdpr/gdpr.module';
import { SessionsModule } from './sessions/sessions.module';
import { AcquisitionModule } from './acquisition/acquisition.module';
import { EngagementModule } from './engagement/engagement.module';
import { AudienceModule } from './audience/audience.module';
import { SegmentsModule } from './segments/segments.module';
import { SecurityIpLogModule } from './security-ip-log/security-ip-log.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.local', '.env'],
    }),

    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        type: 'postgres',
        host: config.get('DATABASE_HOST', 'localhost'),
        port: config.get('DATABASE_PORT', 5432),
        username: config.get('DATABASE_USER', 'analytics'),
        password: config.get('DATABASE_PASSWORD', 'analytics'),
        database: config.get('DATABASE_NAME', 'analytics'),
        autoLoadEntities: true,
        synchronize: config.get('NODE_ENV') !== 'production',
        logging: config.get('NODE_ENV') !== 'production',
      }),
    }),

    // Health check
    HealthModule,

    // Core analytics modules
    TrendsModule,
    FunnelsModule,
    CohortsModule,
    RevenueModule,

    // GA-equivalent modules
    SessionsModule,
    AcquisitionModule,
    EngagementModule,
    AudienceModule,
    SegmentsModule,

    // GDPR compliance
    GdprModule,

    // Security IP logging (law enforcement / fraud prevention)
    SecurityIpLogModule,
  ],
})
export class AppModule {}
