import { RoleType } from '@/domain/enums'
import { DomainError } from '@/shared/exceptions'

export class LadderTargetRule {
  constructor(
    public readonly roleType: RoleType,
    public readonly monthIndex: number,
    public readonly monthlyTarget: number,
    public readonly cumulativeTarget: number,
  ) {}

  // 省代阶梯目标表
  static readonly PROVINCE_LADDER: LadderTargetRule[] = [
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 1, 150, 150),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 2, 300, 450),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 3, 600, 1050),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 4, 1200, 2250),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 5, 2400, 4650),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 6, 4800, 9450),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 7, 9600, 19050),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 8, 19200, 38250),
    new LadderTargetRule(RoleType.AUTH_PROVINCE_COMPANY, 9, 11750, 50000),
  ]

  // 市团队授权阶梯目标表 (AUTH_CITY_COMPANY)
  static readonly CITY_LADDER: LadderTargetRule[] = [
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 1, 30, 30),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 2, 60, 90),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 3, 120, 210),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 4, 240, 450),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 5, 480, 930),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 6, 960, 1890),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 7, 1920, 3810),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 8, 3840, 7650),
    new LadderTargetRule(RoleType.AUTH_CITY_COMPANY, 9, 2350, 10000),
  ]

  // 正式市公司/市区域权益阶梯目标表 (CITY_COMPANY)
  // 与市团队授权使用相同的阶梯目标
  static readonly CITY_COMPANY_LADDER: LadderTargetRule[] = [
    new LadderTargetRule(RoleType.CITY_COMPANY, 1, 30, 30),
    new LadderTargetRule(RoleType.CITY_COMPANY, 2, 60, 90),
    new LadderTargetRule(RoleType.CITY_COMPANY, 3, 120, 210),
    new LadderTargetRule(RoleType.CITY_COMPANY, 4, 240, 450),
    new LadderTargetRule(RoleType.CITY_COMPANY, 5, 480, 930),
    new LadderTargetRule(RoleType.CITY_COMPANY, 6, 960, 1890),
    new LadderTargetRule(RoleType.CITY_COMPANY, 7, 1920, 3810),
    new LadderTargetRule(RoleType.CITY_COMPANY, 8, 3840, 7650),
    new LadderTargetRule(RoleType.CITY_COMPANY, 9, 2350, 10000),
  ]

  // 正式省公司/省区域权益阶梯目标表 (PROVINCE_COMPANY)
  // 与省团队授权使用相同的阶梯目标
  static readonly PROVINCE_COMPANY_LADDER: LadderTargetRule[] = [
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 1, 150, 150),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 2, 300, 450),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 3, 600, 1050),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 4, 1200, 2250),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 5, 2400, 4650),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 6, 4800, 9450),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 7, 9600, 19050),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 8, 19200, 38250),
    new LadderTargetRule(RoleType.PROVINCE_COMPANY, 9, 11750, 50000),
  ]

  // 社区固定目标
  static readonly COMMUNITY_FIXED: LadderTargetRule = new LadderTargetRule(
    RoleType.COMMUNITY,
    1,
    10,
    10,
  )

  /**
   * 获取指定角色和月份的目标
   */
  static getTarget(roleType: RoleType, monthIndex: number): LadderTargetRule {
    switch (roleType) {
      case RoleType.AUTH_PROVINCE_COMPANY:
        // 超过9个月后使用第9个月的目标
        const provinceIndex = Math.min(monthIndex, 9) - 1
        return this.PROVINCE_LADDER[provinceIndex >= 0 ? provinceIndex : 0]

      case RoleType.AUTH_CITY_COMPANY:
        const cityIndex = Math.min(monthIndex, 9) - 1
        return this.CITY_LADDER[cityIndex >= 0 ? cityIndex : 0]

      case RoleType.CITY_COMPANY:
        const cityCompanyIndex = Math.min(monthIndex, 9) - 1
        return this.CITY_COMPANY_LADDER[cityCompanyIndex >= 0 ? cityCompanyIndex : 0]

      case RoleType.PROVINCE_COMPANY:
        const provinceCompanyIndex = Math.min(monthIndex, 9) - 1
        return this.PROVINCE_COMPANY_LADDER[provinceCompanyIndex >= 0 ? provinceCompanyIndex : 0]

      case RoleType.COMMUNITY:
        return this.COMMUNITY_FIXED

      default:
        throw new DomainError(`不支持的角色类型: ${roleType}`)
    }
  }

  /**
   * 获取最终累计目标
   */
  static getFinalTarget(roleType: RoleType): number {
    switch (roleType) {
      case RoleType.AUTH_PROVINCE_COMPANY:
        return 50000
      case RoleType.AUTH_CITY_COMPANY:
        return 10000
      case RoleType.CITY_COMPANY:
        return 10000
      case RoleType.PROVINCE_COMPANY:
        return 50000
      case RoleType.COMMUNITY:
        return 10
      default:
        return 0
    }
  }

  /**
   * 获取所有阶梯目标
   */
  static getAllTargets(roleType: RoleType): LadderTargetRule[] {
    switch (roleType) {
      case RoleType.AUTH_PROVINCE_COMPANY:
        return this.PROVINCE_LADDER
      case RoleType.AUTH_CITY_COMPANY:
        return this.CITY_LADDER
      case RoleType.CITY_COMPANY:
        return this.CITY_COMPANY_LADDER
      case RoleType.PROVINCE_COMPANY:
        return this.PROVINCE_COMPANY_LADDER
      case RoleType.COMMUNITY:
        return [this.COMMUNITY_FIXED]
      default:
        return []
    }
  }
}
