from __future__ import annotations

from dataclasses import dataclass, field


@dataclass
class DialogRuleEngine:
    stop_phrases: tuple[str, ...] = (
        "不用了",
        "算了",
        "先不要了",
        "先这样吧",
        "停一下",
        "停止",
        "停止当前任务",
        "结束这次操作",
        "别弄了",
        "不需要了",
    )
    positive_confirmation_tokens: tuple[str, ...] = (
        "确认",
        "好的",
        "是",
        "继续",
        "可以",
        "确定",
        "yes",
        "ok",
    )
    negative_confirmation_tokens: tuple[str, ...] = (
        "取消",
        "不用",
        "不要",
        "否",
        "no",
        "算了",
        "停止",
    )
    confirmation_required_intents: tuple[str, ...] = ("cs_cancel_order",)
    confirmation_required_risk_levels: tuple[str, ...] = ("high",)
    metadata: dict[str, object] = field(default_factory=dict)

    def is_stop_request(self, text: str) -> bool:
        normalized = text.strip().lower().replace(" ", "")
        if not normalized:
            return False
        return any(phrase in normalized for phrase in self.stop_phrases)

    def parse_confirmation_decision(self, text: str) -> bool | None:
        normalized = text.strip().lower()
        if not normalized:
            return None
        if any(token == normalized or token in normalized for token in self.negative_confirmation_tokens):
            return False
        if any(token == normalized or token in normalized for token in self.positive_confirmation_tokens):
            return True
        return None

    def requires_confirmation(self, intent_id: str, risk_level: str) -> bool:
        if intent_id in self.confirmation_required_intents:
            return True
        return risk_level in self.confirmation_required_risk_levels
