import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PrismaService } from '../persistence/prisma/prisma.service';
import { UserQueryRepositoryImpl } from '../persistence/repositories/user-query.repository.impl';
import { USER_QUERY_REPOSITORY } from '../../domain/repositories/user-query.repository';
import { UserEventConsumerService } from './user-event-consumer.service';
import { CdcConsumerService } from './cdc-consumer.service';
import { ReferralCdcConsumerService } from './referral-cdc-consumer.service';
import { WalletCdcConsumerService } from './wallet-cdc-consumer.service';
import { PlantingCdcConsumerService } from './planting-cdc-consumer.service';
import { AuthorizationCdcConsumerService } from './authorization-cdc-consumer.service';

/**
 * Kafka 模块
 *
 * CDC 消费者 - 通过 Debezium 从各服务同步数据：
 * 1. CdcConsumerService - identity-service 用户数据
 * 2. ReferralCdcConsumerService - referral-service 推荐关系
 * 3. WalletCdcConsumerService - wallet-service 钱包/提现数据
 * 4. PlantingCdcConsumerService - planting-service 认种/持仓/合同数据
 * 5. AuthorizationCdcConsumerService - authorization-service 授权/考核/系统账户数据
 *
 * Outbox 消费者 - 处理业务领域事件：
 * 1. UserEventConsumerService - 用户相关业务事件
 */
@Module({
  imports: [ConfigModule],
  providers: [
    PrismaService,
    {
      provide: USER_QUERY_REPOSITORY,
      useClass: UserQueryRepositoryImpl,
    },
    // Outbox 事件消费者 (业务领域事件)
    UserEventConsumerService,
    // CDC 消费者 - identity-service
    CdcConsumerService,
    // CDC 消费者 - referral-service
    ReferralCdcConsumerService,
    // CDC 消费者 - wallet-service
    WalletCdcConsumerService,
    // CDC 消费者 - planting-service
    PlantingCdcConsumerService,
    // CDC 消费者 - authorization-service
    AuthorizationCdcConsumerService,
  ],
  exports: [
    UserEventConsumerService,
    CdcConsumerService,
    ReferralCdcConsumerService,
    WalletCdcConsumerService,
    PlantingCdcConsumerService,
    AuthorizationCdcConsumerService,
    USER_QUERY_REPOSITORY,
  ],
})
export class KafkaModule {}
