/**
 * 龙虎榜分值值对象
 *
 * 计算公式: 团队总认种量 - 最大单个直推团队认种量
 *
 * 这个公式的设计目的:
 * - 鼓励均衡发展团队，而不是只依赖单个大团队
 * - 防止"烧伤"现象（单腿发展）
 */
export class LeaderboardScore {
  private constructor(
    public readonly totalTeamCount: number,
    public readonly maxDirectTeamCount: number,
    public readonly score: number,
  ) {}

  static calculate(totalTeamCount: number, directTeamCounts: number[]): LeaderboardScore {
    const maxDirectTeamCount = directTeamCounts.length > 0 ? Math.max(...directTeamCounts) : 0;
    const score = Math.max(0, totalTeamCount - maxDirectTeamCount);

    return new LeaderboardScore(totalTeamCount, maxDirectTeamCount, score);
  }

  static zero(): LeaderboardScore {
    return new LeaderboardScore(0, 0, 0);
  }

  /**
   * 当团队认种发生变化时重新计算
   */
  recalculate(newTotalTeamCount: number, newDirectTeamCounts: number[]): LeaderboardScore {
    return LeaderboardScore.calculate(newTotalTeamCount, newDirectTeamCounts);
  }

  /**
   * 比较排名 (降序排列)
   */
  compareTo(other: LeaderboardScore): number {
    return other.score - this.score;
  }
}
