import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
import { JwtModule } from '@nestjs/jwt';

import { PrismaModule } from './infrastructure/database/prisma.module';
import { RedisModule } from './infrastructure/redis/redis.module';
import { QueueModule } from './infrastructure/queue/queue.module';
import { EventBusModule } from './common/event-bus/event-bus.module';
import { AiModule } from './modules/ai/ai.module';
import { StorageModule } from './infrastructure/storage/storage.module';
import { LoggerModule } from './infrastructure/logger/logger.module';

import { SystemModule } from './modules/system/system.module';
import { AuthModule } from './modules/auth/auth.module';
import { AdminAuthModule } from './modules/admin-auth/admin-auth.module';
import { AdminDashboardModule } from './modules/admin-dashboard/admin-dashboard.module';
import { AdminUsersModule } from './modules/admin-users/admin-users.module';
import { ContentSafetyModule } from './modules/content-safety/content-safety.module';
import { SecretModule } from './modules/secret/secret.module';
import { QuotaModule } from './modules/quota/quota.module';
import { AdminMetricsModule } from './modules/admin-metrics/admin-metrics.module';
import { AdminThrottleModule } from './modules/admin-throttle/admin-throttle.module';
import { AppConfigModule } from './modules/config/config.module';
import { AdminEventsModule } from './modules/admin-events/admin-events.module';
import { AdminKnowledgeModule } from './modules/admin-knowledge/admin-knowledge.module';
import { AdminCostsModule } from './modules/admin-costs/admin-costs.module';
import { AdminBillingModule } from './modules/admin-billing/admin-billing.module';

import { AdminServersModule } from './modules/admin-servers/admin-servers.module';
import { AdminConversationModule } from './modules/admin-conversation/admin-conversation.module';
import { AdminAiChatModule } from './modules/admin-ai-chat/admin-ai-chat.module';
import { AdminAuditLogModule } from './modules/admin-audit-log/admin-audit-log.module';
import { UsersModule } from './modules/users/users.module';
import { MembershipModule } from './modules/membership/membership.module';
import { KnowledgeBaseModule } from './modules/knowledge-base/knowledge-base.module';
import { KnowledgeItemsModule } from './modules/knowledge-items/knowledge-items.module';
import { DocumentImportModule } from './modules/document-import/document-import.module';
import { LearningSessionModule } from './modules/learning-session/learning-session.module';
import { ActiveRecallModule } from './modules/active-recall/active-recall.module';
import { AiAnalysisModule } from './modules/ai-analysis/ai-analysis.module';
import { ReviewModule } from './modules/review/review.module';
import { FocusItemsModule } from './modules/focus-items/focus-items.module';
import { LearningActivityModule } from './modules/learning-activity/learning-activity.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { FeedbackModule } from './modules/feedback/feedback.module';
import { FilesModule } from './modules/files/files.module';
import { WaitlistModule } from './modules/waitlist/waitlist.module';
import { WorkspaceModule } from './modules/workspace/workspace.module';
import { KnowledgeSourceModule } from './modules/knowledge-source/knowledge-source.module';
import { ImportCandidateModule } from './modules/import-candidate/import-candidate.module';
import { RagModule } from './modules/rag/rag.module';
import { RagChatModule } from './modules/rag-chat/rag-chat.module';
import { VectorModule } from './modules/vector/vector.module';
import { CacheModule } from './common/cache/cache.module';
import { AdminCacheModule } from './modules/admin-cache/admin-cache.module';
import { BackupModule } from './modules/backup/backup.module';
import { ReportingModule } from './modules/reporting/reporting.module';
import { ProjectCenterModule } from './modules/project-center/project-center.module';
import { HermesAgentModule } from './modules/hermes-agent/hermes-agent.module';
import { ReleaseModule } from './modules/release/release.module';
import { ComplianceModule } from './modules/compliance/compliance.module';
import { AdminNotificationsModule } from './modules/admin-notifications/admin-notifications.module';
import { QuizModule } from './modules/quiz/quiz.module';
import { ReadingEventModule } from './modules/reading-event/reading-event.module';
import { ReadingModule } from './modules/reading/reading.module';
import { MaterialReadingProgressModule } from './modules/material-reading-progress/material-reading-progress.module';
import { LearningRecordModule } from './modules/learning-record/learning-record.module';
import { TemporaryReadingMaterialModule } from './modules/temporary-reading-material/temporary-reading-material.module';
import { AiRuntimeModule } from './modules/ai-runtime/ai-runtime.module';
import { AiJobModule } from './modules/ai-job/ai-job.module';

import { JwtAuthGuard } from './common/guards/jwt-auth.guard';
import { RolesGuard } from './common/guards/roles.guard';
import { RateLimitGuard } from './common/guards/rate-limit.guard';
import { GlobalExceptionFilter } from './common/filters/global-exception.filter';
import { StrictValidationPipe } from './common/pipes/strict-validation.pipe';
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
import { TraceIdInterceptor } from './common/interceptors/trace-id.interceptor';
import { LoggingInterceptor } from './common/interceptors/logging.interceptor';
import { MetricsInterceptor } from './common/interceptors/metrics.interceptor';
import { TimeoutInterceptor } from './common/interceptors/timeout.interceptor';
import { AppThrottleModule } from './common/throttle/throttle.module';


import appConfig from './config/app.config';
import databaseConfig from './config/database.config';
import redisConfig from './config/redis.config';
import jwtConfig from './config/jwt.config';
import aiConfig from './config/ai.config';
import storageConfig from './config/storage.config';
import appleConfig from './config/apple.config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [
        appConfig,
        databaseConfig,
        redisConfig,
        jwtConfig,
        aiConfig,
        storageConfig,
        appleConfig,
      ],
    }),
    JwtModule.registerAsync({
      global: true,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        secret: config.get<string>('jwt.secret'),
        signOptions: { expiresIn: config.get<string>('jwt.expiresIn', '1h') as any },
      }),
    }),
    PrismaModule,
    RedisModule,
    AppThrottleModule,
    AppThrottleModule,
    EventBusModule,
    QueueModule,
    AiModule,
    StorageModule,
    LoggerModule,
    SystemModule,
    AuthModule,
    AdminAuthModule,
    AdminDashboardModule,
    AdminUsersModule,
    ContentSafetyModule,
    SecretModule,
    QuotaModule,
    AdminMetricsModule,
    AdminThrottleModule,
    AppConfigModule,
    AdminEventsModule,
    AdminKnowledgeModule,
    AdminCostsModule,
    AdminBillingModule,
    AdminServersModule,
    AdminConversationModule,
    AdminAiChatModule,
    AdminAuditLogModule,
    UsersModule,
    MembershipModule,
    KnowledgeBaseModule,
    KnowledgeItemsModule,
    KnowledgeSourceModule,
    ImportCandidateModule,
    DocumentImportModule,
    RagModule,
    RagChatModule,
    VectorModule,
    LearningSessionModule,
    ActiveRecallModule,
    AiAnalysisModule,
    ReviewModule,
    FocusItemsModule,
    LearningActivityModule,
    NotificationsModule,
    FeedbackModule,
    FilesModule,
    WaitlistModule,
    WorkspaceModule,
    CacheModule,
    AdminCacheModule,
    BackupModule,
    ReportingModule,
    ProjectCenterModule,
    HermesAgentModule,
    ReleaseModule,
    ComplianceModule,
    AdminNotificationsModule,
    QuizModule,
    // ── M8 Learning Info ──
    ReadingEventModule,
    ReadingModule,
    MaterialReadingProgressModule,
    LearningRecordModule,
    TemporaryReadingMaterialModule,
    AiRuntimeModule,
    AiJobModule,
  ],
  providers: [
    { provide: APP_GUARD, useClass: RateLimitGuard },
    { provide: APP_GUARD, useClass: JwtAuthGuard },
    { provide: APP_GUARD, useClass: RolesGuard },
    { provide: APP_FILTER, useClass: GlobalExceptionFilter },
    { provide: APP_PIPE, useClass: StrictValidationPipe },
    { provide: APP_INTERCEPTOR, useClass: TraceIdInterceptor },
    { provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },
    { provide: APP_INTERCEPTOR, useClass: MetricsInterceptor },
    { provide: APP_INTERCEPTOR, useClass: TimeoutInterceptor },
    { provide: APP_INTERCEPTOR, useClass: ResponseInterceptor },
  ],
})

export class AppModule {}
