import { Global, Module } from '@nestjs/common';
import { BullModule } from '@nestjs/bullmq';
import { ConfigModule, ConfigService } from '@nestjs/config';
import type { AppConfig } from '../config/configuration';
import { PersonaModule } from '../modules/persona/persona.module';
import { PlanModule } from '../modules/plan/plan.module';
import { SyncModule } from '../modules/sync/sync.module';
import { QueueName } from './queue-names';
import { QueueProducer } from './queue-producer.service';
import { StaleScanService } from './stale-scan.service';
import { SyncIncrementalSchedulerService } from './sync-incremental.scheduler';
import { DwLagMonitorService } from './dw-lag-monitor.service';
import { DailyHealthReportService } from './daily-health-report.service';
import { DailyHealthReportController } from './daily-health-report.controller';
import { PersonaRecomputeProcessor } from './processors/persona-recompute.processor';
import { PlanRecomputeProcessor } from './processors/plan-recompute.processor';
import { PlanAssetGenerateProcessor } from './processors/plan-asset-generate.processor';

/**
 * Queues — BullMQ 三个队列的统一注册入口。
 *
 * 触发链路(详见 docs/three-layer-model.md):
 *   pipeline-dispatcher 写完 transaction
 *      → enqueue persona-recompute (jobName=patientId)
 *      → worker 串行跑 → 完成时
 *      → enqueue plan-recompute (jobName=patientId)
 *      → worker 跑出新 plan → 每条新 plan
 *      → enqueue plan-asset-generate (jobName=planId)
 *
 * 兜底:
 *   - 每日凌晨 @Cron(EVERY_DAY_AT_2AM) 扫 stale persona 重新 enqueue
 *   - sync.service @Cron(EVERY_HOUR) 拉数据,事件触发自动跟进
 *
 * Redis 连接:复用 ConfigService.redis.url。BullMQ 要求
 * `maxRetriesPerRequest=null` + `enableReadyCheck=false`(其他设置 ioredis 会报错)。
 */
@Global()
@Module({
  imports: [
    BullModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService<AppConfig, true>) => ({
        connection: {
          // ioredis 接受 URL,但 BullMQ 要求几个特殊 flag
          url: config.get('redis', { infer: true }).url,
          maxRetriesPerRequest: null,
          enableReadyCheck: false,
        },
      }),
    }),
    BullModule.registerQueue(
      { name: QueueName.PERSONA_RECOMPUTE },
      { name: QueueName.PLAN_RECOMPUTE },
      { name: QueueName.PLAN_ASSET_GENERATE },
    ),
    PersonaModule,
    PlanModule,
    SyncModule, // 给 SyncIncrementalSchedulerService 注入 ColdImportService
  ],
  controllers: [DailyHealthReportController],
  providers: [
    QueueProducer,
    StaleScanService,
    SyncIncrementalSchedulerService,
    DwLagMonitorService,
    DailyHealthReportService,
    PersonaRecomputeProcessor,
    PlanRecomputeProcessor,
    PlanAssetGenerateProcessor,
  ],
  exports: [BullModule, QueueProducer, StaleScanService, SyncIncrementalSchedulerService],
})
export class QueuesModule {}
