
import { MiddlewareConsumer, Module, type NestModule } from '@nestjs/common';
import { AnalyticsModule } from './domain/analytics/AnalyticsModule';
import { AuthModule } from './domain/auth/AuthModule';
import { BootstrapModule } from './domain/bootstrap/BootstrapModule';
import { DashboardModule } from './domain/dashboard/DashboardModule';
import { DatabaseModule } from './domain/database/DatabaseModule';
import { DriverModule } from './domain/driver/DriverModule';
import { HelloModule } from './domain/hello/HelloModule';
import { LeagueModule } from './domain/league/LeagueModule';
import { LoggingModule } from './domain/logging/LoggingModule';
import { MediaModule } from './domain/media/MediaModule';
import { PaymentsModule } from './domain/payments/PaymentsModule';
import { PolicyModule } from './domain/policy/PolicyModule';
import { ProtestsModule } from './domain/protests/ProtestsModule';
import { RaceModule } from './domain/race/RaceModule';
import { SponsorModule } from './domain/sponsor/SponsorModule';
import { TeamModule } from './domain/team/TeamModule';

import { getApiPersistence, getEnableBootstrap } from './env';
import { RequestContextMiddleware } from './shared/http/RequestContext';

const API_PERSISTENCE = getApiPersistence();
const USE_DATABASE = API_PERSISTENCE === 'postgres';

// Keep bootstrap on by default; tests can disable explicitly.
const ENABLE_BOOTSTRAP = getEnableBootstrap();

@Module({
  imports: [
    HelloModule,
    ...(USE_DATABASE ? [DatabaseModule] : []),
    LoggingModule,
    ...(ENABLE_BOOTSTRAP ? [BootstrapModule] : []),
    AnalyticsModule,
    AuthModule,
    DashboardModule,
    LeagueModule,
    RaceModule,
    ProtestsModule,
    TeamModule,
    SponsorModule,
    DriverModule,
    MediaModule,
    PaymentsModule,
    PolicyModule,
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    consumer.apply(RequestContextMiddleware).forRoutes('*');
  }
}
