import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { ConfigModule } from '@nestjs/config';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { ScheduleModule as NestScheduleModule } from '@nestjs/schedule';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { configuration } from './config';
import { PrismaModule } from './prisma';
import { QueueModule } from './queue';
import { AuthModule } from './modules/auth';
import { UsersModule } from './modules/users';
import { TenantMiddleware } from './common/middleware/tenant.middleware';

import { ClientsModule } from './modules/clients';
import { ScheduleModule } from './modules/schedule';
import { FunnelModule } from './modules/funnel';
import { SalesModule } from './modules/sales';
import { StatsModule } from './modules/stats';
import { NotificationsModule } from './modules/notifications';
import { ReportsModule } from './modules/reports';
import { WorkScheduleModule } from './modules/work-schedule';
import { SipModule } from './modules/sip';
import { WebhooksModule } from './modules/webhooks';
import { ClubsModule } from './modules/clubs';
import { LicensesModule } from './modules/licenses';
import { ModulesModule } from './modules/modules';
import { MeteringModule } from './modules/metering';
import { ProvisioningModule } from './modules/provisioning';
import { CatalogModule } from './modules/catalog';
import { DepartmentsModule } from './modules/departments';
import { RoomsModule } from './modules/rooms';
import { IntegrationModule } from './modules/integration';
import { AuditModule } from './modules/audit/audit.module';
import { EmailModule } from './modules/email';
import { ThemesModule } from './modules/themes';
import { CrmModule } from './modules/crm';
import { RolesModule } from './modules/roles/roles.module';
import { BookingModule } from './modules/booking';
import { TelegramBotModule } from './modules/telegram-bot';
import { ScreenshotsModule } from './modules/screenshots';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration],
    }),
    EventEmitterModule.forRoot(),
    NestScheduleModule.forRoot(),
    ThrottlerModule.forRoot({
      throttlers: [{ ttl: 60000, limit: 1000 }],
    }),
    QueueModule,
    PrismaModule,
    AuthModule,
    UsersModule,
    ClientsModule,
    ScheduleModule,
    FunnelModule,
    ModulesModule,
    MeteringModule,
    ProvisioningModule,
    NotificationsModule,
    SalesModule,
    StatsModule,
    WorkScheduleModule,
    SipModule,
    // New modules
    DepartmentsModule,
    RoomsModule,
    CatalogModule,
    ClubsModule,
    LicensesModule,
    ReportsModule,
    WebhooksModule,
    IntegrationModule,
    AuditModule,
    EmailModule,
    ThemesModule,
    CrmModule,
    RolesModule,
    BookingModule,
    TelegramBotModule,
    ScreenshotsModule,
  ],
  providers: [{ provide: APP_GUARD, useClass: ThrottlerGuard }],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    consumer
      .apply(TenantMiddleware)
      .exclude(
        'v1/auth/(.*)',
        'v1/crm/deals/from-form',
        'v1/crm/deals/webhook/(.*)',
        'v1/booking/public/(.*)',
        'telegram-bot/webhook/(.*)',
        'v1/screenshots/(.*)',
      )
      .forRoutes('*');
  }
}
