"""Desktop Agent connection / version / grants status queries (0.2.2.50)."""

from __future__ import annotations

import re

_STATUS_QUERY_TOKENS = (
    "версия агента",
    "agent version",
    "версия desktop",
    "desktop agent",
    "подключён ли",
    "подключен ли",
    "есть связь",
    "связь с",
    "connection to",
    "connected to",
    "heartbeat",
    "гранты",
    "grants",
    "разрешения агента",
    "agent permissions",
    "статус агента",
    "agent status",
    "iuve connect agent",
    "connect agent",
    "работает ли агент",
    "is the agent",
    "my desktop agent",
    "мой desktop agent",
    "моим desktop agent",
)
_AGENT_CONTEXT_RE = re.compile(
    r"\b(?:агент|agent|desktop\s+agent|connect\s+agent|iuve\s+connect)\b",
    re.IGNORECASE,
)
_CONNECTION_QUESTION_RE = re.compile(
    r"(?:есть\s+ли\s+связь|do\s+you\s+have\s+(?:a\s+)?connection|"
    r"are\s+you\s+connected|подключён\s+ли|подключен\s+ли)",
    re.IGNORECASE,
)


def is_device_status_query_intent(text: str) -> bool:
    """Agent heartbeat/version/grants — not RAM diagnostics or generic chat."""
    raw = str(text or "").strip()
    if not raw:
        return False
    lower = raw.lower()
    if any(token in lower for token in ("памят", "ram", "оператив", "cpu", "диск", "disk")):
        if not _AGENT_CONTEXT_RE.search(raw):
            return False
    if _CONNECTION_QUESTION_RE.search(raw) and _AGENT_CONTEXT_RE.search(raw):
        return True
    if any(token in lower for token in _STATUS_QUERY_TOKENS):
        return True
    if "?" in raw and _AGENT_CONTEXT_RE.search(raw):
        if any(token in lower for token in ("верси", "version", "статус", "status", "связ", "connect")):
            return True
    return False
