import { Module, Global } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { EventPublisherService } from './event-publisher.service';
import { DepositEventConsumerService } from './deposit-event-consumer.service';
import { PlantingEventConsumerService } from './planting-event-consumer.service';
import { WithdrawalEventConsumerService } from './withdrawal-event-consumer.service';
// [2026-01-08] 新增：Identity 事件消费者 - 用户注册时创建钱包
import { IdentityEventConsumerService } from './identity-event-consumer.service';
// [2026-01-07] 新增：Outbox Pattern 实现
import { OutboxPublisherService } from './outbox-publisher.service';
import { OutboxRepository } from '../persistence/repositories/outbox.repository';
// [已屏蔽] 前端直接从 reward-service 查询，不再订阅 reward-service 消息
// import { RewardEventConsumerController } from './reward-event-consumer.controller';
// import { EventAckPublisher } from './event-ack.publisher';
import { PrismaService } from '../persistence/prisma/prisma.service';

@Global()
@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: 'KAFKA_SERVICE',
        imports: [ConfigModule],
        useFactory: (configService: ConfigService) => ({
          transport: Transport.KAFKA,
          options: {
            client: {
              clientId: configService.get<string>('KAFKA_CLIENT_ID', 'wallet-service'),
              brokers: configService.get<string>('KAFKA_BROKERS', 'localhost:9092').split(','),
            },
            consumer: {
              groupId: configService.get<string>('KAFKA_GROUP_ID', 'wallet-service-group'),
            },
          },
        }),
        inject: [ConfigService],
      },
    ]),
  ],
  // [已屏蔽] 前端直接从 reward-service 查询，不再订阅 reward-service 消息
  // controllers: [RewardEventConsumerController],
  controllers: [],
  providers: [
    PrismaService,
    EventPublisherService,
    DepositEventConsumerService,
    PlantingEventConsumerService,
    WithdrawalEventConsumerService,
    // [2026-01-08] 新增：Identity 事件消费者
    IdentityEventConsumerService,
    // [2026-01-07] 新增：Outbox Pattern
    OutboxRepository,
    OutboxPublisherService,
  ],
  exports: [
    EventPublisherService,
    DepositEventConsumerService,
    PlantingEventConsumerService,
    WithdrawalEventConsumerService,
    // [2026-01-08] 新增：导出 Identity 事件消费者
    IdentityEventConsumerService,
    ClientsModule,
    // [2026-01-07] 新增：导出 Outbox 相关服务
    OutboxRepository,
    OutboxPublisherService,
  ],
})
export class KafkaModule {}
