import { Module, type MiddlewareConsumer, NestModule } from "@nestjs/common";
import { APP_GUARD } from "@nestjs/core";
import { ExamsModule } from "./exams/exams.module.js";
import { HomeworkModule } from "./homework/homework.module.js";
import { GradesModule } from "./grades/grades.module.js";
import { AttendanceModule } from "./attendance/attendance.module.js";
import { ClassesModule } from "./classes/classes.module.js";
import { SchedulingModule } from "./scheduling/scheduling.module.js";
import { IamConsumerModule } from "./iam-consumer/iam-consumer.module.js";
import { HealthModule } from "./shared/health/health.module.js";
import { PermissionGuard } from "./middleware/permission.guard.js";
import { AuthMiddleware } from "./middleware/auth.middleware.js";
import { LifecycleService } from "./shared/lifecycle/lifecycle.service.js";

@Module({
  imports: [
    ExamsModule,
    HomeworkModule,
    GradesModule,
    AttendanceModule,
    ClassesModule,
    SchedulingModule,
    IamConsumerModule,
    HealthModule,
  ],
  providers: [
    { provide: APP_GUARD, useClass: PermissionGuard },
    AuthMiddleware,
    LifecycleService,
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    // 全局注册 AuthMiddleware（排除 /healthz 等健康检查路径）
    consumer.apply(AuthMiddleware).exclude("healthz").forRoutes("*");
  }
}
