import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ThrottlerModule } from '@nestjs/throttler';
import { SentryModule } from '@sentry/nestjs/setup';
import configurations from './configs/configurations';
import { AuthModule } from './modules/auth/auth.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AdminModule } from './admin/admin.module';
import { PrismaModule } from '../prisma/prisma.module'; // ✅
import { PatientsModule } from './modules/patients/patients.module';
import { StaffModule } from './modules/staff/staff.module';
import { OrganizationModule } from './modules/organization/organization.module';
import { AppointmentsModule } from './modules/appointments/appointments.module';
import { TreatmentsModule } from './modules/treatments/treatments.module';
import { CasesModule } from './modules/cases/cases.module';
import { TasksModule } from './modules/tasks/tasks.module';
import { TreatmentCatalogModule } from './modules/treatment-catalog/treatment-catalog.module';
import { CatalogModule } from './modules/catalog/catalog.module';
import { ProsthesisCatalogModule } from './modules/prosthesis-catalog/prosthesis-catalog.module';
import { LabCaseCommentsModule } from './modules/lab-case-comments/lab-case-comments.module';
import { TodayModule } from './modules/today/today.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { RealtimeModule } from './realtime/realtime.module';
import { VoiceModule } from './modules/voice/voice.module';

@Module({
  imports: [
    SentryModule.forRoot(),
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configurations],
    }),
    // First use of @nestjs/throttler in this app. Deliberately NOT bound as a global
    // APP_GUARD: a globally-bound ThrottlerGuard rate-limits every route against every
    // named throttler, which would cap the whole API at the voice limit. ThrottlerGuard
    // is applied to the one expensive route instead, so nothing else changes behaviour.
    ThrottlerModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        throttlers: [
          {
            name: 'voice',
            ttl: (config.get<number>('voice.throttle.ttl') ?? 60) * 1000,
            limit: config.get<number>('voice.throttle.limit') ?? 6,
          },
        ],
      }),
    }),
    PrismaModule, // ✅ ADD THIS
    CatalogModule,
    TreatmentCatalogModule,
    ProsthesisCatalogModule,
    AuthModule,
    PatientsModule,
    AppointmentsModule,
    TreatmentsModule,
    CasesModule,
    TasksModule,
    LabCaseCommentsModule,
    StaffModule,
    OrganizationModule,
    TodayModule,
    NotificationsModule,
    RealtimeModule,
    VoiceModule,
    AdminModule.forRoot(),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
