import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
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 { AiModule } from './modules/ai/ai.module';
import { StorageModule } from './infrastructure/storage/storage.module';
import { LoggerModule } from './infrastructure/logger/logger.module';

import { AiAnalysisWorker } from './workers/ai-analysis.worker';
import { DocumentImportWorker } from './workers/document-import.worker';
import { NotificationWorker } from './workers/notification.worker';
import { AiInteractiveJobWorker } from './workers/ai-interactive.worker';
import { AiBackgroundJobWorker } from './workers/ai-background.worker';

import { AuditLogProcessor } from './modules/admin-audit-log/audit-log.processor';
import { FileCleanupProcessor } from './modules/files/file-cleanup.processor';
import { WorkerHeartbeatService } from './workers/worker-heartbeat.service';
import { OutboxDispatcher } from './modules/ai-job/outbox-dispatcher.service';
import { OutboxRepository } from './infrastructure/outbox/outbox.repository';

import { AiAnalysisModule } from './modules/ai-analysis/ai-analysis.module';
import { AiJobModule } from './modules/ai-job/ai-job.module';
import { DocumentImportModule } from './modules/document-import/document-import.module';
import { KnowledgeItemsModule } from './modules/knowledge-items/knowledge-items.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { EventBusModule } from './common/event-bus/event-bus.module';
import { FocusItemsModule } from './modules/focus-items/focus-items.module';
import { ReviewModule } from './modules/review/review.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,
    QueueModule,
    AiModule,
    StorageModule,
    LoggerModule,
    AiAnalysisModule,
    AiJobModule,  // M-AI-04: provides AI_JOB_EXECUTION_ENGINE, JobDefinitionRegistry, OutboxRepository
    DocumentImportModule,
    KnowledgeItemsModule,
    NotificationsModule,
    EventBusModule,
    FocusItemsModule,
    ReviewModule,
  ],
  providers: [
    AiAnalysisWorker,
    DocumentImportWorker,
    NotificationWorker,
    AiInteractiveJobWorker,
    AiBackgroundJobWorker,
    AuditLogProcessor,
    FileCleanupProcessor,
    WorkerHeartbeatService,
    OutboxRepository,
    OutboxDispatcher,
  ],
})
export class WorkerModule {}
