"""
API入口 - 优化集成版（最终版本）
整合了3个核心优化模块：
1. GiftFilterV2 - 增强禁忌过滤
2. BlessingGeneratorV2 - 个性化祝福语
3. DialogManager - 对话式决策支持
"""
from typing import List, Dict, Optional
from .recommender import Recommender
from .parser import InputParser
from .reason import ReasonGenerator
from .blessing_v2 import BlessingGeneratorV2
from .filter_v2 import GiftFilterV2
from .dialog_manager import DialogManager
from .output_formatter import OutputFormatter
from .preference import get_preference

class GiftAdvisor:
    """礼物推荐助手 - 最终版"""

    def __init__(self, user_id: str = "default", enhanced: bool = True):
        # 核心模块
        self.recommender = Recommender()
        self.parser = InputParser()
        self.reason_gen = ReasonGenerator()
        self.formatter = OutputFormatter()
        self.preference = get_preference()
        
        # 新模块
        self.enhanced = enhanced
        if enhanced:
            self.blessing_gen = BlessingGeneratorV2()
            self.filter_v2 = GiftFilterV2()
            self.dialog = DialogManager()
        else:
            from .blessing import BlessingGenerator
            from .filter import GiftFilter
            self.blessing_gen = BlessingGenerator()
            self.filter_v2 = GiftFilter()
            self.dialog = None
        
        # 用户状态
        self.user_id = user_id
        self.conversation_state = "initial" if enhanced else "direct"

    def recommend(
        self,
        relation: Optional[str] = None,
        occasion: Optional[str] = None,
        budget_min: Optional[int] = None,
        budget_max: Optional[int] = None,
        limit: int = 5
    ) -> Dict:
        """推荐礼物"""
        # 获取推荐
        gifts = self.recommender.recommend(
            relation=relation,
            occasion=occasion,
            budget_min=budget_min,
            budget_max=budget_max,
            limit=limit * 2
        )
        
        # 禁忌过滤
        filtered_gifts = gifts
        taboo_report = {"summary": "✅ 无禁忌礼物"}
        
        if self.enhanced:
            filtered_gifts, taboo_report = self.filter_v2.filter(
                gifts=gifts,
                relation=relation
            )
        
        if len(filtered_gifts) < limit:
            filtered_gifts = gifts[:limit]
        
        filtered_gifts = filtered_gifts[:limit]
        
        # 生成结果
        recommendations = []
        for gift in filtered_gifts:
            reason = self.reason_gen.generate(gift, relation, occasion)
            score = self._calculate_score(gift, budget_max)
            
            recommendations.append({
                "name": gift.get("name"),
                "price": gift.get("price"),
                "reason": reason,
                "score": score,
            })
        
        recommendations.sort(key=lambda x: x["score"], reverse=True)
        
        # 祝福语
        if self.enhanced and recommendations:
            top_gift = recommendations[0]["name"]
            blessing = self.blessing_gen.generate(
                occasion=occasion,
                relation=relation,
                gift_name=top_gift,
                style="标准版"
            )
            blessing_options = self.blessing_gen.generate_multiple_options(
                occasion=occasion,
                relation=relation,
                gift_name=top_gift,
                count=3
            )
        else:
            blessing = "祝你开心每一天！"
            blessing_options = []
        
        return {
            "success": True,
            "query": {
                "relation": relation,
                "occasion": occasion,
                "budget": f"{budget_min or '?'}-{budget_max or '?'}元",
            },
            "recommendations": recommendations,
            "blessing": blessing,
            "blessing_options": blessing_options,
            "taboo_report": taboo_report,
            "count": len(recommendations),
        }

    def parse_and_recommend(self, text: str, limit: int = 5) -> Dict:
        """解析自然语言并推荐"""
        # 检测"换一个"
        change_keywords = ["换一个", "再来一个", "还有吗"]
        if any(kw in text for kw in change_keywords):
            last_query = self.preference.get_last_query(self.user_id)
            if last_query:
                return self._handle_change_request()
        
        # 对话模式
        if self.enhanced and len(text) < 30 and not "?" in text:
            return self.process_message(text)
        
        # 解析
        parsed = self.parser.parse(text)
        
        if parsed.get("needs_clarification"):
            return {
                "success": False,
                "needs_clarification": True,
                "missing_fields": parsed.get("missing_fields", []),
                "clarification_questions": self.parser.get_clarification_questions(
                    parsed.get("missing_fields", [])
                ),
                "parsed": parsed,
            }
        
        # 保存查询
        self.preference.set_last_query(parsed, self.user_id)
        self.preference.clear_shown_gifts(self.user_id)
        
        # 推荐
        result = self.recommend(
            relation=parsed.get("relation"),
            occasion=parsed.get("occasion"),
            budget_min=parsed.get("budget_min"),
            budget_max=parsed.get("budget_max"),
            limit=limit
        )
        
        # 记录
        for gift in result.get("recommendations", []):
            self.preference.add_shown_gift(gift["name"], self.user_id)
        
        return result

    def process_message(self, user_input: str) -> Dict:
        """对话处理"""
        if not self.enhanced or not self.dialog:
            return {"success": False, "message": "对话功能未启用"}
        
        response, dialog_state = self.dialog.process_message(user_input)
        
        # 更新状态
        self.conversation_state = dialog_state["state"]
        
        # 生成响应
        if dialog_state["state"] == "ready_to_recommend":
            # 生成推荐
            return self._generate_recommendation(dialog_state["preferences"])
        else:
            # 继续对话
            return {
                "success": True,
                "message": response,
                "type": "question"
            }

    def _handle_change_request(self) -> Dict:
        """处理换一个请求"""
        last_query = self.preference.get_last_query(self.user_id)
        if not last_query:
            return {
                "success": False,
                "message": "没有之前的查询记录"
            }
        
        # 已展示的礼物
        shown_gifts = self.preference.get_shown_gifts(self.user_id)
        
        # 获取更多推荐
        all_gifts = self.recommender.recommend(
            relation=last_query.get("relation"),
            occasion=last_query.get("occasion"),
            budget_min=last_query.get("budget_min"),
            budget_max=last_query.get("budget_max"),
            limit=20
        )
        
        # 过滤
        new_gifts = [g for g in all_gifts if g["name"] not in shown_gifts]
        if not new_gifts:
            self.preference.clear_shown_gifts(self.user_id)
            new_gifts = all_gifts
        
        # 禁忌过滤
        if self.enhanced:
            new_gifts, _ = self.filter_v2.filter(
                gifts=new_gifts,
                relation=last_query.get("relation")
            )
        
        # 生成推荐
        recommendations = []
        for gift in new_gifts[:5]:
            reason = self.reason_gen.generate(gift, last_query.get("relation"))
            score = self._calculate_score(gift, last_query.get("budget_max"))
            recommendations.append({
                "name": gift.get("name"),
                "price": gift.get("price"),
                "reason": reason,
                "score": score,
            })
            self.preference.add_shown_gift(gift["name"], self.user_id)
        
        # 祝福语
        if self.enhanced:
            blessing = self.blessing_gen.generate(
                occasion=last_query.get("occasion"),
                relation=last_query.get("relation")
            )
        else:
            blessing = "祝你开心！"
        
        return {
            "success": True,
            "query": {
                "relation": last_query.get("relation"),
                "occasion": last_query.get("occasion"),
                "budget": f"{last_query.get('budget_min') or 0}-{last_query.get('budget_max') or '不限'}",
            },
            "recommendations": recommendations,
            "blessing": blessing,
            "count": len(recommendations),
        }

    def _generate_recommendation(self, preferences: Dict) -> Dict:
        """生成推荐"""
        relation = preferences.get("relation")
        occasion = preferences.get("occasion")
        budget_min = preferences.get("budget_min")
        budget_max = preferences.get("budget_max")
        
        return self.recommend(
            relation=relation,
            occasion=occasion,
            budget_min=budget_min,
            budget_max=budmax
        )

    def _calculate_score(self, gift: Dict, budget_max: Optional[int] = None) -> int:
        """计算分数"""
        score = 80
        
        price = gift.get("price", 0)
        if budget_max:
            ratio = price / budget_max
            if 0.3 <= ratio <= 0.7:
                score += 10
        
        if gift.get("reason"):
            score += 5
        
        return min(score, 100)

    def explain_taboo(self, gift_name: str, relation: str = None) -> Dict:
        """解释禁忌"""
        if not self.enhanced:
            return {"success": False, "message": "增强功能未启用"}
        
        explanation = self.filter_v2.explain_taboo(gift_name, relation)
        alternatives = self.filter_v2.get_safe_alternatives(gift_name)
        
        return {
            "success": True,
            "gift_name": gift_name,
            "explanation": explanation,
            "alternatives": alternatives,
            "has_taboo": "暂无发现禁忌" not in explanation
        }

    def get_blessing_options(
        self,
        occasion: str,
        relation: str = None,
        gift_name: str = None,
        count: int = 3
    ) -> Dict:
        """获取祝福语选项"""
        if not self.enhanced:
            return {"success": False, "message": "增强功能未启用"}
        
        options = self.blessing_gen.generate_multiple_options(
            occasion=occasion,
            relation=relation,
            gift_name=gift_name,
            count=count
        )
        
        return {
            "success": True,
            "occasion": occasion,
            "relation": relation,
            "gift_name": gift_name,
            "options": options
        }

    def reset_conversation(self):
        """重置对话"""
        if self.dialog:
            self.dialog.reset_conversation()
        
        self.conversation_state = "initial"
        
        return {
            "success": True,
            "message": "对话已重置"
        }


# 便捷函数
def recommend_gift(
    relation: str = None,
    occasion: str = None,
    budget_min: int = None,
    budget_max: int = None,
    enhanced: bool = True
) -> Dict:
    """便捷推荐"""
    advisor = GiftAdvisor(enhanced=enhanced)
    return advisor.recommend(relation, occasion, budget_min, budget_max)

def parse_and_recommend(text: str, enhanced: bool = True) -> Dict:
    """便捷解析推荐"""
    advisor = GiftAdvisor(enhanced=enhanced)
    return advisor.parse_and_recommend(text)

def explain_gift_taboo(gift_name: str, relation: str = None) -> Dict:
    """便捷解释禁忌"""
    advisor = GiftAdvisor(enhanced=True)
    return advisor.explain_taboo(gift_name, relation)

def get_blessing_options(
    occasion: str,
    relation: str = None,
    gift_name: str = None,
    count: int = 3
) -> Dict:
    """便捷获取祝福语"""
    advisor = GiftAdvisor(enhanced=True)
    return advisor.get_blessing_options(occasion, relation, gift_name, count)


# 测试
if __name__ == "__main__":
    print("=== 测试API ===")
    
    # 推荐测试
    result1 = recommend_gift("妈妈", "生日", 200, 1000)
    print(f"1. 妈妈生日推荐: {result1['count']}个")
    if result1["recommendations"]:
        print("Top 3:")
        for i, rec in enumerate(result1["recommendations"][:3], 1):
            print(f"  {i}. {rec['name']} ({rec['price']}元) - {rec['reason'][:20]}...")
    
    # 禁忌解释
    result2 = explain_gift_taboo("精美时钟", "长辈")
    print(f"\n2. 禁忌解释:")
    print(f"   {result2['explanation'][:60]}...")
    
    # 祝福语选项
    result3 = get_blessing_options("情人节", "女友", "巧克力")
    print(f"\n3. 祝福语选项:")
    for option in result3["options"]:
        print(f"   • {option[:40]}...")
    
    print("\n✅ 测试完成！")