import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { BidsModule } from './modules/bids/bids.module';
import { DatabasesModule } from './modules/databases/databases.module';
import { AppConfigsModule } from './modules/app-configs/app-configs.module';
import { AppValidatorsModule } from './modules/app-validators/app-validators.module';
import { AuthModule } from './modules/auth/auth.module';
import { AdminsModule } from './modules/admins/admins.module';
import { AuthenticationMiddleware } from './modules/auth/middlewares/authentication.middleware';
import {
  clientExcludeAuth,
  excludeAuth,
  excludeAuthor,
} from './system/routes/exclude-route';
import { AuthorizationMiddleware } from './modules/admins/middlewares/authorization.middleware';
import { ClientAuthenticationMiddleware } from './modules/auth/middlewares/client-authentication.middleware';
import { NotificationModule } from './modules/notification/notification.module';
import { ScrapsModule } from './modules/scraps/scraps.module';
import { ExtensionsModule } from './modules/extensions/extensions.module';
import { MailsModule } from './modules/mails/mails.module';

@Module({
  imports: [
    BidsModule,
    DatabasesModule,
    AppConfigsModule,
    AppValidatorsModule,
    AuthModule,
    AdminsModule,
    NotificationModule,
    ScrapsModule,
    ExtensionsModule,
    MailsModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(AuthenticationMiddleware)
      .exclude(...excludeAuth)
      .forRoutes({ path: 'admin/*', method: RequestMethod.ALL });

    consumer
      .apply(AuthorizationMiddleware)
      .exclude(...excludeAuthor)
      .forRoutes({ path: 'admin/*', method: RequestMethod.ALL });

    consumer
      .apply(ClientAuthenticationMiddleware)
      .exclude(...clientExcludeAuth)
      .forRoutes({ path: '/*', method: RequestMethod.ALL });
  }
}
