TOOL_NAME = "detect_provider"
TOOL_DESCRIPTION = (
    "Detect which email/calendar provider the user is referring to. "
    "Returns 'outlook_only', 'gmail_only', 'cross_provider', or 'default'. "
    "Recognizes provider names in Dutch and English (Outlook, Gmail, Google Mail, "
    "Google Calendar, Google Agenda, beide, both, allebei, etc.)."
)
TOOL_ARGS = '{"text": {"type": "string", "description": "The user message to analyze for provider references"}}'

import re
from tools._std import tool_ok

_PROVIDER_OUTLOOK_RE = re.compile(r"\boutlook\b", re.IGNORECASE)
_PROVIDER_GMAIL_RE = re.compile(
    r"\b(gmail|google mail|google agenda|google calendar)\b", re.IGNORECASE
)
_BOTH_PROVIDERS_RE = re.compile(
    r"\b(beide|both|allebei|all accounts|across accounts|combine|merge|vergelijk|compare)\b",
    re.IGNORECASE,
)


def run(args: dict) -> str:
    text = str(args.get("text") or "")
    if _BOTH_PROVIDERS_RE.search(text):
        return tool_ok({"provider_scope": "cross_provider"})
    has_outlook = bool(_PROVIDER_OUTLOOK_RE.search(text))
    has_gmail = bool(_PROVIDER_GMAIL_RE.search(text))
    if has_outlook and has_gmail:
        return tool_ok({"provider_scope": "cross_provider"})
    if has_outlook:
        return tool_ok({"provider_scope": "outlook_only"})
    if has_gmail:
        return tool_ok({"provider_scope": "gmail_only"})
    return tool_ok({"provider_scope": "default"})
