package com.red.circle.other.app.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.math.BigDecimal;

/**
 * BD薪资政策工具类.
 *
 * @author system
 */
public class BdSalaryPolicyUtils {

    /**
     * BD薪资等级枚举.
     */
    @Getter
    @AllArgsConstructor
    public enum BdSalaryLevel {
        LEVEL_1(1, 3, 50, 1),
        LEVEL_2(2, 5, 500, 2),
        LEVEL_3(3, 7, 1000, 3),
        LEVEL_4(4, 9, 3000, 4),
        LEVEL_5(5, 10, 5000, 5);

        private final int level;
        private final int requiredAgentNumber;
        private final int requiredTeamSalary;
        private final int foreverPercentage;
    }

    /**
     * 薪资政策结果.
     */
    @Data
    public static class BdSalaryResult {
        /**
         * 等级.
         */
        private Integer level;
        
        /**
         * 永久百分比.
         */
        private Integer foreverPercentage;
        
        /**
         * 是否达标.
         */
        private Boolean qualified;
        
        /**
         * 需要的代理数量.
         */
        private Integer requiredAgentNumber;
        
        /**
         * 需要的团队薪资.
         */
        private Integer requiredTeamSalary;

        public BdSalaryResult(Integer level, Integer foreverPercentage, Boolean qualified, 
                             Integer requiredAgentNumber, Integer requiredTeamSalary) {
            this.level = level;
            this.foreverPercentage = foreverPercentage;
            this.qualified = qualified;
            this.requiredAgentNumber = requiredAgentNumber;
            this.requiredTeamSalary = requiredTeamSalary;
        }
    }

    /**
     * 根据代理数量和团队薪资计算BD等级和百分比.
     *
     * @param agentQuantity 代理数量
     * @param teamSalary 团队薪资
     * @return BD薪资结果
     */
    public static BdSalaryResult calculateBdSalaryLevel(Long agentQuantity, BigDecimal teamSalary) {
        if (agentQuantity == null || teamSalary == null) {
            return new BdSalaryResult(null, null, false, null, null);
        }

        int agentCount = agentQuantity.intValue();
        int salary = teamSalary.intValue();

        // 从高等级往低等级匹配
        for (int i = BdSalaryLevel.values().length - 1; i >= 0; i--) {
            BdSalaryLevel level = BdSalaryLevel.values()[i];
            
            // 代理数量和团队薪资都要达标
            if (agentCount >= level.getRequiredAgentNumber() && salary >= level.getRequiredTeamSalary()) {
                return new BdSalaryResult(
                    level.getLevel(),
                    level.getForeverPercentage(),
                    true,
                    level.getRequiredAgentNumber(),
                    level.getRequiredTeamSalary()
                );
            }
        }

        // 如果都不达标，返回最低等级要求
        BdSalaryLevel lowestLevel = BdSalaryLevel.LEVEL_1;
        return new BdSalaryResult(
            0,
            0,
            false,
            lowestLevel.getRequiredAgentNumber(),
            lowestLevel.getRequiredTeamSalary()
        );
    }

    /**
     * 获取指定等级的薪资政策信息.
     *
     * @param level 等级
     * @return 薪资政策信息
     */
    public static BdSalaryResult getBdSalaryLevelInfo(int level) {
        for (BdSalaryLevel salaryLevel : BdSalaryLevel.values()) {
            if (salaryLevel.getLevel() == level) {
                return new BdSalaryResult(
                    salaryLevel.getLevel(),
                    salaryLevel.getForeverPercentage(),
                    null,
                    salaryLevel.getRequiredAgentNumber(),
                    salaryLevel.getRequiredTeamSalary()
                );
            }
        }
        return null;
    }

    /**
     * 获取下一个等级需要达到的条件.
     *
     * @param currentAgentQuantity 当前代理数量
     * @param currentTeamSalary 当前团队薪资
     * @return 下一个等级的要求，如果已经是最高等级则返回null
     */
    public static BdSalaryResult getNextLevelRequirement(Long currentAgentQuantity, BigDecimal currentTeamSalary) {
        if (currentAgentQuantity == null || currentTeamSalary == null) {
            return getBdSalaryLevelInfo(1);
        }

        BdSalaryResult currentResult = calculateBdSalaryLevel(currentAgentQuantity, currentTeamSalary);
        
        // 如果当前没有达到任何等级，返回最低等级要求
        if (!currentResult.getQualified()) {
            return getBdSalaryLevelInfo(1);
        }

        // 查找下一个等级
        for (BdSalaryLevel level : BdSalaryLevel.values()) {
            if (level.getLevel() > currentResult.getLevel()) {
                return getBdSalaryLevelInfo(level.getLevel());
            }
        }

        // 已经是最高等级
        return null;
    }
}
