"""
祝福语生成系统 2.0 - 增强版
支持个性化定制、多风格选择、方言祝福
"""
from typing import Dict, List, Optional
import random

class BlessingGeneratorV2:
    """祝福语生成器 2.0"""

    # 多风格祝福语库
    BLESSINGS = {
        "生日": {
            "标准版": [
                "生日快乐！愿你年年有今日，岁岁有今朝！",
                "祝生日快乐，心想事成，万事如意！",
                "生日快乐！愿你每一天都充满阳光和欢笑！",
            ],
            "深情版": [
                "在这个特别的日子里，愿所有美好都围绕着你！生日快乐！",
                "时光会老去，但我们的友谊/爱情永远不会！生日快乐！",
                "愿你的人生如阳光般灿烂，生日快乐！",
            ],
            "幽默版": [
                "又老一岁啦！不过没关系，年龄只是数字，心态永远18岁！",
                "生日快乐！记得许愿要吹蜡烛，不然蛋糕会长胖！",
                "恭喜你又成功活过一年！继续加油，活到老玩到老！",
            ],
            "简洁版": [
                "生日快乐！",
                "生日快乐，天天开心！",
                "生日快乐，一切顺利！",
            ],
            "文艺版": [
                "愿岁月温柔以待，时光不负你。生日快乐！",
                "一岁一礼，一寸欢喜。生日快乐！",
                "愿你的世界，永远充满阳光和花香。生日快乐！",
            ],
        },
        "情人节": {
            "标准版": [
                "情人节快乐！愿我们的爱情永远甜蜜！",
                "有你的每一天都是情人节，爱你！",
                "愿得一人心，白首不分离。情人节快乐！",
            ],
            "深情版": [
                "遇见你是最美好的意外，爱上你是最正确的决定。情人节快乐！",
                "你的存在，让我的世界都变得明亮。情人节快乐，我的爱人！",
                "时光不老，我们不散。情人节快乐！",
            ],
            "幽默版": [
                "情人节快乐！别问我要什么礼物，我要你...的巧克力！",
                "今天你是我的，巧克力是我的，浪漫也是我的！情人节快乐！",
                "情人节规定：今天必须开心，必须爱我，必须吃糖！",
            ],
            "简洁版": [
                "情人节快乐！",
                "爱你，情人节快乐！",
                "情人节甜蜜！",
            ],
            "文艺版": [
                "你是我此生最美的诗篇，情人节快乐！",
                "星河璀璨，你是我眼中最亮的星。情人节快乐！",
                "春风十里，不如你。情人节快乐！",
            ],
        },
        "纪念日": {
            "标准版": [
                "纪念日快乐！感谢有你相伴！",
                "时光飞逝，爱意永存。纪念日快乐！",
                "每一个纪念日都是我们爱的见证！",
            ],
            "深情版": [
                "从相识到相知，从相知到相爱，感谢你一直在我身边！",
                "多少个日日夜夜，我们携手走过，纪念日快乐！",
                "愿我们的爱，如美酒般越久越醇香。纪念日快乐！",
            ],
            "幽默版": [
                "恭喜我们又成功在一起一年！继续努力，争取下个纪念日！",
                "纪念日快乐！记得我们的约定，吵架不过夜，吃饭不剩饭！",
                "又到纪念日啦！这次准备怎么庆祝？我建议先吃顿好的！",
            ],
            "文艺版": [
                "时光荏苒，岁月如歌，唯有爱永恒。纪念日快乐！",
                "每一寸光阴，都刻着我们的故事。纪念日快乐！",
                "愿我们的爱情，永远如初见般美好。纪念日快乐！",
            ],
        },
        "母亲节": {
            "标准版": [
                "妈妈，您辛苦了！母亲节快乐！",
                "世上只有妈妈好，母亲节快乐！",
                "感谢您的养育之恩，妈妈节日快乐！",
            ],
            "深情版": [
                "妈妈，您是我心中最温暖的港湾。母亲节快乐！",
                "您的爱如阳光，温暖我一生。母亲节快乐！",
                "妈妈，谢谢您给予我生命和爱。母亲节快乐！",
            ],
            "幽默版": [
                "妈妈，今天您最大！想吃什么随便点，我请客！",
                "母亲节快乐！今天您休息，家务我来做！",
                "妈妈，您永远是我心中最美的女神！节日快乐！",
            ],
            "文艺版": [
                "母爱如海，深不见底；母爱如山，高大巍峨。母亲节快乐！",
                "世上最动人的是母爱，最温暖的是妈妈的怀抱。母亲节快乐！",
                "妈妈，您的爱是我一生最珍贵的财富。母亲节快乐！",
            ],
        },
        "父亲节": {
            "标准版": [
                "爸爸，您是我的榜样！父亲节快乐！",
                "父爱如山，感恩有您！节日快乐！",
                "祝爸爸身体健康，节日快乐！",
            ],
            "深情版": [
                "爸爸，您用坚实的臂膀为我撑起一片天。父亲节快乐！",
                "谢谢您默默付出的一切，爸爸。父亲节快乐！",
                "您是我心中最伟大的英雄，爸爸。节日快乐！",
            ],
            "幽默版": [
                "爸爸，今天您说了算！想吃什么随便点！",
                "父亲节快乐！您的'对付我的招数'又升级了吗？",
                "爸爸，您永远是我心中最帅的男神！节日快乐！",
            ],
            "文艺版": [
                "父爱如山，沉稳而厚重；父爱如伞，为我遮风挡雨。父亲节快乐！",
                "您用行动教会我责任和担当。爸爸，父亲节快乐！",
                "父爱无言，却深沉如海。爸爸，节日快乐！",
            ],
        },
        "教师节": {
            "标准版": [
                "老师，您辛苦了！教师节快乐！",
                "桃李满天下，师恩如海深。教师节快乐！",
                "感谢您的教导，祝教师节快乐！",
            ],
            "深情版": [
                "谢谢您点燃我求知的火焰，老师。教师节快乐！",
                "您不仅教会我知识，更教会我做人。感恩有您！",
                "老师，您是我成长路上的明灯。教师节快乐！",
            ],
            "正式版": [
                "辛勤耕耘育桃李，三尺讲台谱华章。教师节快乐！",
                "感谢您的辛勤付出，祝您节日快乐，身体健康！",
                "教诲如春风，师恩似海深。教师节快乐！",
            ],
        },
        "春节": {
            "标准版": [
                "新春快乐，万事如意！",
                "恭喜发财，红包拿来！新年快乐！",
                "愿新年带来好运和幸福！春节快乐！",
            ],
            "传统版": [
                "辞旧迎新，吉祥如意！春节快乐！",
                "金玉满堂，福星高照！新年快乐！",
                "春风送暖，福满人间！春节快乐！",
            ],
            "幽默版": [
                "新年快乐！又到了收红包和长肉的时候啦！",
                "春节快乐！多吃点，反正过年不长肉！",
                "恭喜发财，红包多多！新年快乐！",
            ],
        },
        "中秋": {
            "标准版": [
                "月圆人团圆，中秋节快乐！",
                "但愿人长久，千里共婵娟。中秋快乐！",
                "花好月圆，中秋快乐！",
            ],
            "传统版": [
                "天上月圆，人间团圆。中秋快乐！",
                "月满人圆，家和万事兴。中秋快乐！",
                "佳节共赏天上月，中秋一品人间情。中秋快乐！",
            ],
            "诗意版": [
                "明月千里寄相思，中秋佳节倍思亲。中秋快乐！",
                "海上生明月，天涯共此时。中秋快乐！",
                "月色如水，思念如潮。中秋快乐！",
            ],
        },
        "乔迁": {
            "标准版": [
                "乔迁之喜，新家新气象！",
                "恭喜搬新家，愿你生活更美好！",
                "新居落成，乔迁大吉！",
            ],
            "吉祥版": [
                "迁入新居吉祥如意，搬进高楼福寿安康！",
                "喜迁新居喜洋洋，福星高照福满堂！",
                "新居迎百福，好地风光好！",
            ],
            "简洁版": [
                "恭喜乔迁！",
                "乔迁大吉！",
                "新家快乐！",
            ],
        },
        "结婚": {
            "标准版": [
                "百年好合，永结同心！",
                "祝新婚快乐，早生贵子！",
                "愿你们的爱情甜蜜如初，幸福美满！",
            ],
            "吉祥版": [
                "天作之合，佳偶天成！新婚快乐！",
                "花开并蒂，珠联璧合！新婚快乐！",
                "相亲相爱幸福永，同德同心幸福长！新婚快乐！",
            ],
            "温馨版": [
                "愿你们执子之手，与子偕老！新婚快乐！",
                "从今以后，风雨同舟，甘苦与共！新婚快乐！",
                "愿你们的生活充满甜蜜和欢笑！新婚快乐！",
            ],
        },
        "道歉": {
            "真诚版": [
                "对不起，我知道错了，请给我一个改正的机会！",
                "我的无心之失伤害了你，真的很抱歉！",
                "对不起，我会努力改正，请你原谅！",
            ],
            "直接版": [
                "对不起，我错了！原谅我好吗？",
                "真的很抱歉，我保证以后不会这样了！",
                "对不起，请接受我的歉意！",
            ],
        },
        "表白": {
            "深情版": [
                "从遇见你的那一刻起，我的心就属于你了！",
                "你是我的命中注定，我想和你共度一生！",
                "我愿意用一生的时间，来证明我对你的爱！",
            ],
            "勇敢版": [
                "我喜欢你，可以给我一个机会吗？",
                "你愿意做我的女朋友/男朋友吗？",
                "我想和你在一起，可以吗？",
            ],
            "浪漫版": [
                "春风十里，不如你。你愿意和我一起看遍世间美景吗？",
                "星河璀璨，你是我眼中最亮的星。你愿意成为我的伴侣吗？",
                "岁月静好，我想和你一起慢慢变老。你愿意吗？",
            ],
        },
    }

    # 方言祝福语
    DIALECT_BLESSINGS = {
        "粤语": {
            "生日": ["生日快乐！祝你身体健康，心想事成！", "生日快乐！祝你日日都咁开心！", "生日快乐！福如东海，寿比南山！"],
            "春节": ["恭喜发财！新年快乐！", "新年快乐！身体健康！", "恭喜发财，利是逗来！"],
            "结婚": ["新婚快乐！百年好合！", "恭喜恭喜！早生贵子！", "永结同心，白头偕老！"],
        },
        "四川话": {
            "生日": ["生日快乐！祝你天天开心！", "生日快乐！身体倍儿棒！", "生日快乐！想啥有啥！"],
            "春节": ["新年快乐！恭喜发财！", "新春大吉！万事如意！", "新年快乐，吃好喝好！"],
        },
        "上海话": {
            "生日": ["生日快乐！祝侬身体健康！", "生日快乐！天天开心！", "生日快乐！心想事成！"],
            "春节": ["新年快乐！恭喜发财！", "新春快乐！吉祥如意！", "新年好，万事顺！"],
        },
        "东北话": {
            "生日": ["生日快乐！整得挺像样啊！", "生日快乐！天天乐呵！", "生日快乐！身体杠杠滴！"],
            "春节": ["新年快乐！老铁！", "新春快乐！心想事成！", "过年好！吃好喝好！"],
        },
    }

    # 关系个性化结尾
    RELATION_ENDINGS = {
        "妈妈": ["妈妈，我爱您！", "妈妈，您辛苦了！", "妈妈，谢谢您！"],
        "爸爸": ["爸爸，您辛苦了！", "爸爸，我爱您！", "爸爸，谢谢您！"],
        "女友": ["爱你，宝贝！", "亲爱的，我爱你！", "宝贝，你是我的唯一！"],
        "男友": ["爱你，亲爱的！", "亲爱的，我爱你！", "有你真好！"],
        "老婆": ["老婆，我爱你！", "亲爱的，我爱你！", "谢谢你，我的爱人！"],
        "老公": ["老公，我爱你！", "亲爱的，我爱你！", "谢谢你，我的爱人！"],
        "老师": ["老师，您辛苦了！", "老师，谢谢您！", "感恩有您！"],
        "领导": ["感谢您的指导！", "谢谢您的关心！", "祝您工作顺利！"],
        "朋友": ["朋友，谢谢你！", "友谊万岁！", "有你真好！"],
    }

    def generate(
        self,
        occasion: str = None,
        relation: str = None,
        gift_name: str = None,
        style: str = "标准版",
        dialect: str = None,
        add_relation_ending: bool = True
    ) -> str:
        """
        生成个性化祝福语

        Args:
            occasion: 场合
            relation: 收礼人关系
            gift_name: 礼物名称（可选）
            style: 风格（标准版、深情版、幽默版、简洁版、文艺版）
            dialect: 方言（粤语、四川话等）
            add_relation_ending: 是否添加关系个性化结尾

        Returns:
            个性化祝福语
        """
        # 1. 确定场合
        occasion_key = self._find_occasion_key(occasion)
        
        # 2. 生成基础祝福语
        base_blessing = self._get_base_blessing(occasion_key, style)
        
        # 3. 如有方言要求，转换为方言
        if dialect:
            blessing = self._convert_to_dialect(base_blessing, dialect, occasion_key)
        else:
            blessing = base_blessing
        
        # 4. 添加礼物个性化内容
        if gift_name:
            blessing = self._add_gift_personalization(blessing, gift_name, occasion, relation)
        
        # 5. 添加关系个性化结尾
        if add_relation_ending and relation:
            ending = self._get_relation_ending(relation)
            if ending:
                blessing += f" {ending}"
        
        return blessing

    def _find_occasion_key(self, occasion: str) -> str:
        """查找场合关键词"""
        if not occasion:
            return "默认"
        
        # 精确匹配
        if occasion in self.BLESSINGS:
            return occasion
        
        # 模糊匹配
        occasion_lower = occasion.lower()
        for key in self.BLESSINGS.keys():
            if key in occasion or occasion in key:
                return key
            key_lower = key.lower()
            if key_lower in occasion_lower or occasion_lower in key_lower:
                return key
        
        # 默认
        return "生日"  # 最常见的场合

    def _get_base_blessing(self, occasion_key: str, style: str) -> str:
        """获取基础祝福语"""
        if occasion_key not in self.BLESSINGS:
            occasion_key = "生日"
        
        occasion_blessings = self.BLESSINGS[occasion_key]
        
        # 检查风格是否可用
        if style not in occasion_blessings:
            style = "标准版"
        
        blessings = occasion_blessings[style]
        return random.choice(blessings)

    def _convert_to_dialect(self, blessing: str, dialect: str, occasion_key: str) -> str:
        """转换为方言祝福语"""
        if dialect not in self.DIALECT_BLESSINGS:
            return blessing
        
        dialect_blessings = self.DIALECT_BLESSINGS[dialect]
        if occasion_key in dialect_blessings:
            return random.choice(dialect_blessings[occasion_key])
        
        # 如果该场合没有方言祝福语，返回原版
        return blessing

    def _add_gift_personalization(self, blessing: str, gift_name: str, occasion: str, relation: str) -> str:
        """添加礼物个性化内容"""
        # 礼物的个性化修饰
        gift_modifiers = {
            "鲜花": "花香四溢，",
            "巧克力": "甜蜜在心，",
            "蛋糕": "甜甜蜜蜜，",
            "书籍": "书香满怀，",
            "珠宝": "璀璨夺目，",
            "茶叶": "茶香四溢，",
            "红酒": "醇香美酒，",
            "化妆品": "美丽动人，",
            "电子产品": "科技感十足，",
            "手工品": "亲手制作，",
        }
        
        # 查找匹配的修饰词
        modifier = ""
        for key, value in gift_modifiers.items():
            if key in gift_name:
                modifier = value
                break
        
        # 如有修饰词，添加到祝福语前
        if modifier:
            blessing = modifier + blessing
        
        return blessing

    def _get_relation_ending(self, relation: str) -> Optional[str]:
        """获取关系个性化结尾"""
        # 精确匹配
        if relation in self.RELATION_ENDINGS:
            return random.choice(self.RELATION_ENDINGS[relation])
        
        # 模糊匹配
        for key, endings in self.RELATION_ENDINGS.items():
            if key in relation or relation in key:
                return random.choice(endings)
        
        return None

    def generate_multiple_options(
        self,
        occasion: str = None,
        relation: str = None,
        gift_name: str = None,
        count: int = 3
    ) -> List[str]:
        """
        生成多个风格选项
        
        Args:
            occasion: 场合
            relation: 关系
            gift_name: 礼物名称
            count: 生成数量
            
        Returns:
            多个祝福语选项
        """
        options = []
        
        # 获取可用的风格
        occasion_key = self._find_occasion_key(occasion)
        available_styles = list(self.BLESSINGS.get(occasion_key, self.BLESSINGS["生日"]).keys())
        
        # 如果数量超过可用风格，调整数量
        if count > len(available_styles):
            count = len(available_styles)
        
        # 随机选择不同的风格
        selected_styles = random.sample(available_styles, count)
        
        for style in selected_styles:
            blessing = self.generate(
                occasion=occasion,
                relation=relation,
                gift_name=gift_name,
                style=style
            )
            options.append(f"{style}：{blessing}")
        
        return options

    def generate_for_gift_type(
        self,
        gift_type: str,
        relation: str = None,
        occasion: str = None
    ) -> str:
        """
        根据礼物类型生成特定祝福语
        
        Args:
            gift_type: 礼物类型（鲜花、巧克力、书籍、珠宝、茶叶等）
            relation: 收礼人关系
            occasion: 场合
            
        Returns:
            特定祝福语
        """
        # 礼物类型特定祝福语
        gift_type_blessings = {
            "鲜花": [
                "愿这束鲜花带给你好心情！",
                "鲜花配美人/帅哥，愿你像鲜花一样美丽/帅气！",
                "鲜花代表我的心意，愿你生活如花般灿烂！",
            ],
            "巧克力": [
                "愿生活像巧克力一样甜蜜！",
                "甜甜蜜蜜，幸福满满！",
                "巧克力的甜，就像我对你的心意！",
            ],
            "书籍": [
                "书中自有黄金屋，愿你收获知识和快乐！",
                "愿书香伴你度过美好时光！",
                "读万卷书，行万里路，愿你不断进步！",
            ],
            "珠宝": [
                "愿你如珠宝般闪耀！",
                "珠宝配美人/帅哥，愿你永远光彩照人！",
                "愿这份珍贵，如同我们的情谊！",
            ],
            "茶叶": [
                "茶香四溢，品味人生！",
                "品一壶好茶，享一份宁静！",
                "茶如人生，愿你静心品味！",
            ],
        }
        
        # 获取礼物类型祝福语
        if gift_type in gift_type_blessings:
            base_blessing = random.choice(gift_type_blessings[gift_type])
        else:
            base_blessing = "愿这份礼物带给你快乐！"
        
        # 添加关系结尾
        if relation:
            ending = self._get_relation_ending(relation)
            if ending:
                base_blessing += f" {ending}"
        
        return base_blessing


# 使用示例
if __name__ == "__main__":
    generator = BlessingGeneratorV2()
    
    print("=== 生日祝福语 ===")
    print("标准版：", generator.generate("生日", "妈妈", "鲜花"))
    print("深情版：", generator.generate("生日", "妈妈", "鲜花", style="深情版"))
    print("幽默版：", generator.generate("生日", "妈妈", "鲜花", style="幽默版"))
    
    print("\n=== 粤语生日祝福 ===")
    print(generator.generate("生日", "女友", "鲜花", dialect="粤语"))
    
    print("\n=== 多个选项 ===")
    options = generator.generate_multiple_options("情人节", "女友", "巧克力", 4)
    for i, option in enumerate(options, 1):
        print(f"选项{i}：{option}")
    
    print("\n=== 礼物类型特定祝福 ===")
    print("鲜花：", generator.generate_for_gift_type("鲜花", "老师", "教师节"))
    print("书籍：", generator.generate_for_gift_type("书籍", "学生", "生日"))