import random
import re
from dataclasses import dataclass


@dataclass
class Rule:
    pattern: re.Pattern
    templates: list[str]


REFLECTIONS = {
    "我": "你",
    "我的": "你的",
    "你": "我",
    "你的": "我的",
}


class ElizaBot:
    def __init__(self) -> None:
        self.rules = [
            Rule(re.compile(r".*我想要(.+)"), ["为什么你想要{slot}？", "得到{slot}之后会发生什么？"]),
            Rule(re.compile(r".*我担心(.+)"), ["你担心{slot}，这种担心持续多久了？", "如果{slot}发生，你准备怎么应对？"]),
            Rule(re.compile(r".*因为(.+)"), ["这个原因听起来很重要，能再展开说说吗？", "{slot}和你的目标有什么关系？"]),
        ]

    def reflect(self, text: str) -> str:
        for source, target in REFLECTIONS.items():
            text = text.replace(source, target)
        return text

    def reply(self, message: str) -> str:
        for rule in self.rules:
            match = rule.pattern.match(message)
            if match:
                slot = self.reflect(match.group(1).strip())
                return random.choice(rule.templates).format(slot=slot)
        return random.choice(["继续说。", "这件事里最重要的部分是什么？", "你希望我怎么帮助你？"])


if __name__ == "__main__":
    bot = ElizaBot()
    for message in ["我想要一个学习计划", "我担心学不会 Agent", "因为工具调用太复杂"]:
        print("用户：", message)
        print("ELIZA：", bot.reply(message))
