import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { APP_GUARD, APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';
import { PrismaModule } from './prisma/prisma.module';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { RolesModule } from './roles/roles.module';
import { PermissionsModule } from './permissions/permissions.module';
import { MenusModule } from './menus/menus.module';
import { DictModule } from './dict/dict.module';
import { ConfigModule as SystemConfigModule } from './config/config.module';
import { LogsModule } from './logs/logs.module';
import { TenantsModule } from './tenants/tenants.module';
import { SchoolModule } from './school/school.module';
import { ContestsModule } from './contests/contests.module';
import { JudgesManagementModule } from './judges-management/judges-management.module';
import { UploadModule } from './upload/upload.module';
import { HomeworkModule } from './homework/homework.module';
import { OssModule } from './oss/oss.module';
import { AI3DModule } from './ai-3d/ai-3d.module';
import { PublicModule } from './public/public.module';
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
import { RolesGuard } from './auth/guards/roles.guard';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { LoggingInterceptor } from './common/interceptors/logging.interceptor';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      // envFilePath 数组中第一个文件优先级最高
      envFilePath: [
        `.env.${process.env.NODE_ENV || 'development'}`, // 优先加载环境特定配置
        '.env', // 通用配置作为后备
      ],
    }),
    PrismaModule,
    AuthModule,
    UsersModule,
    RolesModule,
    PermissionsModule,
    MenusModule,
    DictModule,
    SystemConfigModule,
    LogsModule,
    TenantsModule,
    SchoolModule,
    ContestsModule,
    JudgesManagementModule,
    UploadModule,
    HomeworkModule,
    OssModule,
    AI3DModule,
    PublicModule,
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor, // 日志拦截器，先执行
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor, // 响应转换拦截器
    },
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
  ],
})
export class AppModule {}
