"""
模型估值B计算模块

模型估值B = （经济价值B1*0.7+文化价值B2*0.3）*风险调整系数B3
"""

from typing import Dict

from app.utils.calculation_engine import RiskAdjustmentB3Calculator

try:
    # 相对导入（当作为包使用时）
    from ..economic_value_b1.economic_value_b1 import EconomicValueB1Calculator
    from ..cultural_value_b2.cultural_value_b2 import CulturalValueB2Calculator
except ImportError:
    # 绝对导入（当直接运行时）
    from app.utils.calculation_engine.economic_value_b1.economic_value_b1 import EconomicValueB1Calculator
    from app.utils.calculation_engine.cultural_value_b2.cultural_value_b2 import CulturalValueB2Calculator


class ModelValueBCalculator:
    """模型估值B计算器"""

    def __init__(self):
        """初始化计算器"""
        self.economic_value_calculator = EconomicValueB1Calculator()
        self.cultural_value_calculator = CulturalValueB2Calculator()
        self.risk_adjustment_calculator = RiskAdjustmentB3Calculator()

    def calculate_model_value_b(self,
                                economic_value_b1: float,
                                cultural_value_b2: float,
                                risk_value_b3: float) -> float:
        """
        模型估值B = （经济价值B1*0.7+文化价值B2*0.3）*风险调整系数B3
        
        Args:
            economic_value_b1: 经济价值B1
            cultural_value_b2: 文化价值B2
            risk_value_b3 : 风险调整系数B3
            
        Returns:
            float: 模型估值B
        """
        model_value = (economic_value_b1 * 0.7 + cultural_value_b2 * 0.3) * risk_value_b3

        return model_value

    def calculate_complete_model_value_b(self, input_data: Dict) -> Dict:
        """
        计算完整的模型估值B，包含所有子公式
        
        Args:
            input_data: 输入数据字典，包含所有必要的参数
            
        Returns:
            Dict: 包含所有中间计算结果和最终结果的字典
        """
        # 计算经济价值B1
        economic_result = self.economic_value_calculator.calculate_complete_economic_value_b1(
            input_data['economic_data']
        )
        economic_value_b1 = economic_result['economic_value_b1']

        # 计算文化价值B2
        cultural_result = self.cultural_value_calculator.calculate_complete_cultural_value_b2(
            input_data['cultural_data']
        )
        cultural_value_b2 = cultural_result['cultural_value_b2']

        risk_value_result = self.risk_adjustment_calculator.calculate_complete_risky_value_b3(
            input_data['risky_data']
        )
        risk_value_b3 = risk_value_result['risk_adjustment_b3']
        # 计算模型估值B
        model_value_b = self.calculate_model_value_b(
            economic_value_b1,
            cultural_value_b2,
            risk_value_b3
        )

        return {
            'economic_value_b1': economic_value_b1,
            'cultural_value_b2': cultural_value_b2,
            'risk_value_b3': risk_value_b3,
            'model_value_b': model_value_b,
        }


# 示例使用
if __name__ == "__main__":
    # 创建计算器实例
    calculator = ModelValueBCalculator()

    # 示例数据
    input_data = {
        # 经济价值B1相关数据
        'economic_data': {
            'financial_value': 68.04,
            'legal_strength': 7.90,
            'development_potential': 6.00,
            'industry_coefficient': 0.25,
            'search_index_s1': 4500.0,
            'industry_average_s2': 5000.0,
            'social_media_spread_s3': 1.07,
            'policy_compatibility_score': 9.1,
            'sales_volume': 99,  # 销售量
            'link_views': 88  # 浏览量
        },

        # 文化价值B2相关数据
        'cultural_data': {
            'inheritor_level_coefficient': 10.0,
            'teaching_frequency': 7.0,
            'cross_border_depth': 7.0,
            'structure_complexity': 0.75,
            'normalized_entropy': 0.85,
            'historical_inheritance': 0.80
        }
    }

    # 计算模型估值B
    result = calculator.calculate_complete_model_value_b(input_data)

    print("模型估值B计算结果:")
    print(f"经济价值B1: {result['economic_value_b1']:.2f}")
    print(f"文化价值B2: {result['cultural_value_b2']:.4f}")
    print(f"模型估值B: {result['model_value_b']:.2f}")
