"""配置管理模块"""
from typing import Dict, List, Optional, Any
import yaml
from pathlib import Path


# 默认场景配置
DEFAULT_SCENARIOS = {
    "birthday_mom_practical": {
        "relation": "妈妈",
        "occasion": "生日",
        "style": "实用型",
        "budget_range": [200, 1000],
        "gifts": [
            {"name": "扫地机器人", "price": 899, "reason": "解放双手"},
            {"name": "足浴盆", "price": 299, "reason": "健康养生"},
            {"name": "按摩椅", "price": 1599, "reason": "缓解疲劳"},
        ]
    },
    "birthday_dad_tech": {
        "relation": "爸爸",
        "occasion": "生日",
        "style": "科技型",
        "budget_range": [500, 2000],
        "gifts": [
            {"name": "智能手表", "price": 1299, "reason": "健康监测"},
            {"name": "电动牙刷", "price": 399, "reason": "口腔健康"},
            {"name": "蓝牙音箱", "price": 699, "reason": "享受音乐"},
        ]
    },
    "anniversary_couple_romantic": {
        "relation": "恋人",
        "occasion": "纪念日",
        "style": "浪漫型",
        "budget_range": [300, 2000],
        "gifts": [
            {"name": "情侣对戒", "price": 999, "reason": "永恒的承诺"},
            {"name": "香薰蜡烛套装", "price": 299, "reason": "浪漫氛围"},
            {"name": "定制相册", "price": 199, "reason": "记录美好时光"},
        ]
    }
}


class Config:
    """配置管理类"""
    
    def __init__(self, config_path: Optional[str] = None):
        """
        初始化配置
        
        Args:
            config_path: YAML配置文件路径（可选）
        """
        self.scenarios: Dict[str, Dict[str, Any]] = {}
        
        # 加载默认配置
        self.scenarios.update(DEFAULT_SCENARIOS)
        
        # 如果提供了配置文件，尝试加载
        if config_path:
            self._load_from_file(config_path)
    
    def _load_from_file(self, config_path: str) -> None:
        """
        从YAML文件加载配置
        
        Args:
            config_path: 配置文件路径
        """
        path = Path(config_path)
        if path.exists():
            with open(path, 'r', encoding='utf-8') as f:
                data = yaml.safe_load(f)
                if data and 'scenarios' in data:
                    self.scenarios.update(data['scenarios'])
    
    def get_scenarios(self) -> List[Dict[str, Any]]:
        """
        获取所有场景列表
        
        Returns:
            场景列表，每个场景包含id和详细信息
        """
        return [
            {"id": scenario_id, **scenario_info}
            for scenario_id, scenario_info in self.scenarios.items()
        ]
    
    def get_scenario(self, scenario_id: str) -> Optional[Dict[str, Any]]:
        """
        根据ID获取场景配置
        
        Args:
            scenario_id: 场景ID
            
        Returns:
            场景配置字典，如果不存在返回None
        """
        return self.scenarios.get(scenario_id)
    
    def get_gifts(self, scenario_id: str) -> List[Dict[str, Any]]:
        """
        获取场景对应的礼物列表
        
        Args:
            scenario_id: 场景ID
            
        Returns:
            礼物列表，如果场景不存在返回空列表
        """
        scenario = self.get_scenario(scenario_id)
        if scenario and 'gifts' in scenario:
            return scenario['gifts']
        return []
