import Decimal from 'decimal.js';

/**
 * 分配比例值对象
 * 表示贡献值分配的百分比
 */
export class DistributionRate {
  private readonly _value: Decimal;

  // 预定义的分配比例
  static readonly PERSONAL = new DistributionRate(0.70);           // 70% 个人
  static readonly OPERATION = new DistributionRate(0.12);          // 12% 运营
  static readonly PROVINCE = new DistributionRate(0.01);           // 1% 省公司
  static readonly CITY = new DistributionRate(0.02);               // 2% 市公司
  static readonly TEAM_TOTAL = new DistributionRate(0.15);         // 15% 团队
  static readonly LEVEL_PER = new DistributionRate(0.005);         // 0.5% 每级
  static readonly BONUS_PER = new DistributionRate(0.025);         // 2.5% 每档

  constructor(value: Decimal | string | number) {
    this._value = new Decimal(value);
    if (this._value.isNaN()) {
      throw new Error('DistributionRate must be a valid number');
    }
    if (this._value.isNegative()) {
      throw new Error('DistributionRate cannot be negative');
    }
    if (this._value.greaterThan(1)) {
      throw new Error('DistributionRate cannot exceed 1 (100%)');
    }
  }

  get value(): Decimal {
    return this._value;
  }

  get asPercentage(): number {
    return this._value.times(100).toNumber();
  }

  multiply(amount: Decimal | string | number): Decimal {
    return this._value.times(new Decimal(amount));
  }

  equals(other: DistributionRate): boolean {
    return this._value.equals(other._value);
  }

  toString(): string {
    return `${this.asPercentage}%`;
  }

  static create(value: Decimal | string | number): DistributionRate {
    return new DistributionRate(value);
  }

  static fromPercentage(percentage: number): DistributionRate {
    return new DistributionRate(percentage / 100);
  }
}
