import { LeaderboardConfig } from '../../../src/domain/aggregates/leaderboard-config/leaderboard-config.aggregate';
import { LeaderboardType } from '../../../src/domain/value-objects/leaderboard-type.enum';

describe('LeaderboardConfig', () => {
  describe('createDefault', () => {
    it('应该创建默认配置', () => {
      const config = LeaderboardConfig.createDefault();

      expect(config.configKey).toBe('GLOBAL');
      expect(config.dailyEnabled).toBe(true);
      expect(config.weeklyEnabled).toBe(true);
      expect(config.monthlyEnabled).toBe(true);
      expect(config.virtualRankingEnabled).toBe(false);
      expect(config.virtualAccountCount).toBe(0);
      expect(config.displayLimit).toBe(30);
      expect(config.refreshIntervalMinutes).toBe(5);
    });
  });

  describe('updateLeaderboardSwitch', () => {
    it('应该更新日榜开关', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateLeaderboardSwitch('daily', false, 'admin');

      expect(config.dailyEnabled).toBe(false);
      expect(config.domainEvents.length).toBe(1);
    });

    it('应该更新周榜开关', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateLeaderboardSwitch('weekly', false, 'admin');

      expect(config.weeklyEnabled).toBe(false);
    });

    it('应该更新月榜开关', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateLeaderboardSwitch('monthly', false, 'admin');

      expect(config.monthlyEnabled).toBe(false);
    });
  });

  describe('updateVirtualRankingSettings', () => {
    it('应该更新虚拟排名设置', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateVirtualRankingSettings(true, 30, 'admin');

      expect(config.virtualRankingEnabled).toBe(true);
      expect(config.virtualAccountCount).toBe(30);
    });

    it('虚拟账户数量为负数时应该抛出错误', () => {
      const config = LeaderboardConfig.createDefault();

      expect(() => {
        config.updateVirtualRankingSettings(true, -1, 'admin');
      }).toThrow('虚拟账户数量不能为负数');
    });
  });

  describe('updateDisplayLimit', () => {
    it('应该更新显示数量', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateDisplayLimit(50, 'admin');

      expect(config.displayLimit).toBe(50);
    });

    it('显示数量为0时应该抛出错误', () => {
      const config = LeaderboardConfig.createDefault();

      expect(() => {
        config.updateDisplayLimit(0, 'admin');
      }).toThrow('显示数量必须大于0');
    });

    it('显示数量为负数时应该抛出错误', () => {
      const config = LeaderboardConfig.createDefault();

      expect(() => {
        config.updateDisplayLimit(-1, 'admin');
      }).toThrow('显示数量必须大于0');
    });
  });

  describe('updateRefreshInterval', () => {
    it('应该更新刷新间隔', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateRefreshInterval(10, 'admin');

      expect(config.refreshIntervalMinutes).toBe(10);
    });

    it('刷新间隔为0时应该抛出错误', () => {
      const config = LeaderboardConfig.createDefault();

      expect(() => {
        config.updateRefreshInterval(0, 'admin');
      }).toThrow('刷新间隔必须大于0');
    });
  });

  describe('isLeaderboardEnabled', () => {
    it('应该正确判断日榜是否启用', () => {
      const config = LeaderboardConfig.createDefault();

      expect(config.isLeaderboardEnabled(LeaderboardType.DAILY)).toBe(true);

      config.updateLeaderboardSwitch('daily', false, 'admin');
      expect(config.isLeaderboardEnabled(LeaderboardType.DAILY)).toBe(false);
    });

    it('应该正确判断周榜是否启用', () => {
      const config = LeaderboardConfig.createDefault();

      expect(config.isLeaderboardEnabled(LeaderboardType.WEEKLY)).toBe(true);
    });

    it('应该正确判断月榜是否启用', () => {
      const config = LeaderboardConfig.createDefault();

      expect(config.isLeaderboardEnabled(LeaderboardType.MONTHLY)).toBe(true);
    });
  });

  describe('getVirtualRankingSlots', () => {
    it('虚拟排名未启用时应该返回0', () => {
      const config = LeaderboardConfig.createDefault();
      expect(config.getVirtualRankingSlots()).toBe(0);
    });

    it('虚拟排名启用时应该返回账户数量', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateVirtualRankingSettings(true, 30, 'admin');

      expect(config.getVirtualRankingSlots()).toBe(30);
    });
  });

  describe('clearDomainEvents', () => {
    it('应该清空领域事件', () => {
      const config = LeaderboardConfig.createDefault();
      config.updateLeaderboardSwitch('daily', false, 'admin');

      expect(config.domainEvents.length).toBe(1);

      config.clearDomainEvents();
      expect(config.domainEvents.length).toBe(0);
    });
  });
});
