import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ChainType } from '@/domain/value-objects';
import { ChainTypeEnum } from '@/domain/enums';

/**
 * 确认策略服务
 * 定义各链的确认数要求
 */
@Injectable()
export class ConfirmationPolicyService {
  private readonly defaultConfirmations: number;

  constructor(private readonly configService: ConfigService) {
    this.defaultConfirmations = this.configService.get<number>(
      'blockchain.confirmationsRequired',
      12,
    );
  }

  /**
   * 获取所需确认数
   */
  getRequiredConfirmations(chainType: ChainType): number {
    // 可以根据不同链配置不同的确认数
    switch (chainType.value) {
      case ChainTypeEnum.KAVA:
        return this.configService.get<number>('blockchain.kava.confirmations', 12);
      case ChainTypeEnum.BSC:
        return this.configService.get<number>('blockchain.bsc.confirmations', 15);
      default:
        return this.defaultConfirmations;
    }
  }

  /**
   * 检查是否已确认
   */
  isConfirmed(chainType: ChainType, confirmations: number): boolean {
    return confirmations >= this.getRequiredConfirmations(chainType);
  }
}
