from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Literal

Intent = Literal["chat", "memory_query", "device"]


@dataclass(frozen=True)
class IntentResult:
    intent: Intent
    force_ingest: bool = False
    confidence: float = 1.0


_REMEMBER = re.compile(r"记住|帮我记|别忘了")
_MEMORY_QUERY = re.compile(r"之前|还记得|有没有记过")


def classify_intent(text: str, *, history_len: int = 0) -> IntentResult:
    """规则优先意图分类（chat-llm-design §7.1）。"""
    stripped = text.strip()
    if not stripped:
        return IntentResult(intent="chat", confidence=0.5)

    if _REMEMBER.search(stripped):
        return IntentResult(intent="chat", force_ingest=True, confidence=1.0)

    if stripped.startswith("/query"):
        return IntentResult(intent="memory_query", confidence=1.0)

    if _MEMORY_QUERY.search(stripped):
        return IntentResult(intent="memory_query", confidence=0.9)

    if stripped.startswith("/device"):
        return IntentResult(intent="device", confidence=1.0)

    return IntentResult(intent="chat", confidence=0.8 if history_len else 0.7)
