/**
 * 来源类型枚举
 * - FIXED_RATE: 固定比例分配（OPERATION 12%、PROVINCE 1%、CITY 2%）
 * - LEVEL_OVERFLOW: 层级溢出归总部（上线未解锁该级别）
 * - LEVEL_NO_ANCESTOR: 无上线归总部（该级无上线）
 * - BONUS_TIER_1/2/3: 团队奖励未解锁归总部
 */
export type SystemContributionSourceType =
  | 'FIXED_RATE'
  | 'LEVEL_OVERFLOW'
  | 'LEVEL_NO_ANCESTOR'
  | 'BONUS_TIER_1'
  | 'BONUS_TIER_2'
  | 'BONUS_TIER_3';

/**
 * 系统账户算力明细创建事件
 * 用于将系统账户的每笔算力来源明细同步到 mining-admin-service
 */
export class SystemContributionRecordCreatedEvent {
  static readonly EVENT_TYPE = 'SystemContributionRecordCreated';
  static readonly AGGREGATE_TYPE = 'SystemContributionRecord';

  constructor(
    public readonly recordId: bigint,                  // 明细记录ID
    public readonly accountType: string,              // 系统账户类型（OPERATION/PROVINCE/CITY/HEADQUARTERS）
    public readonly regionCode: string | null,        // 区域代码（省/市代码，如 440000, 440100）
    public readonly sourceAdoptionId: bigint,          // 来源认种ID
    public readonly sourceAccountSequence: string,     // 认种人账号
    public readonly sourceType: SystemContributionSourceType, // 来源类型
    public readonly levelDepth: number | null,         // 层级深度（1-15），仅对 LEVEL_OVERFLOW/LEVEL_NO_ANCESTOR 有效
    public readonly distributionRate: number,          // 分配比例
    public readonly amount: string,                    // 算力金额
    public readonly effectiveDate: Date,               // 生效日期
    public readonly expireDate: Date | null,           // 过期日期
    public readonly createdAt: Date,                   // 创建时间
  ) {}

  toPayload(): Record<string, any> {
    return {
      eventType: SystemContributionRecordCreatedEvent.EVENT_TYPE,
      recordId: this.recordId.toString(),
      accountType: this.accountType,
      regionCode: this.regionCode,
      sourceAdoptionId: this.sourceAdoptionId.toString(),
      sourceAccountSequence: this.sourceAccountSequence,
      sourceType: this.sourceType,
      levelDepth: this.levelDepth,
      distributionRate: this.distributionRate,
      amount: this.amount,
      effectiveDate: this.effectiveDate.toISOString(),
      expireDate: this.expireDate?.toISOString() ?? null,
      createdAt: this.createdAt.toISOString(),
    };
  }
}
