import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import { AiModule } from './ai/ai.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { FileService } from './file/file.service';
import { GeoModule } from './geo/geo.module';
import { ImageModule } from './image/image.module';
import { ListingsModule } from './listings/listings.module';
import { LogController } from './log/log.controller';
import { LogModule } from './log/log.module';

import { EventModule } from './event/event.module';
import { MailModule } from './mail/mail.module';

import { ConfigModule } from '@nestjs/config';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ClsMiddleware, ClsModule } from 'nestjs-cls';
import path from 'path';
import { AuthService } from './auth/auth.service';
import { FirebaseAdminModule } from './firebase-admin/firebase-admin.module';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { UserInterceptor } from './interceptors/user.interceptor';
import { RequestDurationMiddleware } from './request-duration/request-duration.middleware';
import { SelectOptionsModule } from './select-options/select-options.module';
import { SitemapModule } from './sitemap/sitemap.module';
import { UserModule } from './user/user.module';

//loadEnvFiles();
console.log('Loaded environment variables:');
//console.log(JSON.stringify(process.env, null, 2));
@Module({
  imports: [
    ClsModule.forRoot({
      global: true, // Macht den ClsService global verfügbar
      middleware: { mount: true }, // Registriert automatisch die ClsMiddleware
    }),
    //ConfigModule.forRoot({ envFilePath: '.env' }),
    ConfigModule.forRoot({
      envFilePath: [path.resolve(__dirname, '..', '.env')],
    }),
    MailModule,
    AuthModule,
    WinstonModule.forRoot({
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp({
              format: 'YYYY-MM-DD hh:mm:ss.SSS A',
            }),
            winston.format.ms(),
            nestWinstonModuleUtilities.format.nestLike('Bizmatch', {
              colors: true,
              prettyPrint: true,
            }),
          ),
        }),
        // other transports...
      ],
      // other options
    }),
    GeoModule,
    UserModule,
    ListingsModule,
    SelectOptionsModule,
    ImageModule,
    AiModule,
    LogModule,
    // PaymentModule,
    EventModule,
    FirebaseAdminModule,
    SitemapModule,
  ],
  controllers: [AppController, LogController],
  providers: [
    AppService,
    FileService,
    {
      provide: APP_INTERCEPTOR,
      useClass: UserInterceptor, // Registriere den Interceptor global
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor, // Registriere den LoggingInterceptor global
    },
    AuthService,
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(ClsMiddleware).forRoutes('*');
    consumer.apply(RequestDurationMiddleware).forRoutes('*');
  }
}
