import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { ServeStaticModule } from '@nestjs/serve-static';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { join } from 'path';

import { AuthModule } from './auth/auth.module';
import { StudentsModule } from './students/students.module';
import { ClassesModule } from './classes/classes.module';
import { SectionsModule } from './sections/sections.module';
import { AcademicYearModule } from './academic-year/academic-year.module';
import { AttendanceModule } from './attendance/attendance.module';
import { PaymentsModule } from './payments/payments.module';
import { NotificationsModule } from './notifications/notifications.module';
import { DashboardModule } from './dashboard/dashboard.module';
import { AdminModule } from './admin/admin.module';
import { TeachersModule } from './teachers/teachers.module';
import { SettingsModule } from './settings/settings.module';
import { DesignationsModule } from './designations/designations.module';
import { DropdownOptionsModule } from './dropdown-options/dropdown-options.module';
import { UsersModule } from './users/users.module';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuditModule } from './audit/audit.module';
import { FeesModule } from './fees/fees.module';
import { TransportModule } from './transport/transport.module';
import { InvoicesModule } from './invoices/invoices.module';
import { OpsModule } from './ops/ops.module';
import { AcademicYearMiddleware } from './common/middleware/academic-year.middleware';
import { AuditSyncMiddleware } from './common/middleware/audit-sync.middleware';
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    ThrottlerModule.forRoot([{ ttl: 60000, limit: 60 }]),

    // Serve /uploads/** (students, notifications, etc.)
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), 'uploads'),
      serveRoot: '/uploads',
    }),

    MongooseModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        uri: config.get<string>('MONGO_URI')!,
      }),
    }),

    // Feature Modules
    AuthModule,
    StudentsModule,
    ClassesModule,
    SectionsModule,
    AcademicYearModule,
    AttendanceModule,
    PaymentsModule,
    NotificationsModule,
    DashboardModule,
    AdminModule,
    TeachersModule,
    SettingsModule,
    DesignationsModule,
    DropdownOptionsModule,
    UsersModule,
    AuditModule,
    FeesModule,
    TransportModule,
    InvoicesModule,
    OpsModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    { provide: APP_FILTER, useClass: HttpExceptionFilter },
    { provide: APP_GUARD, useClass: ThrottlerGuard },
  ],
})

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AcademicYearMiddleware, AuditSyncMiddleware).forRoutes('*');
  }
}
