"""
经济价值B1计算模块

经济价值B1 = 基础价值B11 × (1 + 流量因子B12) × 政策乘数B13

"""

from typing import Dict

try:
    # 相对导入（当作为包使用时）
    from .sub_formulas.basic_value_b11 import BasicValueB11Calculator, calculate_popularity_score
    from .sub_formulas.traffic_factor_b12 import TrafficFactorB12Calculator
    from .sub_formulas.policy_multiplier_b13 import PolicyMultiplierB13Calculator
except ImportError:
    # 绝对导入（当直接运行时）
    from sub_formulas.basic_value_b11 import BasicValueB11Calculator
    from sub_formulas.traffic_factor_b12 import TrafficFactorB12Calculator
    from sub_formulas.policy_multiplier_b13 import PolicyMultiplierB13Calculator


class EconomicValueB1Calculator:
    """经济价值B1计算器"""

    def __init__(self):
        """初始化计算器"""
        self.basic_value_calculator = BasicValueB11Calculator()
        self.traffic_factor_calculator = TrafficFactorB12Calculator()
        self.policy_multiplier_calculator = PolicyMultiplierB13Calculator()

    def calculate_economic_value_b1(self,
                                    basic_value_b11: float,
                                    traffic_factor_b12: float,
                                    policy_multiplier_b13: float) -> float:
        """
        计算经济价值B1
        
        
        经济价值B1 = 基础价值B11 × (1 + 流量因子B12) × 政策乘数B13
        
        args:
            basic_value_b11: 基础价值B11 (系统计算)
            traffic_factor_b12: 流量因子B12 (系统计算)
            policy_multiplier_b13: 政策乘数B13 (系统计算)
            
        returns:
            float: 经济价值B1
        """
        economic_value = basic_value_b11 * (1 + traffic_factor_b12) * policy_multiplier_b13

        return economic_value

    def calculate_complete_economic_value_b1(self, input_data: Dict) -> Dict:
        """
        计算完整的经济价值B1，包含所有子公式
        
        args:
            input_data: 输入数据字典，包含所有必要的参数
            
        returns:
            Dict: 包含所有中间计算结果和最终结果的字典
        """
        # 财务价值F 近三年年均收益列表 [1,2,3]
        financial_value = self.basic_value_calculator.calculate_financial_value_f(input_data["three_year_income"])
        # 计算法律强度L   patent_score: 专利分 (0-10分) (用户填写)
        #              popularity_score: 普及地域分 (0-10分) (用户填写)
        #              infringement_score: 侵权分 (0-10分) (用户填写)

        legal_strength = self.basic_value_calculator.calculate_legal_strength_l(
            input_data["patent_score"],
            input_data["popularity_score"],
            input_data["infringement_score"],
        )

        # 发展潜力      patent_count: 专利分 (0-10分) (用户填写)
        #             esg_score: ESG分 (0-10分) (用户填写)
        #             innovation_ratio: 创新投入比 (研发费用/营收) * 100 (用户填写)
        development_potential = self.basic_value_calculator.calculate_development_potential_d(

            input_data["patent_count"],
            input_data["esg_score"],
            input_data["innovation_ratio"],
        )
        # 计算行业系数I  target_industry_roe: 目标行业平均ROE (系统配置)
        #             benchmark_industry_roe: 基准行业ROE (系统配置)
        # industry_coefficient = self.basic_value_calculator.calculate_industry_coefficient_i(
        #
        # )
        # 计算基础价值B11
        basic_value_b11 = self.basic_value_calculator.calculate_basic_value_b11(
            financial_value,  # 财务价值F
            legal_strength,  # 法律强度L
            development_potential,
            input_data["industry_coefficient"]
        )

        # 计算互动量指数
        interaction_index = self.traffic_factor_calculator.calculate_interaction_index(
            input_data["likes"],
            input_data["comments"],
            input_data["shares"],
        )
        # 计算覆盖人群指数
        coverage_index = self.traffic_factor_calculator.calculate_coverage_index(0)
        # 计算转化率
        conversion_efficiency = self.traffic_factor_calculator.calculate_conversion_efficiency(
            input_data["sales_volume"], input_data["link_views"])

        social_media_spread_s3 = self.traffic_factor_calculator.calculate_social_media_spread_s3(interaction_index,
                                                                                                 coverage_index,
                                                                                                 conversion_efficiency)

        traffic_factor_b12 = self.traffic_factor_calculator.calculate_traffic_factor_b12(
            input_data['search_index_s1'],
            input_data['industry_average_s2'],
            social_media_spread_s3
        )

        # 计算政策乘数B13
        policy_compatibility_score = self.policy_multiplier_calculator.calculate_policy_compatibility_score(
            input_data["policy_match_score"],
            input_data["implementation_stage"],
            input_data["funding_support"])
        policy_multiplier_b13 = self.policy_multiplier_calculator.calculate_policy_multiplier_b13(
            policy_compatibility_score
        )

        # 计算经济价值B1
        economic_value_b1 = self.calculate_economic_value_b1(
            basic_value_b11,
            traffic_factor_b12,
            policy_multiplier_b13
        )

        return {
            'basic_value_b11': basic_value_b11,
            'traffic_factor_b12': traffic_factor_b12,
            'policy_multiplier_b13': policy_multiplier_b13,
            'economic_value_b1': economic_value_b1
        }


# 示例使用
if __name__ == "__main__":
    calculator = EconomicValueB1Calculator()

    # 示例数据
    '''
    {
        # 基础价值B11相关参数
        'three_year_income': three_year_income,
        'patent_score': patent_score,
        'popularity_score': popularity_score,
        'infringement_score': infringement_score,
        'innovation_ratio': innovation_ratio,

        # 流量因子B12相关参数
        'search_index_s1': search_index_s1,
        'industry_average_s2': industry_average_s2,
        # 'social_media_spread_s3': social_media_spread_s3,
        'likes':likes,
        'comments':comments,
        'shares':shares,
        'followers':followers,
        
        'click_count': int(data.click_count) or 100,
        'view_count': int(data.link_views) or 100,

        # 政策乘数B13相关参数
        'implementation_stage': implementation_stage,
        'funding_support':funding_support
    }
    
    '''
    input_data = {
        # 基础价值B11相关参数
        'three_year_income': [2000, 2400, 2600],
        'patent_score': 0,  # 专利分
        'patent_count': 0,  # 专利分
        'popularity_score': 10.0,  # 普及地域分值
        'infringement_score': 0,  # 侵权分
        'innovation_ratio': 1200 / 3000,
        'esg_score': 7.0,
        'industry_coefficient': -0.0625,  # 行业系数

        # 流量因子B12相关参数
        'search_index_s1': 0,
        'industry_average_s2': 15000,  # 行业均值
        # 'social_media_spread_s3': social_media_spread_s3,
        'likes': 3933333.33,  # 点赞
        'comments': 3933333.33,  # 评论
        'shares': 3933333.33,  # 转发
        'followers': 15000,  # 粉丝数

        "sales_volume": 5000,
        "link_views": 20000,

        # 政策乘数B13相关参数
        'policy_match_score': 7.0,  # 政策匹配度
        'implementation_stage': 10.0,  # 实施阶段评分
        'funding_support': 10.0  # 资金支持度
    }
    # input_data = {
    #     # 基础价值B11相关参数
    #     'financial_value': 68.04,  # 财务价值F
    #     'legal_strength': 7.90,    # 法律强度L
    #     'development_potential': 6.00,  # 发展潜力D
    #     'industry_coefficient': 0.25,   # 行业系数I
    #
    #     # 流量因子B12相关参数
    #     'search_index_s1': 4500.0,  # 近30天搜索指数S1
    #     'industry_average_s2': 5000.0,  # 行业均值S2
    #     'social_media_spread_s3': 1.07,  # 社交媒体传播度S3
    #
    #     # 政策乘数B13相关参数
    #     'policy_compatibility_score': 9.1  # 政策契合度评分P
    # }

    # 计算经济价值B1
    result = calculator.calculate_complete_economic_value_b1(input_data)

    print("经济价值B1计算结果:")
    print(f"基础价值B11: {result['basic_value_b11']:.2f}")
    print(f"流量因子B12: {result['traffic_factor_b12']:.4f}")
    print(f"政策乘数B13: {result['policy_multiplier_b13']:.4f}")
    print(f"经济价值B1: {result['economic_value_b1']:.2f}")
