import { Controller, Logger } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';

/**
 * Saga 事件消费者
 * 接收来自各服务的 Saga 步骤完成/失败确认
 * 具体的 Saga 推进逻辑将在 Application 层的 SagaOrchestrator 中实现
 */
@Controller()
export class SagaEventConsumer {
  private readonly logger = new Logger(SagaEventConsumer.name);

  /**
   * planting-service: 树锁定完成确认
   */
  @MessagePattern('transfer.trees.lock.ack')
  async handleTreesLockAck(@Payload() message: any): Promise<void> {
    this.logger.log(`[SAGA] Trees lock ack received: ${JSON.stringify(message)}`);
    // 将在 SagaOrchestrator 注入后委托处理
  }

  /**
   * planting-service: 所有权变更完成确认
   */
  @MessagePattern('planting.transfer.completed')
  async handleOwnershipTransferCompleted(@Payload() message: any): Promise<void> {
    this.logger.log(`[SAGA] Ownership transfer completed: ${JSON.stringify(message)}`);
  }

  /**
   * contribution-service: 算力调整完成确认
   */
  @MessagePattern('contribution.transfer.adjusted')
  async handleContributionAdjusted(@Payload() message: any): Promise<void> {
    this.logger.log(`[SAGA] Contribution adjusted: ${JSON.stringify(message)}`);
  }

  /**
   * referral-service: 团队统计更新完成确认
   */
  @MessagePattern('referral.transfer.stats-updated')
  async handleStatsUpdated(@Payload() message: any): Promise<void> {
    this.logger.log(`[SAGA] Team stats updated: ${JSON.stringify(message)}`);
  }

  /**
   * planting-service: 树解锁完成确认（补偿）
   */
  @MessagePattern('transfer.trees.unlock.ack')
  async handleTreesUnlockAck(@Payload() message: any): Promise<void> {
    this.logger.log(`[SAGA] Trees unlock ack received: ${JSON.stringify(message)}`);
  }
}
