import { RankingScore } from '../../../src/domain/value-objects/ranking-score.vo';

describe('RankingScore', () => {
  describe('calculate', () => {
    it('应该正确计算龙虎榜分值', () => {
      // 用户A的团队数据：
      // - 团队总认种: 230棵
      // - 最大单个直推团队: 100棵
      // - 龙虎榜分值: 230 - 100 = 130
      const score = RankingScore.calculate(230, 100);

      expect(score.totalTeamPlanting).toBe(230);
      expect(score.maxDirectTeamPlanting).toBe(100);
      expect(score.effectiveScore).toBe(130);
    });

    it('当团队总认种等于最大直推时，有效分值为0', () => {
      const score = RankingScore.calculate(100, 100);
      expect(score.effectiveScore).toBe(0);
    });

    it('有效分值不能为负数', () => {
      const score = RankingScore.calculate(50, 100);
      expect(score.effectiveScore).toBe(0);
    });
  });

  describe('zero', () => {
    it('应该创建零分值', () => {
      const score = RankingScore.zero();

      expect(score.totalTeamPlanting).toBe(0);
      expect(score.maxDirectTeamPlanting).toBe(0);
      expect(score.effectiveScore).toBe(0);
    });
  });

  describe('compareTo', () => {
    it('分值高的应该排在前面', () => {
      const score1 = RankingScore.calculate(200, 50); // 有效分值: 150
      const score2 = RankingScore.calculate(150, 50); // 有效分值: 100

      expect(score1.compareTo(score2)).toBeLessThan(0); // score1 排名更靠前
    });

    it('相同分值应该返回0', () => {
      const score1 = RankingScore.calculate(200, 100);
      const score2 = RankingScore.calculate(200, 100);

      expect(score1.compareTo(score2)).toBe(0);
    });
  });

  describe('equals', () => {
    it('相同有效分值应该相等', () => {
      const score1 = RankingScore.calculate(200, 100);
      const score2 = RankingScore.calculate(200, 100);

      expect(score1.equals(score2)).toBe(true);
    });

    it('不同有效分值应该不相等', () => {
      const score1 = RankingScore.calculate(200, 100);
      const score2 = RankingScore.calculate(300, 100);

      expect(score1.equals(score2)).toBe(false);
    });
  });

  describe('hasEffectiveScore', () => {
    it('有分值时应该返回true', () => {
      const score = RankingScore.calculate(200, 100);
      expect(score.hasEffectiveScore()).toBe(true);
    });

    it('零分值时应该返回false', () => {
      const score = RankingScore.zero();
      expect(score.hasEffectiveScore()).toBe(false);
    });
  });

  describe('getMaxTeamRatio', () => {
    it('应该正确计算大腿占比', () => {
      const score = RankingScore.calculate(200, 100);
      expect(score.getMaxTeamRatio()).toBe(0.5);
    });

    it('团队总认种为0时占比为0', () => {
      const score = RankingScore.zero();
      expect(score.getMaxTeamRatio()).toBe(0);
    });
  });

  describe('isHealthyTeamStructure', () => {
    it('大腿占比低于50%应该是健康结构', () => {
      const score = RankingScore.calculate(300, 100); // 33.3%
      expect(score.isHealthyTeamStructure()).toBe(true);
    });

    it('大腿占比等于50%应该不是健康结构', () => {
      const score = RankingScore.calculate(200, 100); // 50%
      expect(score.isHealthyTeamStructure()).toBe(false);
    });

    it('大腿占比高于50%应该不是健康结构', () => {
      const score = RankingScore.calculate(200, 150); // 75%
      expect(score.isHealthyTeamStructure()).toBe(false);
    });
  });
});
