from __future__ import annotations

import re

from armorer.core.router_models import ExecutionMode, RouteDecision


_RUNTIME_RETRIEVAL_PATTERNS = (
    r"\burl\b",
    r"\blink\b",
    r"\bdashboard\b",
    r"\bendpoint\b",
    r"\bport\b",
    r"\bstatus\b",
    r"\bhealth\b",
    r"\baccess\b",
    r"\bhow do i reach\b",
    r"\bwhere is\b.*\b(?:dashboard|ui|url)\b",
    r"\bwhat(?:'s| is) the\b.*\b(?:url|link|endpoint|port)\b",
)

_SETUP_STATE_RETRIEVAL_PATTERNS = (
    r"\bwhat(?:'s| is)\b.*\b(?:missing|configured|ready|setup|configuration)\b",
    r"\bwhich\b.*\b(?:credential|credentials|token|tokens|channel|channels)\b.*\b(?:missing|configured|enabled)\b",
    r"\bare\b.*\b(?:credentials|tokens|channels|skills)\b.*\b(?:configured|ready|missing)\b",
    r"\bshow\b.*\b(?:setup|configuration)\b.*\bstate\b",
    r"\btell me\b.*\b(?:missing|ready|configured)\b.*\b(?:setup|configuration)\b",
)

_WORKFLOW_PATTERNS = (
    r"\binstall\b",
    r"\buninstall\b",
    r"\bconfigure\b",
    r"\bsetup\b",
    r"\bset up\b",
    r"\bbind\b",
    r"\bpair\b",
    r"\brestart\b",
    r"\brun\b",
    r"\bstart\b",
    r"\bstop\b",
)

_AGENT_PATTERNS = (
    r"\bdebug\b",
    r"\bfix\b",
    r"\binvestigate\b",
    r"\broot cause\b",
    r"\bfigure out\b",
    r"\bwhy\b",
    r"\banalyze\b",
    r"\bdiagnose\b",
    r"\bwhat happened\b",
)


def _matches_any(text: str, patterns: tuple[str, ...]) -> bool:
    return any(re.search(pattern, text) for pattern in patterns)


def looks_like_runtime_retrieval(prompt: str) -> bool:
    text = str(prompt or "").strip().lower()
    return bool(text) and _matches_any(text, _RUNTIME_RETRIEVAL_PATTERNS)


def looks_like_setup_state_retrieval(prompt: str) -> bool:
    text = str(prompt or "").strip().lower()
    return bool(text) and _matches_any(text, _SETUP_STATE_RETRIEVAL_PATTERNS)


def deterministic_route(prompt: str) -> RouteDecision | None:
    text = str(prompt or "").strip().lower()
    if not text:
        return RouteDecision(
            mode=ExecutionMode.RETRIEVAL,
            reason="Empty or whitespace-only request defaults to retrieval handling.",
            confidence=1.0,
            required_tool_groups=["runtime_state"],
        )

    if _matches_any(text, _AGENT_PATTERNS):
        return RouteDecision(
            mode=ExecutionMode.AGENT,
            reason="The request is open-ended troubleshooting or investigation.",
            confidence=0.9,
            required_tool_groups=["runtime_state", "lifecycle", "debug"],
        )

    if looks_like_setup_state_retrieval(text):
        return RouteDecision(
            mode=ExecutionMode.RETRIEVAL,
            reason="The request is asking for current setup/configuration state rather than requesting a mutation.",
            confidence=0.92,
            required_tool_groups=["runtime_state", "setup_state"],
        )

    if looks_like_runtime_retrieval(text):
        return RouteDecision(
            mode=ExecutionMode.RETRIEVAL,
            reason="The request is asking for runtime access details or current status.",
            confidence=0.96,
            required_tool_groups=["runtime_state"],
        )

    if _matches_any(text, _WORKFLOW_PATTERNS):
        return RouteDecision(
            mode=ExecutionMode.WORKFLOW,
            reason="The request is a bounded operational flow such as install, setup, or lifecycle change.",
            confidence=0.88,
            required_tool_groups=["runtime_state", "lifecycle", "setup"],
        )

    return None
