"""A local-only, human-review-first intake triage example.

SPDX-License-Identifier: MIT

This module performs deterministic pattern matching. It does not call a model,
send data, write files, provide legal advice or approve automated drafting.
"""

from __future__ import annotations

import re
from typing import Dict, List


JURISDICTIONS = (
    ("England and Wales", re.compile(r"\bEngland and Wales\b", re.IGNORECASE)),
    ("European Union", re.compile(r"\b(?:EU|European Union)\b", re.IGNORECASE)),
    ("United Kingdom", re.compile(r"\b(?:UK|United Kingdom)\b", re.IGNORECASE)),
    ("United States", re.compile(r"\b(?:US|USA|United States)\b", re.IGNORECASE)),
    ("New York", re.compile(r"\bNew York\b", re.IGNORECASE)),
    ("California", re.compile(r"\bCalifornia\b", re.IGNORECASE)),
)

RISK_PATTERNS = {
    "confidential_records": re.compile(r"\bconfidential records?\b", re.IGNORECASE),
    "financial_ledger": re.compile(r"\bfinancial ledgers?\b", re.IGNORECASE),
    "restricted_data": re.compile(r"\brestricted data\b", re.IGNORECASE),
    "compliance_breach": re.compile(r"\bcompliance breach(?:es)?\b", re.IGNORECASE),
    "regulatory_penalty": re.compile(r"\bregulatory penalt(?:y|ies)\b", re.IGNORECASE),
}


def process_client_inquiry(incoming_text: str) -> Dict[str, object]:
    """Return routing metadata without retaining or reproducing the source text."""

    if not isinstance(incoming_text, str):
        raise TypeError("incoming_text must be text")
    if not incoming_text.strip():
        raise ValueError("incoming_text cannot be blank")
    if len(incoming_text) > 100_000:
        raise ValueError("incoming_text exceeds the 100,000 character local limit")

    jurisdictions: List[str] = [
        label for label, pattern in JURISDICTIONS if pattern.search(incoming_text)
    ]
    risk_reasons = [
        label for label, pattern in RISK_PATTERNS.items() if pattern.search(incoming_text)
    ]
    restricted = bool(risk_reasons)

    return {
        "processing_status": "LOCALLY_CLASSIFIED",
        "jurisdictions": jurisdictions or ["Not identified"],
        "risk_classification": "RESTRICTED_REVIEW" if restricted else "STANDARD_HUMAN_TRIAGE",
        "risk_reasons": risk_reasons,
        "human_review_required": True,
        "recommended_action": (
            "Pause automation and escalate to a named human reviewer"
            if restricted
            else "Route to a named human reviewer"
        ),
    }
