import { LadderTargetRule } from './ladder-target-rule.entity'
import { RoleType } from '@/domain/enums'
import { DomainError } from '@/shared/exceptions'

describe('LadderTargetRule', () => {
  describe('Province Ladder', () => {
    it('should have correct targets for province company', () => {
      const rule1 = LadderTargetRule.getTarget(RoleType.AUTH_PROVINCE_COMPANY, 1)
      expect(rule1.monthlyTarget).toBe(150)
      expect(rule1.cumulativeTarget).toBe(150)

      const rule2 = LadderTargetRule.getTarget(RoleType.AUTH_PROVINCE_COMPANY, 2)
      expect(rule2.monthlyTarget).toBe(300)
      expect(rule2.cumulativeTarget).toBe(450)

      const rule9 = LadderTargetRule.getTarget(RoleType.AUTH_PROVINCE_COMPANY, 9)
      expect(rule9.monthlyTarget).toBe(11750)
      expect(rule9.cumulativeTarget).toBe(50000)
    })

    it('should use 9th month target for months > 9', () => {
      const rule10 = LadderTargetRule.getTarget(RoleType.AUTH_PROVINCE_COMPANY, 10)
      expect(rule10.cumulativeTarget).toBe(50000)

      const rule12 = LadderTargetRule.getTarget(RoleType.AUTH_PROVINCE_COMPANY, 12)
      expect(rule12.cumulativeTarget).toBe(50000)
    })

    it('should have correct final target for province', () => {
      expect(LadderTargetRule.getFinalTarget(RoleType.AUTH_PROVINCE_COMPANY)).toBe(50000)
    })
  })

  describe('City Ladder', () => {
    it('should have correct targets for city company', () => {
      const rule1 = LadderTargetRule.getTarget(RoleType.AUTH_CITY_COMPANY, 1)
      expect(rule1.monthlyTarget).toBe(30)
      expect(rule1.cumulativeTarget).toBe(30)

      const rule2 = LadderTargetRule.getTarget(RoleType.AUTH_CITY_COMPANY, 2)
      expect(rule2.monthlyTarget).toBe(60)
      expect(rule2.cumulativeTarget).toBe(90)

      const rule9 = LadderTargetRule.getTarget(RoleType.AUTH_CITY_COMPANY, 9)
      expect(rule9.monthlyTarget).toBe(2350)
      expect(rule9.cumulativeTarget).toBe(10000)
    })

    it('should have correct final target for city', () => {
      expect(LadderTargetRule.getFinalTarget(RoleType.AUTH_CITY_COMPANY)).toBe(10000)
    })
  })

  describe('Community Fixed', () => {
    it('should have fixed target for community', () => {
      const rule = LadderTargetRule.getTarget(RoleType.COMMUNITY, 1)
      expect(rule.monthlyTarget).toBe(10)
      expect(rule.cumulativeTarget).toBe(10)
    })

    it('should have correct final target for community', () => {
      expect(LadderTargetRule.getFinalTarget(RoleType.COMMUNITY)).toBe(10)
    })
  })

  describe('getAllTargets', () => {
    it('should return all province targets', () => {
      const targets = LadderTargetRule.getAllTargets(RoleType.AUTH_PROVINCE_COMPANY)
      expect(targets.length).toBe(9)
    })

    it('should return all city targets', () => {
      const targets = LadderTargetRule.getAllTargets(RoleType.AUTH_CITY_COMPANY)
      expect(targets.length).toBe(9)
    })

    it('should return single community target', () => {
      const targets = LadderTargetRule.getAllTargets(RoleType.COMMUNITY)
      expect(targets.length).toBe(1)
    })
  })

  describe('Error handling', () => {
    it('should throw error for unsupported role type', () => {
      expect(() => LadderTargetRule.getTarget(RoleType.PROVINCE_COMPANY, 1)).toThrow(
        DomainError,
      )
    })
  })
})
